You can get the current settings of a highlight group with the :highlight
command. For example:
:highlight Comment
Comment xxx term=bold ctermfg=4 guifg=Blue
This will just output the text; there is (as far as I know), no VimScript function to get the same text in a string, but not all is lost, since we can use the :redir
command to redirect the output of a command to a variable.
We can then match this, and call the appropriate :highlight
command.
Putting that all together, we end up with this function:
fun! ToggleHighlight()
" Redirect output of :highlight to l:current
redir => l:current
silent highlight Comment
redir END
if l:current =~# '\(cterm\|gui\)=bold'
highlight Comment gui=none cterm=none
else
highlight Comment gui=none cterm=bold
endif
endfun
And you can map this function to <F7>
with:
noremap <F7> <Esc>:call ToggleComments()<CR>
:hi comment gui=bold cterm=bold
, no need for theif
statement here. – Martin Tournoij Jun 28 '15 at 0:03