Vim: Go to Beginning/End of Next Method

Vim has [m / ]m built in "for Java or similar structured language".

I have written custom versions that handle Vim functions, VBScript, and batch files, among others. These are all powered by my CountJump plugin, which can be used to write custom jump functions based on regular expressions.


I spent hours to make this pattern: /^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{, it works good for me.

EDIT: a better pattern(version 2): /\(\(if\|for\|while\|switch\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{

see the effect here: enter image description here enter image description here

you can map some convenient bindings in your .vimrc, such as:

" jump to the previous function
nnoremap <silent> [f :call search('^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{', "bw")<CR>
" jump to the next function
nnoremap <silent> ]f :call search('^\s*\(\i\+\_[ \t\*]\+\)\+\i\+\_s*(\_[^)]*)\_s*{', "w")<CR>

EDIT: a better pattern(version 2):

" jump to the previous function
nnoremap <silent> [f :call
\ search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{', "bw")<CR>
" jump to the next function
nnoremap <silent> ]f :call
\ search('\(\(if\\|for\\|while\\|switch\\|catch\)\_s*\)\@64<!(\_[^)]*)\_[^;{}()]*\zs{', "w")<CR>

Looks like a duplicate of: Vim [m motion with c#

You could, for instance, give a try to this dirty trick: 9]}. Which just jumps to the 9-th } from the current location (if you're not too nested, should work...)