robromo
robromo

Reputation: 331

Changing Vim background color - and not text color

I was hoping to see if I could change the background color of Vim, so that when I'm insert mode my background changes slightly. (my default is dark grey, i'm hoping to change to light grey).

I was following some other SO posts I found, and tried

autocmd InsertEnter * hi Normal ctermbg=darkgrey
autocmd InsertEnter * hi Normal ctermbg=none

That was slightly working, since it would change my background, but it also changed my text color too - I just want the background changed.

I tried adding in ctermfg (foreground?)

autocmd InsertEnter * hi Normal ctermfg=none ctermbg=darkgrey
autocmd InsertEnter * hi Normal ctermfg=none ctermbg=none

Still did not fix it.

Pictures for reference:

  1. When I open up vi - Default colors - which I like - Command mode When I open up vi - Default colors - which I like - Command mode

  2. When I go into "insert" mode (background subtly changes (good!) text changes (not what I wanted) enter image description here

  3. Escaping out of insert, back to "Command" mode enter image description here

I google'd some more, and found another SO post, that was explaining how the way ctermbg works, is it doesn't just change the background color, instead use

set background=

I tried that too:

autocmd InsertEnter * set background=dark
autocmd InsertLeave * set background=light

It changes the font color and background color, but this time when I go back to command mode the font color is back to normal (along with the background).

Is it possible, to just change the background color between 2 color (light grey and dark grey) without affecting text color, going from Insert Mode to Command Mode?

Upvotes: 4

Views: 9158

Answers (1)

Badacadabra
Badacadabra

Reputation: 8497

Is it possible, to just change the background color between 2 color (light grey and dark grey) without affecting text color, going from Insert Mode to Command Mode?

The answer is yes!

The trick consists in using GUI colors directly (like GVim) instead of terminal ones. Please try this:

  1. Add set termguicolors before setting the colorscheme in your .vimrc
  2. Add autocmd InsertEnter * hi Normal guibg=#4D4D4D
  3. Add autocmd InsertLeave * hi Normal guibg=#333333

If your terminal is compatible and your Vim distribution is up-to-date, it should work.

If you like dark Vim colorschemes, you might be interested in Archery. I have shared the project on GitHub: https://github.com/Badacadabra/vim-archery

Upvotes: 4

Related Questions