sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #177: Highlight matching brackets as one moves in normal mode (plugin)

 tip karma   Rating 185/72, Viewed by 16495 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   December 12, 2001 8:19      complexity:   intermediate
author:   Charles E. Campbell, Jr.      as of Vim:   6.0

Check out http://www.erols.com/astronaut/vim/index.html#VimFuncs for
a plugin script which highlights matching brackets.  The script has
two always-on maps:
\[i   : start [HiMtchBrkt] mode
\[s   : stop [HiMtchBrkt] mode
The plugin will save all user maps and options that the plugin uses and
will restore them when the mode is stopped.

 rate this tip  Life Changing Helpful Unfulfilling 

<< Autocheckout from perforce | Making a "derived" colorscheme without copy & paste >>

Additional Notes

[email protected] - NOSPAM, December 12, 2001 8:21
The script is under the title "Highlighting Matching Brackets"
[email protected], December 27, 2001 2:46
Nice script, but the 'Special' highlighting of most color-schemes isn't very bright, so I changed the 'Special' in line 158 of the script to 'Search', because the 'Search' highlighting is much brighter in most colorschemes, like the one I use (asu1dark). Now the other bracket really 'flashes' on, and I don't have to hurt my eyes searching for a somewhat different coloured bracket.....
[email protected], February 8, 2002 7:38
The highlighting group change sounds ok to me!  The latest version
now incorporates "Search" instead of "Special" for highlighting of
the corresponding curly brace.
[email protected], February 28, 2002 7:10
<himtchbrkt.vim> now supports highlighting the matching bracket with
cursor keys (<up> <down> <right> <left>) in insert mode.
[email protected], March 14, 2002 12:15
HiMtchBrkt now supports the highlighting of matching brackets with
   <home> <end> b w ; ,
and upon termination, unmaps <left> <right> <up> <down> <home> and <end>.
A variable "g:DrChipTopLvlMenu" has the default value "DrChip" which
controls where the menu items go.
[email protected], March 14, 2002 14:08
How about saying in the docs that the keybinding to start highlighting is <leader>[i   instead of \[i.
[email protected], June 29, 2002 14:07
Some note to vim newbies (i am one of them) :

1) I downloaded and compiled vim 6.1 in my home directory and soon realized that if i put HiMtchBrkt.vim in my runtime/plugins dir it is not loaded at all
instead you should create in your homedir (~/) subdirectory "~/.vim/plugins" and put it there.
2) also I ralized that I can't start it running neither by ":\[i" or :<leader>[i
All the time vim reports error: "E10: \ should be followed by /, ? or &"

instead I made very rude and at least working for me "hack" :
opened ~/.vim/plugins/HiMtchBrkt.vim and added two stupid functions just before line "fu! <SID>HMBStart()"

==========two stupid functions==========
fu! BStart()
call <SID>HMBStart()
endfunction
fu! BStop()
call <SID>HMBStop()
endfunction
===========original continues...============
" HMBStart:
fu! <SID>HMBStart()
if exists("b:dohimtchbrkt") && b:dohimtchbrkt == 1
   " already in HiMtchBrkt mode
   echo "[HiMtchBrkt]"
   return
endif
.........
....etc.....
....etc.....
=====================================

Now (as I am real newbie) I can easy and "reliable" start and stop breackets highlighting by executing commands:

:call BStart()
and
:call BStop()

------------------------
This comment was intended only for real vim newbies who just want it to put in work (no more).
I need no credits because hack is very rude and stupid.
Thanks for attention.
--vim newbie.
[email protected], June 29, 2002 14:18
Sorry for misleading a bit: at homedir must be created "~/.vim/plugin"
NOT "~/.vim/plugins"
Anonymous, July 13, 2002 21:20
I-hat...: did you ever realize that \[i should be typed from normal mode?
The colon puts you into command mode, and admittedly \[i is not defined
there.
[email protected], February 10, 2003 23:09

This is the simplest form of this map:

nn <silent> K mk:exe 'match Search /\\%'.line(".").'l/'<CR>
Anonymous, March 18, 2003 12:03
Hmm, the map maxiangjiang provided highlights the entire line whenever a K is pressed.  Doesn't seem to help with highlighting matching braces any.
[email protected], August 5, 2003 1:00
Hi,
I have downloaded  the plugin and  i would like to enable it by default.
The start mapping is '<Leader>[i',  what do i have to do so when I vim a file it will automatically enable it.

Regards,
Maoz.
[email protected], August 5, 2003 13:07
To enable it by default, see vimtip#411 !
[email protected], March 23, 2004 12:48
Hi...great script!
I have one problem with it though: Whenever it is activated, I can't use a range >9 on any commands. Vim always gives the error E481 "No range allowed".
Once I disable the script everythings back to normal.

Any solutions?

Greets larki
vim kat cyruslesser kot com, May 27, 2004 19:00
You might want to add these two, so you can search through your code (for a bracket) and still have syntax highlighting...

nnoremap <silent>   n         n:silent call <SID>HiMatchBracket()<CR>
nnoremap <silent>   N         N:silent call <SID>HiMatchBracket()<CR>
[email protected], June 9, 2004 3:35
Yes, "Error 481" bugs me too ! Any solutions ?
Anonymous, November 2, 2004 9:47
Arg!  This sounded like what I wanted but the link http://www.erols.com/astronaut/vim/index.html#VimFuncs is broken.  8^(
[email protected] - NOSPAM, November 18, 2004 7:29
My website has changed locations; the script is now at:

http://mysite.verizon.net/astronaut/vim/index.html#VimFuncs
under "Highlighting Matching Brackets"
Anonymous, November 23, 2004 9:25
maybe i am just not doing somthing right, but i can't get it to tell me anymore that "match is on current line" and only when a '(' or ')' is selected
[email protected] - NOSPAM, February 2, 2005 8:14
To the preceding anonymous tipper -- please send me an email describing your problem.  Perhaps some option is interfering that I haven't worked around yet, for example.  Its best to give bug reports directly to me; sometimes quite some time can pass before I read the tips to see if anything has been appended there.
Anonymous, July 14, 2005 9:19
When I use this plugin it highlights everything on the page, even if there are no brackets, parens, etc.  I'm using vim 6.3 on x86_64.  Anyone else experienced this?
Anonymous, July 14, 2005 9:29
Ok.. It doesn't highlight the entire line anymore... I closed all running instances of vim and that went away.  However now it doesn't do anything.  I have tried running it both from within vim and automatically at startup.  Any suggestions?
[email protected] - NOSPAM, August 24, 2005 5:53
Usually bugs are caused by some setting that I haven't worked around as yet (largely because I don't use that setting myself and so don't know about its ill effect).  Try  vim -u NONE -N, then source HiMtchBrkt.vim in, see if it works.  If it does, then some setting (or possibly some other plugin) is causing interference.  Try commenting out your settings in your .vimrc; if HiMtchBrkt works, then restore comments until you find out what's causing the problem.  Please let me know so I can workaround such settings!
[email protected], November 10, 2005 9:58
men this site is cooal

Bassalonian
[email protected], April 20, 2006 12:27
WARNING TO GVIM USERS
The plugin is great, but i was strongly discouraged from using it with gvim. Basically, whenever I have cursor over a curly brace, it highlights the matching one, but if I scroll up or down using the scrollbar - gvim shuts down with the following message:

~$ Vim: Caught deadly signal SEGV
Vim: Finished.

Since I'm a vim newbie, I have no idea how to fix it, so I turned it off. Is there a way to contact the author with that bug?
[email protected] - NOSPAM, December 5, 2006 14:12
In the top of my scripts you'll typically find my email.  Of course, you can look at this particular note to find it (just remove the NOSPAM).

Now: I've used gvim and HMBstart without problem, scrolling all the way up and down the file.  When you're talking segment violations, the problem isn't directly with the script or plugin, but rather vim itself.  You should let Bram Moolenaar know how to reproduce the problem:  vim -s $VIMRUNTIME/bugreport.vim  to create a bug report.  You can also join the [email protected] and perhaps others can help.  As is, you haven't provided enough information for me to duplicate your difficulty.
jfe, January 18, 2007 9:18
Is there a way to highlight def-end and if-then-end matches?
I am debugging some code and it seems that there are some
mismatched "end" statements.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo