tags:

views:

229

answers:

4
+2  Q: 

% VIM Key Command

Is there a way to have % in vim find the next ([{ or whatever, even if it is not on the same line?

Example:

int main(int argc, char ** argv) {
     #Your cursor is somewhere in this comment, I want
     #it to get to the ( after printf
     printf("Hello there.\n");
}
+1  A: 

That's puzzling... For me, % finds the matching close for any {[( no matter how many lines away the match is. I don't see anything in my .vimrc that would change this behaviour, offhand.

Mike G.
+4  A: 

If you want to find opening braces on subsequent lines, without plugins, just enter normal mode and type:

/{ [enter]

Where { is the type of brace your looking for.

You can then browse them all with n and N.

To map the F12 key to turn search highlighting on and off use this trick.

Jim Burger
Good point, however the highlighting cand kind of get annoying, so I was hoping for another option.
To turn off search highlighting, either ":noh" for a temporary fix, or ":set nohlsearch" for a session fix, or put "set nohlsearch" in your .vimrc for a permanent fix.
Greg Hewgill
Well, I like my searches highlighted, but if I am searching for every { in a file, my screen lights up like a flood light.
Typing :noh will stop the highlighting until the next time you search.
Peter Stuifzand
+2  A: 

If I understand correctly, you're trying to to get it to find the opening brace even on the next line. If you're complaining that it doesn't find the closing brace unless the whole thing is one line, I don't know why that wouldn't be working.

In any case, if you want % to have superpowers, the matchit plugin is the place to start. It's included in the normal distribution, so you shouldn't have to download it. Just add

:runtime macros/matchit.vim

To your .vimrc, and % will also know lots of new tricks (how to match balaced XML tags if/then/end if statements in languages that do those with keywords), etc. It won't solve your request directly, since matchit uses the same limitations as normal % (it wasnts the match to start at or after the cursor, on the same line). But since it can use regex searches as match markers (instead of just characters), it should be possible to configure it so the open expression is .\n.{ or some such that would meet that criteria, yet pick up a brace on a line further down.

puetzk
A: 

It looks like Jim Burger has it, but just in case you were actually asking how to search for any of those things:

/[{[(] [Enter]

This will find the next of any of those symbols.

By the way: In this case, vim is smart enough to figure out what you want, but you'll often have to escape a square or round bracket with '\'. For example, to search for the next closing bracket, you would type (Note the \]):

/[\]})] [Enter]