Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

As most of you probably know, you can use % to search forwards on the line to find the next paired brace/bracket/paren and move to its matched partner:

|a|rray[index] = value;
" |e| is the cursor; hit %:
array[index|]| = value;

I am hoping there is a similar key that searches backwards on the line, e.g.:

array[index] = value|;|
" |;| is the cursor again, hit the key I'm looking for:
array|[|index] = value;

Most vim commands have both a backwards and forwards, so it seems this should to. Does it not have a partner? If so, is that because once it's on a paired character they would act the same?

share|improve this question
    
It should be noted that % not only searches for the paired bracket on the same line, but further down the file. I like it especially for loops etc in any program language of choice. –  Bernhard Jan 30 '12 at 18:51
1  
@Bernhard Yes, but only when it finds one of the braces on the line. –  Kevin Jan 30 '12 at 18:54
    
If you are not opposed to addon scripts, the matchit addon provides a g% command that seems similar to what you are looking for. –  jw013 Jan 30 '12 at 20:18
    
Silly but, your question states that you want to know why there is no partner. Do you mean "is there some partner that I don't know about?" :) –  rjewell Jan 31 '12 at 2:51
    
@rjewell OK, I've split it so it's no longer technically asking that. –  Kevin Jan 31 '12 at 3:13
add comment

3 Answers

I just tried :map ^] ^%% (with ^] = Ctrl+v Ctrl+5 here, as an analogy with % = Shift+5).

It works for the specific case given, but because it doesn't search backwards from the cursor, it will always pick the first open parenthesis on the line, and leave your cursor at the start of the line if it doesn't find any match.


This is a better solution; it's ugly and there's probably lots of room to simplify, but I think the semantics are exactly right (doesn't move cursor if there is no open paren, and moves to the first open paren before the current column).

:map ^] ^[:call search("[({[]", "bes", line("."))^M

for

  • ^] = Ctrl+v Ctrl+5
  • ^[ = Ctrl+v Esc
  • ^M = Ctrl+v Return
share|improve this answer
    
This won't quite be the same if there are two non-nested pairs on a line, but it certainly is the best suggestion so far, thanks. –  Kevin Feb 16 '12 at 17:45
    
This will only work within a block, right? If so, then [( would work just as well and doesn't require a map. –  David Feb 16 '12 at 17:54
    
There would have to be a block beginning on the current line, yes. I've found a more complete solution though, edit coming ... –  Useless Feb 16 '12 at 18:12
add comment

If you really want to search backwards for common matching characters, you can use one of these vim-specific commands:

  • [( ...(go to previous unmatched ( character)
  • [{ ...(go to previous unmatched { character)

These two commands have matching forwards partners:

  • ]) ...(go to next unmatched ) character)
  • ]} ...(go to next unmatched } character)

There are other similar commands for #ifdef and for C comments.

You can find more when in vim by using the command :help %.

share|improve this answer
    
I know of these, the problem is they find unmatched pairs, I need to find matched pairs. –  Kevin Feb 16 '12 at 17:05
    
@Kevin: I misunderstood the docs (sigh). Of course, once you've used % then it goes both ways - finding the matched partner. Also, if inside a block, then the above commands will find the appropriate match. –  David Feb 16 '12 at 17:07
add comment

% find the matching bracket. So if you are on ( or { it will search forward.

If you are on a } or ) it will search backwards.

% is not directional, it just find the matching bracket.

share|improve this answer
    
If I'm behind one side of a pair, % will find it's partner. If I'm on the end of a line, it won't find the pair earlier on the line. –  Kevin Feb 17 '12 at 0:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.