Linux HowTo: Defining a color to be the same color of comments

Original Source Link

I’m using the indentLine plugin to display line indents with a continuos vertical line, and I’d like to set the color of that line to the same color of my comments.

To get the foreground color of my comments I listed all the color groups with :hi, checked what my colorscheme had set for the guifg of the Comments group, and set it accordingly, i.e. I have this in my init.vim config

let g:indentLine_color_gui='#5c6370'

which works, but if I want to change colorscheme I’d have to do this again. Is there a variable that holds the color of the comments defined by my colorscheme? What I’d like to do is

let g:indentLine_color_gui=<comment_guifg>

There’s :h g:indentLine_defaultGroup specially for this case. So it becomes simply

let g:indentLine_defaultGroup = 'Comment'

In principle, when you need to read in the color from an existing highlight group it’s done like that:

let color = synIDattr(synIDtrans(hlID('Comment')), 'fg')

hlID() returns numerical ID; synIDtrans() follows links if any; synIDattr() returns requested attribute.

This is given as an example in :h hlID() and it’s basically what indentLine plugin does with g:indentLine_defaultGroup for you.

I came up with

execute('highlight Comment')->split('n')->filter({i,v -> v =~# '^Comment'})[0]->split(' ')->filter({i,v -> v =~# 'guifg'})[0]->split('=')

as a way to parse the output of :highlight, but it doesn’t take links into account, so it will probably be wrong. (With dracula it gives Blue, but Comment is linked to DraculaComment which uses a completely different color.)

If the plugin exposes a highlight group, you can just do

augroup myPluginHighlights
  autocmd!
  autocmd ColorScheme * highlight link pluginGroup Comment
augroup END

Tagged : / /

Leave a Reply

Your email address will not be published. Required fields are marked *