Vim Tips Wiki
Advertisement

This tip is deprecated for the following reasons:

This feature is available in vim7, see :help matchparen

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Previous TipNext Tip

Tip: #1017 - Highlight text inside matching parentheses

Created: October 10, 2005 19:04 Complexity: basic Author: Carlos Pita Version: 6.0 Karma: 21/12 Imported from: Tip#1017

Sometimes (specially when programming in lisp dialects) it's useful to easily find matching parentheses. vim provides the showmatch option but you need to (re)type the closing parenthesis in order to find the opening one. Alternatively, you can also type '%' over an already written parenthesis so the cursor travel to its matching pair. This is almost ok, but if you are inside a parenthesized form perhaps you don't want to lose you current cursor position but just to find the delimiting parenthesis and continue typing (or whatever you were doing). So I find more convenient to define the following simple key bindings:

nmap <a-key> m[%v%:sleep 350m<CR>`[ 
imap <a-key> <ESC>m[%v%:sleep 350m<CR>`[a 

They simply put a mark, visit the opening parenthesis with '%', enter visual mode (to highlight the parenthesized block), travel to the closing parenthesis with '%', sleep for a while and return to the mark. The second one is for visual mode, so it enters normal mode first and returns to visual mode via 'a' at the end. Of course, they both work with (:), [:], {:} or whatever pairs the matchpairs

option happens to be setted to.

Personally I prefer <M-p> for the key.

Comments

or hit % to go to the matching, then hit % to come back to it's matching (i.e. where you were just at), and continue about your business :-)

Anonymous , October 14, 2005 9:03


It's nice

Anonymous , October 16, 2005 8:53


Although this doesn't introduce anything dramatically new to my Vim editing, it's still a cute, nice tip.

Anonymous , October 17, 2005 11:23


thanks, i really like it, it's way better than just plain '%'

Anonymous , October 19, 2005 22:09


actually, i find it more useful that way:

nmap <C-p> m[vab:sleep 350m<CR>`[ 
imap <C-p> <ESC>m[vab:sleep 350m<CR>`[a 

so you get whole enclosing form marked, and not the 'next after cursor' form.

Anonymous , October 20, 2005 0:04


I can only get this to work for (). I can get % to match (),{},[],<>. When I'm in anything but () it changes to Visual mode then stops.

Any thoughts?

jason . hopkins at med.ge.com , October 20, 2005 12:54


The key to highlighting anything else other than '(' and ')' is in the visual-mode command used to select the text ... in this case 'ab' for 'a block' ... if you use ':help ab' you'll see there are many other ways to select blocks of text:

  • a[ - select a '[ ]' block of text
  • a( - select a '( )' block of text
  • a{ - select a '{ }' block of text
  • a< - select a '< >' block of text
  • ap - select a paragraph
  • as - select a sentence
  • aw - select a word

etc .... So with a few extra mappings, you could select some of the other areas instead:

" highlight a '( )' block of text 
nmap <C-P>9 m[va(:sleep 350m<CR>`[ 

" highlight a '[ ]' block of text 
nmap <C-P>[ m[va[:sleep 350m<CR>`[ 

" highlight a '{ }' block of text 
nmap <C-P>] m[va{:sleep 350m<CR>`[ 

" highlight a '< >' block of text 
nmap <C-P>, m[va<:sleep 350m<CR>`[ 

" enable these commands from 'insert' mode as well 
imap <C-P>9 <ESC><C-P>9a 
imap <C-P>[ <ESC><C-P>[a 
imap <C-P>] <ESC><C-P>]a 
imap <C-P>, <ESC><C-P>,a 

toomuchphp-vim--AT--yahoo.com , October 21, 2005 16:13


nmap <C-p> vabl:sleep 350m<CR>=ab`` 

imap <C-p> <C-O>vabl:sleep 350m<CR><C-O>=ab<C-O>`` 

this way it also do indenting ( note =ab ), which i find useful. and somehow m[ .. `] doesn't work in xterm, but it can be replaced by simple ``

Anonymous , January 21, 2006 7:32


it seems `` is not appropriate here. seems what i have to use some letter for a mark in xterm

Anonymous , January 21, 2006 7:46


Advertisement