Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Go to Beginning/End of Next Method

Is there native functionality in Vim that allows one to move the cursor to the beginning/end of the next method? I already know about [[, ]], [], and ][, but these don't cut the job, because they only work on braces that are in column zero. Hence, they are of little use in, say, navigating C++ code. Is there such a command that is already built into Vim? If not, would you recommend a plugin that does implement it?

Thanks for your help!

Edit: [{ and }] will not work all of the time, because you have to be within the block with the {} (and not in some deeper scope within that block) for you to end up at the right { or } afterwards.

Edit 2: Here's a code listing for which [m and friends don't work.

namespace foo {  #define define_foo         \     template <class T>     \     struct foo_traits<X>   \     {                      \         using foo = X;     \     };  template <class T> struct foo_traits;  define_bar(T*, T*, T*);  template <class T> struct baz;  template <class T> struct baz<T&> {     static T* apply(T& t) { return &t; } };  template <class T> inline T a(T t) { return t; }  } 
like image 462
void-pointer Avatar asked Aug 26 '12 08:08

void-pointer


People also ask

How to jump to beginning of function Vim?

To jump to the beginning of a C code block (while, switch, if etc), use the [{ command. To jump to the end of a C code block (while, switch, if etc), use the ]} command. The above two commands will work from anywhere inside the code block. To jump to the beginning of a parenthesis use the [( command.

How do I go back to previous position in Vim?

To jump back to line # 300 or previous position press CTRL-O (press and hold Ctrl key and press letter O). To jump back forwards press CTRL-I (press and hold Ctrl key and press letter I).

How do I jump to the next word in Vim?

You'll find that Shift-Left/Right and Ctrl-Left/Right will do the trick. Now use CTRL-H and CTRL-L to move by word in insert mode.


2 Answers

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.

like image 64
Ingo Karkat Avatar answered Oct 02 '22 09:10

Ingo Karkat


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> 
like image 39
ZHOU Ling Avatar answered Oct 02 '22 08:10

ZHOU Ling