-2

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)?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Jan
  • 6,532
  • 9
  • 37
  • 48
  • 2
    http://www.catb.org/~esr/faqs/smart-questions.html – romainl Aug 12 '22 at 05:23
  • Note to any flaggers/closers, [this question](https://stackoverflow.com/questions/1117526/setting-the-vim-background-colors) is not really a duplicate, as this asks for specific colours and probably their overall syntax – DialFrost Aug 16 '22 at 08:44
  • To OP: I think your question is perfectly valid, but you need to add more context and focus. – DialFrost Aug 16 '22 at 08:53

2 Answers2

1

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]'
funnydman
  • 9,083
  • 4
  • 40
  • 55
Hi computer
  • 946
  • 4
  • 8
  • 19
  • It would be great, if you could provide an example for vim 9, as not everybody can read Chinese (?) here. – Jan Aug 16 '22 at 13:04
  • When I search for autocmd somehow the solutions for version 9 come up first, I thought it is small thing and I just point out what you can do with version 9. As for the example, I haven’t quite figure out the syntax, and I am using version 8 on my computer and it’s hard to test on version 9 . I posted one of the examples on the site. Maybe later I will add useful examples but should be untested – Hi computer Aug 16 '22 at 22:54
0

highlight command (temporary)

Use the highlight command:

:highlight Normal ctermfg=darkgreen ctermbg=gray

This sets the background colour to gray, with dark green text.

Short form:

:hi Normal ctermfg=darkgreen ctermbg=gray

cterm colours [ctermfg+ctermbg]

List of colours [cterm]:

NR-16   NR-8    COLOR NAME 
0       0       Black
1       4       DarkBlue
2       2       DarkGreen
3       6       DarkCyan
4       1       DarkRed
5       5       DarkMagenta
6       3       Brown, DarkYellow
7       7       LightGray, LightGrey, Gray, Grey
8       0*      DarkGray, DarkGrey
9       4*      Blue, LightBlue
10      2*      Green, LightGreen
11      6*      Cyan, LightCyan
12      1*      Red, LightRed
13      5*      Magenta, LightMagenta
14      3*      Yellow, LightYellow
15      7*      White

The number under "NR-16" is used for 16-color terminals ('t_Co' greater than or equal to 16). The number under "NR-8" is used for 8-color terminals ('t_Co' less than 16). The '' indicates that the bold attribute is set for ctermfg. In many 8-color terminals (e.g., "linux"), this causes the bright colors to appear. This doesn't work for background colors! Without the '' the bold attribute is removed. If you want to set the bold attribute in a different way, put a "cterm=" argument AFTER the "ctermfg=" or "ctermbg=" argument. Or use a number instead of a color name.

Here * means 'add 8' so that Blue is 12, DarkGray is 8 etc.

Note the case of the color names is ignored.

Note that this is not permanent, when you close the vim session, the colour scheme is discarded and reset to the default.

Colour change (permanently)

If you want to achieve this permanently, we have to access the vim start up file, .vimrc. The .vimrc file holds the elective runtime settings that get initialized once a vim session begins. We’ll add the color scheme that we want with the help of the colorscheme command:

:colorscheme white

This now configures the default background to be white coloured.

Related sources:

DialFrost
  • 1,610
  • 1
  • 8
  • 28
  • Hi @DialFrost, thanks for the answer. Unfortunately, it did not tell me how the background color changes are to be triggered by a mode change, so I awarded another answer, thanks a lot anyway! – Jan Aug 16 '22 at 12:09
  • "*it did not tell me how the background color changes are to be triggered by a mode change*", not sure what you mean, but glad it was solved :3 @Jan – DialFrost Aug 16 '22 at 12:12
  • Did my code help though? @Jan, Just want to make sure it actually gives info on how to solve it (other than the mode change ofc) :P – DialFrost Aug 16 '22 at 12:14
  • I needed to know, how pressing `I` or `Esc` can trigger the background color change. Now that I am experimenting with it, it only seems to work as expected in nvim, both vim 8 and 9 color the backgound of characters in lines, but leave whitespace unchanged, which does not look good. Other than that, it would be interesting to see how to combine @wu hoyt's solution with yours. – Jan Aug 16 '22 at 13:02