vimtricks.wiki Concise Vim tricks, one at a time.

How do I tell Vim whether to use light or dark background colors?

Answer

:set background=dark or =light

Explanation

The background option tells Vim (and colorschemes) whether the terminal has a dark or light background. Colorschemes use this setting to select appropriate color palettes. Toggling it can switch between light and dark variants of the same theme.

How it works

  • :set background=dark — assume dark terminal background; use bright/light text colors
  • :set background=light — assume light terminal background; use dark text colors
  • :set background=dark does NOT change the actual terminal background color — it only affects Vim's color choices

Toggle between dark and light

nnoremap <leader>bg :let &background = (&background == 'dark') ? 'light' : 'dark'<CR>

With specific colorschemes

Many popular colorschemes (Solarized, Gruvbox, Tomorrow) have both dark and light variants:

set background=dark
colorscheme gruvbox

or:

set background=light
colorscheme solarized

Order matters

Set background before colorscheme in your vimrc:

set background=dark
colorscheme desert

Setting colorscheme first and then background may reset the colors.

Tips

  • Some colorschemes change background automatically when loaded — check the colorscheme docs
  • set background? shows the current value
  • GUI Vim (gvim) may auto-detect the system theme; terminal Vim cannot and relies on this setting
  • The &background vimscript variable lets you read and write the option programmatically

Next

How do I run a search and replace only within a visually selected region?