2

I have the following Vim code which highlights trailing whitespace in red:

highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/

But instead of making the color "red", I want to use the color scheme's default error color. I tried using "error" instead of "red", but that didn't work and it gave me an error saying that the color is not recognized.

1 Answer 1

3

One simple way is to link ExtraWhitespace to Error with

colorscheme whatever
hi link ExtraWhitespace Error

Or if you use :h colorscheme-override:

augroup my_colorschemes
  au!
  au Colorscheme pablo hi link ExtraWhitespace Error
augroup END
colorscheme pablo

In recent vim you can also use combination of hlget()/hlset() functions to target specific colors in highlighting:

let hl_err = hlget('Error')[0]
call hlset([{"name": "ExtraWhitespace", "ctermbg": hl_err.ctermbg, "guibg": hl_err.guibg}])
6
  • The first solution worked for me but I would like to inverse the foreground and background colors. Is this possible? As for the third solution, it told me that the hlget function doesn't exist, I am on Neovim version 0.7.2.
    – Amarakon
    Sep 1, 2022 at 22:27
  • @Amarakon, I don't know if neovim has similar functions, you can check its help, I guess.
    – Maxim Kim
    Sep 2, 2022 at 4:27
  • hlget() was added to Vim fairly recently @Amarakon (Nov last year); there's a PR to port it to Neovim, but it hasn't seen activity for a while. Sep 2, 2022 at 7:12
  • Is there a way to use highlight link ExtraWhiteSpace Error and then invert ExtraWhiteSpace foreground and background colors without using hlget()?
    – Amarakon
    Sep 2, 2022 at 13:37
  • @Amarakon I don't think so. But you can pick up colors Error uses and use them directly.
    – Maxim Kim
    Sep 2, 2022 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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