This is certainly possible, but it requires a bit of VimScript. The following function does the basic thing:
func SwapHiGroup(group)
let id = synIDtrans(hlID(a:group))
for mode in ['cterm', 'gui']
for g in ['fg', 'bg']
exe 'let '. mode.g. "= synIDattr(id, '".
\ g."#', '". mode. "')"
exe "let ". mode.g. " = empty(". mode.g. ") ? 'NONE' : ". mode.g
endfor
endfor
exe printf('hi %s ctermfg=%s ctermbg=%s guifg=%s guibg=%s', a:group, ctermbg, ctermfg, guibg, guifg)
endfunc
The idea is, to query the syntax attributes using the function synIDattr(). However you need to query each attribute (like 'fg', 'bg', etc) separately. In addition you need to do this separately for the different modes (like 'term', 'cterm', 'gui'), etc. So the whole function is a bit tedious to write. Therefore I made the function dynamically assign the different variables 'ctermfg', 'ctermbg', 'guifg' and 'guibg'. However this makes quoting a bit nasty :(
Note also, the use of querying the attributes with a #
(e.g. fg#
instead of fg
) will translate color names into the RGB
color specification. But that shouldn't be a problem in practice.
In an ideal world, you also would need to query the basic terminal mode term (which I left out here) and the additional attributes like 'strike', 'underline', 'undercurl', 'bold', 'italic', etc. This becomes additional fun if you find the attribute 'inverse' or 'reverse' :)
In addition, you'd need to add some error handling as well.
Note, I would not recommend the use of execute()
, since this depends on parsing the output of the command, which might wrap (on small screen sizes) or add newlines or might even be translated.
And finally, one remark I learned from my work as the vim-airline maintainer. A function that queries all the syntax attributes of a highlighting group several times and executes a new :hi
statement, might slow down Vim considerably, if this happens often. vim-airline does this e.g. when evaluating the statusline and dynamically changes the colors of several highlighting groups and this has been the cause of many performance issues. So be careful with this.