Created
April 22, 2012 21:27
-
-
Save averyvery/2467046 to your computer and use it in GitHub Desktop.
Navigating Indents in Vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Indent navigation in vim - https://gist.github.com/gists/2467046/ | |
" Adapted from http://vim.wikia.com/wiki/Back_and_forth_between_indented_lines_again | |
" Use Option+hjkl to navigate to the start/end of indents, or move in/out of indented code | |
" Additionally, combine the actions <, >, d, and y with these four movements | |
" This is useful for quickly performing actions on functions, objects, anything that begins and ends with a matching indent level | |
" | |
" Examples: | |
" <opt-j> at a function's start moves to the end of the function | |
" ><opt-j> at a function's start indents the whole function another level. | |
" y<opt-k> at a function's end yanks the entire thing | |
" d<opt-l> at a function's start will delete the function's contents | |
" <<opt-h> inside a function will unindent the function (by traveling up one indent level, then <ing) | |
function! MoveIndent(forward, outerlevel, innerlevel) | |
let line = line('.') | |
let column = col('.') | |
let lastline = line('$') | |
let indent = indent(line) | |
let stepvalue = a:forward ? 1 : -1 | |
while (line > 0 && line <= lastline) | |
let line = line + stepvalue | |
if (a:out && indent(line) < indent || a:in && indent(line) > indent || indent(line) == indent && !a:out && !a:in) | |
if (strlen(getline(line)) > 0) | |
exe line | |
exe "normal " column . "|" | |
return | |
endif | |
endif | |
endwhile | |
endfunction | |
function! IndentEnd() | |
call MoveIndent(1, 0, 0) | |
endfunction | |
function! IndentStart() | |
call MoveIndent(0, 0, 0) | |
endfunction | |
function! IndentOuter() | |
call MoveIndent(0, 1, 0) | |
endfunction | |
function! IndentInner() | |
call MoveIndent(1, 0, 1) | |
endfunction | |
function! BindIndentAction(key) | |
exe ":nnoremap " . a:key . "<M-j> ^mm :call IndentEnd()<CR> :normal " . a:key . "'m<CR>" | |
exe ":nnoremap " . a:key . "<M-k> ^mm :call IndentStart()<CR> :normal " . a:key . "'m<CR>" | |
exe ":nnoremap " . a:key . "<M-h> :call IndentOuter()<CR> :normal ^mm<CR> :call IndentEnd()<CR> :normal " . a:key . "'m<CR>" | |
exe ":nnoremap " . a:key . "<M-l> :normal ^jmmk<CR> :call IndentEnd()<CR> :normal k<CR> :normal " . a:key . "'m<CR>" | |
endfunction | |
nnoremap <M-j> :call IndentEnd()<CR> | |
nnoremap <M-k> :call IndentStart()<CR> | |
nnoremap <M-h> :call IndentOuter()<CR> | |
nnoremap <M-l> :call IndentInner()<CR> | |
call BindIndentAction('d') | |
call BindIndentAction('y') | |
call BindIndentAction('<') | |
call BindIndentAction('>') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment