Skip to content

Instantly share code, notes, and snippets.

@Dubhead
Created October 21, 2011 08:09
Show Gist options
  • Save Dubhead/1303340 to your computer and use it in GitHub Desktop.
Save Dubhead/1303340 to your computer and use it in GitHub Desktop.
Go to the next (or previous) line with the same indentation to the current line (or less than), ignoring empty lines.
" GoUp
" include guard
"if exists("g:loaded_GoUp") && g:loaded_GoUp
" finish
"endif
"let g:loaded_GoUp = 1
" Go to the next (or previous) line with the same indentation to the
" current line (or less than), ignoring empty lines.
function! GoUp(forward, lessIndent)
if a:forward == 1
let l:increment = 1
else
let l:increment = -1
endif
if a:lessIndent
let l:numIndents = indent(".") - 1
else
let l:numIndents = indent(".")
endif
let l:lastLine = line("$")
let l:destLine = line(".") + l:increment
while (1 <= l:destLine) && (l:destLine <= l:lastLine)
if (indent(l:destLine) <= l:numIndents) && (getline(l:destLine) != "")
call cursor(l:destLine, 0)
return
endif
let l:destLine += l:increment
endwhile
endfunction
" suggested keymaps
noremap <silent> g[ :call GoUp(0, 0)<CR>^
noremap <silent> g] :call GoUp(1, 0)<CR>^
noremap <silent> g{ :call GoUp(0, 1)<CR>^
noremap <silent> g} :call GoUp(1, 1)<CR>^
" eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment