Skip to content

Instantly share code, notes, and snippets.

@kshenoy
Last active December 17, 2015 19:39
Show Gist options
  • Save kshenoy/5661871 to your computer and use it in GitHub Desktop.
Save kshenoy/5661871 to your computer and use it in GitHub Desktop.
Function to jump to start/end of next/prev method. Basically jumps to { and }. Similar to ]], [[ and ]m, ]M etc. but more useful and convenient to use.
function! MethodJump( dir, pos )
" Description: Jump to next/previous start/end of method
" Arguments:
" dir = 'next' - Jump to next instance
" 'prev' - Jump to previous instance
" pos = 'start' - Jump to start of method
" 'end' - Jump to end of method
let curr_search = @/
let curr_hlsearch = &hlsearch
let curr_wrapscan = &wrapscan
set nohlsearch nowrapscan
let l:dir = ( a:dir ==# "next" ? "" : "b" )
let l:pat = ( a:pos ==# "start" ? '\w\+\s*(\_.\{-}{' : '.\{-}}' )
call search( l:pat, l:dir . 'eW' )
let &hlsearch = curr_hlsearch
let &wrapscan = curr_wrapscan
let @/ = curr_search
endfunction
nnoremap <silent> ][ :call MethodJump( "next", "start" )<CR>
nnoremap <silent> ]] :call MethodJump( "next", "end" )<CR>
nnoremap <silent> [[ :call MethodJump( "prev", "start" )<CR>
nnoremap <silent> [] :call MethodJump( "prev", "end" )<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment