Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move from first character of function declaration to ending brace in VIM

Tags:

vim

If I have a function declared as such:

public static void main(String[] args){
    blahblahlbah;
}

Is there anyway to move from the "p" in public to the ending "}" of the function?

It can be assumed that the method body is of considerable length and does contain curly braces if that makes a difference.

Thanks!

like image 866
Luke Cycon Avatar asked Dec 02 '11 23:12

Luke Cycon


2 Answers

Try this key sequence: f{% Should do it.

like image 148
Herbert Sitz Avatar answered Sep 30 '22 09:09

Herbert Sitz


There is not a few ways to accomplish the movement.

The most appropriate motion command is

]M

which is short and to the point: It moves the cursor to the end of the next method in a Java-like source code.

There are also three satellite motions that together with ]M give possibility to jump to next or previous starts and ends of methods, see :help ]m and below. Therefore, in addition to ]M, in this situation one can issue

]m%

The rest of the answer contains discussion of some tricks that should be used only if the aforementioned motion commands for some reason have failed to solve the problem in your case.

Another simple idea to take advantage of is to jump over the argument list, find the next opening curly brace and move to its closing counterpart,

%l%

or

f{%

or even

/%Enter%

A similar idea to move the cursor to the very beginning of the method's body and than jump to the next unmatched closing curly brace leads to the following command,

j]}

Note, though, that three of the last four commands work only if the function header is a single line. If that is not the case, they need modifications.

Under some assumptions on the code formatting, it is also possible to achieve the same result using plain-text-oriented movements. If the opening curly brace is the last character on the method header line, one can use

$%

or

g_%

if that brace is the last non-blank character.

In conclusion, stick to the ]M movement as far as it works for you (it should, in the vast majority of cases), fall back upon tricks based on combinations of other text motion commands, otherwise.

like image 32
ib. Avatar answered Sep 30 '22 09:09

ib.