Wikia

Vim Tips Wiki

Highlight text inside matching parentheses

Talk0
1,610pages on
this wiki
Tip 1017 Printable Monobook Previous Next

created October 10, 2005 · complexity basic · author Carlos Pita · version 6.0


Sometimes (especially 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 insert mode, so it enters normal mode first and returns to insert mode via 'a' at the end. Of course, they both work with (:), [:], {:} or whatever pairs the matchpairs option happens to be set to.

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

CommentsEdit

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.


Actually, I find it more useful this 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.


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

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 ``


Advertisement | Your ad here

Around Wikia's network

Random Wiki