Skip to content

Instantly share code, notes, and snippets.

@leonid-ed
Created June 11, 2020 08:57
Show Gist options
  • Save leonid-ed/7bcbb8c6bc30afa62539e57a4c5e516c to your computer and use it in GitHub Desktop.
Save leonid-ed/7bcbb8c6bc30afa62539e57a4c5e516c to your computer and use it in GitHub Desktop.
SelectIndentOn() function for VIM to select all the lines with the same indent level in Python
" Inspired by https://vim.fandom.com/wiki/Visual_selection_of_indent_block
" This version additionaly goes through empty lines to select also separated blocks
" on the same indent level.
function SelectIndentOn()
let temp_var=indent(line("."))
if temp_var is 0 | return | endif
while (line(".") > 1) &&
\ ((indent(line(".")-1) >= temp_var) || (getline(line(".")-1) is ""))
exe "normal k"
endwhile
let empty_lines = 0
exe "normal V"
while (line(".") < line("$")) &&
\ ((indent(line(".")+1) >= temp_var) || (getline(line(".")+1) is ""))
exe "normal j"
if getline(line(".")) is ""
let empty_lines += 1
else
let empty_lines = 0
endif
endwhile
if empty_lines > 0 | exe "normal " . empty_lines . "k" | endif
exe "normal ^"
endfun
noremap <F5> :call SelectIndentOn()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment