Vim logo vim online Vim Book Ad

basic Tip #163: Toggle Search Highlighting

 tip karma   Rating 16/18, Viewed by 913 

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

[email protected], November 12, 2001 7:07
You can also do something like :
noremap XX :set hlsearch!<cr>:set hlsearch?<cr>

<advertize>
And take a look at http://vim.sourceforge.net/scripts/script.php?script_id=48 for more complex toggle actions.
</adv>
[email protected], 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.
[email protected], 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 [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo