How to integrate lightline status line plugin for Vim with Solarized theme

I am using lightline status line with Solarized color scheme inside Vim the ubiquitous text editor. It looks great, but there is a small issue that needs to be solved - change status line color scheme alongside main background.

Light/Dark Solarized theme with lightline status line


Ensure that terminal is set to Solarized color scheme.

Terminal color scheme settings

This tutorial requires at least most recent Vim 7.4 (probably the lowest supported version is 7.4.1528 in this case) as it provides native third-party package loading, but you can use older version with pathogen, vundle or vim-plug Vim plugin manager.

Vim version

On the other hand, minipac is a minimal package manager for Vim 8 that takes advantage of new features.

Install plugins

Create directory that will be used to load main color scheme and status line plugin during application startup.

$ mkdir -p ~/.vim/pack/interface/start/

Download Solarized color scheme.

$ git clone ~/.vim/pack/interface/start/vim-colors-solarized

Download lightline status line plugin.

$ git clone ~/.vim/pack/interface/start/lightline.vim

Configure Vim

Create basic ~/.vimrc file with additional ToggleSolarizedTheme() function mapped to F12 key that will change background color and indirectly main color scheme, then update status line to reflect changes. It is based on ToggleBG function from Solarized theme.

" make Vim behave in a more useful way
set nocompatible

" define number of colors
set t_Co=256

" define initial background (light/dark)
set background=dark

" define color scheme
colorscheme solarized

" always display status line
set laststatus=2

" do not show mode
set noshowmode

" define lightline configuration
let g:lightline = {
      \ 'colorscheme': 'solarized',
      \ }

" define function to toggle solarized theme 
" change background and update lightline color scheme
function! ToggleSolarizedTheme()
  let &background = ( &background == "dark"? "light" : "dark" )
  if exists("g:lightline")
    runtime autoload/lightline/colorscheme/solarized.vim
    call lightline#colorscheme()

" map F12 to ToggleSolarizedTheme() function
map <F12> :call ToggleSolarizedTheme()<CR>

Reload configuration file or restart Vim. Use F12 key to swap between dark/light color scheme.