sponsor Vim development Vim logo Vim Book Ad

basic Tip #163: Toggle Search Highlighting

 tip karma   Rating 29/25, Viewed by 2139 

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

created:   November 11, 2001 13:11      complexity:   basic
author:   Bindu Wavell      as of Vim:   6.0

" Map H to toggle search highlighting
map H :let &hlsearch = !&hlsearch<CR>

 rate this tip  Life Changing Helpful Unfulfilling 

<< write plugin with explorer like interfaces | Make non-ASCII characters displayed on console >>

Additional Notes

hermitte@free.fr, November 12, 2001 7:07
You can also do something like :
noremap XX :set hlsearch!<cr>:set hlsearch?<cr>

And take a look at http://vim.sourceforge.net/scripts/script.php?script_id=48 for more complex toggle actions.
coryechols@yahoo.com, November 14, 2001 4:52
I think the simplest way to do this is :noremap XX :set invhlsearch<CR>

For options which have only "on" or "off" settings, giving the 'inv' prefix to the option causes it to be toggled.
hermitte@free.fr, November 15, 2001 5:07
The "inv" prefix only works with versions 6.x of VIM. It seems to do strictly the same
thing '!' does. But it does not echoes the value of the option.

Hence, 'set option?<cr>' is still useful if you want to know its new current state.
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.
Sponsored by Web Concept Group Inc. SourceForge.net Logo