1

I have this keymap: map <F3> :hi Tab gui=underline guifg=8 ctermbg=8<CR> and I know that, I can clear group by :hi clear Tab, but how to map only F3 to do this? I want one key to turn on/off this higlight group, like: map <F3> :set number!<CR>.

Regards.

3

Here is a different approach, doing it in a single line:

nnoremap <expr><silent> <f3>  synIDattr(hlID('Tab'),'underline')?':hi clear Tab<cr>':':hi Tab gui=underline guifg=8 ctermbg=8<cr>'

This uses expression mappings to check the underline attribute of the Tab highlighting. If there exists one, it will clear the highlighting and if there does not exist one, it will recreate it.

This is explained in the help at :h :map-expression and the if-then-else expression (condition?then:else) at :h expr1

  • Thanks! I was thinking about some function, as saginaw presented, but this is better solution. Thanks! – Nolias Nov 18 '15 at 15:46
1

I may be wrong but I don't think you can give a decimal number as a value for the guifg attribute (guifg=8).
When I type :hi Tab gui=underline guifg=8 ctermbg=8 inside a terminal it works, but inside a gui, gvim returns the error message E254: Cannot allocate color8.

Here's what the help says about this error (:help e254) :

The color name {name} is unknown. See |gui-colors| for a list of colors that are available on most systems.

And the help about gui-colors (:help gui-colors) talks about color names, or hexadecimal values in the form of #rrggbb (#rr= red, #gg=green, #bb=blue).

But maybe I misunderstood something. Anyway, concerning your problem, here's what I would do.

  • Find a way to get the value of one of the attributes that I want to set.
    For example, you want to give to the guifg attribute of the Tab highlight group the value of 8. To get this value from a vim script you can use this expression :

    synIDattr(hlID('Tab'), 'fg#')

  • Assign this value to a variable :

    let guifgcolor=synIDattr(hlID('Tab'), 'fg#')

  • Write a function that tests the value of the variable. If it's not the one that I want (8 in this example), then execute the command hi Tab gui=underline guifg=8 ctermbg=8 and if it is then execute hi clear Tab.

  • Write a mapping that calls the function.

It could give something like this :

function! ToggleHighlightGroup()

    let guifgcolor=synIDattr(hlID('Tab'), 'fg#')

    if guifgcolor == 8
        highlight clear Tab
    else
        highlight Tab gui=underline guifg=8 ctermbg=8
    endif

endfunction

nnoremap <F3> :<c-u>call ToggleHighlightGroup()<cr>

Edit : VanLaser suggests a good link that explains how to toggle an option in more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.