Search code examples
vim

Can vim's background color be changed based on the current mode?


Just to make the title a bit more verbose:

Emphasis is on: "based on the current mode"

I would like the background to be one color when in normal or visual mode and in another when I can freely type (insert mode), automatically (=> probably autocmd, as mentioned in the solution below).

How can this be done the best way, that works in all modern flavors of vim (especially including terminal rendering)?


Solution

  • check autocmd

    http://vimdoc.sourceforge.net/htmldoc/autocmd.html

    for vim 8

    :autocmd InsertEnter * set bg=light
    :autocmd InsertLeave * set bg=dark
    

    for vim version 9.0 please check

    https://yianwillis.github.io/vimcdoc/doc/autocmd.html#ModeChanged

    For the example in the site, you can change to relative numbering when enter visual mode

    :au ModeChanged [vV\x16]*:* let &l:rnu = mode() =~# '^[vV\x16]'
    :au ModeChanged *:[vV\x16]* let &l:rnu = mode() =~# '^[vV\x16]'
    :au WinEnter,WinLeave * let &l:rnu = mode() =~# '^[vV\x16]'