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 : / /

Server Bug Fix: How can I change the colorscheme of the vim terminal buffer?

Original Source Link

I use GVim with the gruvbox colorscheme. When I open a terminal inside vim with :term, the colors are different, it looks like another colorscheme is applied.

enter image description here

The other terminals I use (urxvt and xfce4-terminal) also look nothing like what I see in the vim terminal.

Where is the coloscheme of the vim terminal buffer configured and how can I change it ?

In GVim, you can control the 16 ANSI colors used by your terminal with the g:terminal_ansi_colors variable.

See :help g:terminal_ansi_colors:

In GUI mode or with 'termguicolors', the 16 ANSI colors used by default in new
terminal windows may be configured using the variable g:terminal_ansi_colors, which should be a list of 16 color names or
hexadecimal color codes, similar to those accepted by highlight-guifg. […]

The term_setansicolors() function can be used to change the colors, and
term_getansicolors() to get the currently used colors.

In modern vim/neovim there is :h g:terminal_ansi_colors (vim) or 16 of g:terminal_color_0 .. g:terminal_color_15 (neovim).

If you open gruvbox colorscheme you use, you will find those definitions there (most probably).

Basically:

  1. your whatever terminal can have palette of base 16 colors defined (there are defaults for each type of terminal, but you can also redefine it).
  2. In Vim when you open a :term those default/redefined base 16 colors are applied to it unless you or a colorscheme again redefine them with g:terminal_ansi_colors to match your colorscheme
Tagged : / / /

Server Bug Fix: Is it possible to match vim and gvim colorschemes?

Original Source Link

This was a nice little article that somewhat demystified vim colors to me.

I downloaded and installed the Base2Tone vim colorschemes, and set the following in my ~/.vimrc:

colorscheme Base2Tone_PoolDark
hi LineNr ctermfg=yellow ctermbg=NONE

I was mostly satisfied with the result in vim:
enter image description here

But I much prefer how it renders in gvim:
enter image description here

Is there some way I can get vim‘s colorscheme rendering more like gvim‘s?


Note: I mentioned the hi LineNr because I’m curious why the line numbers don’t render yellow in gvim – but I don’t necessarily want to clutter this post with too many incidental questions – I’m mostly interested in the background color rendering. To my eye, it looks like the text (foreground?) colors are a pretty reasonable match.


UPDATE:

Based on @filbranden’s answer, I tried export TERM=xterm-true-color and restarting vim, and this looks like it’s on the right track, but it’s still a bit off — it looks like it’s filling spaces past EOL with black; does anyone recognize the issue, and what setting to change so there isn’t that mismatched background color?
enter image description here

Highlight in gui or true color terminal with :h termguicolors is not affect by :h cterm, cterm is used for 8, 16, 256 color terminal. This command should change gui LineNr:

hi LineNr guifg=#ff0000 guibg=#000000

A quick look at the source, this colorscheme use only 16colors, it doesn’t use ansi 16 colors, it works for gui, true color and 256color terminal.

If your terminal has truecolor, you can turn on termguicolors, it will use gui color to render terminal.

If your terminal is 256color, you must setup your 256 color palette, otherwise it will be degraded.

Change xterm-256color color palette

color palette is usually set by Operating System Command (OSC) characters, it’s ESC] in xterm

ctlseqs.txt.gz
---------------------
Operating System Commands
...
OSC Ps ; Pt ST
...
            Ps = 4 ; c; spec -> Change Color Number c to the color spec-
          ified by spec.  This can be a name or RGB specification as per
          XParseColor.  Any number of c/spec pairs may be given.  The
          color numbers correspond to the ANSI colors 0-7, their bright
          versions 8-15, and if supported, the remainder of the 88-color
          or 256-color table.

It’s a bit hard to read, it basically says you can change your 256 color palette by:

{OSC characters}4;{color index};{XParseColor color specification}{ST characters}

We already know OSC, color index is 0-255, ST is ESC, You can find XParseColor specification in man xparsecolor:

...

COLOR NAMES
       An RGB Device specification is identified by the prefix ``rgb:'' and conforms to the follow‐
       ing syntax:

       rgb:<red>/<green>/<blue>

Put everything together:

printf "33]4;235;rgb:30/30/3033\"

Above command will change color 235 to #303030

Different TERM use different escape sequece to change 256 color palette. This might only applied to xterm family terminal.

Popular colorscheme such as gruvbox will provide a shell script to help you setup your color palette.

Is there some way I can get vim’s colorscheme rendering more like gvim’s?

Yes! As long as your terminal supports “true color,” you can ask Vim to use the gvim color settings of the colorscheme by enabling the 'termguicolors' setting:

When on, uses highlight-guifg and highlight-guibg attributes in the terminal (thus using 24-bit color).

Requires a ISO-8613-3 compatible terminal. If setting this option does not work (produces a colorless UI) reading xterm-true-color might help.

Simply add this line to your vimrc:

set termguicolors

If that doesn’t work, take a further look at the documentation to ensure your terminal supports this feature and is properly configured to use it.


Your terminal emulator, MobaXterm, is supposed to support “true color”, so it should be possible to make it work.

If setting TERM=xterm-true-color is giving you trouble, then try to keep TERM=xterm-256color and set vim options t_8f and t_8b explicitly, as recommended in :help xterm-true-color:

let &t_8f = "<Esc>[38:2:%lu:%lu:%lum"
let &t_8b = "<Esc>[48:2:%lu:%lu:%lum"

(And, if that doesn’t work, try using semicolons instead of colons in those expressions.)

Tagged : / /