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

Prerequisites

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 adavantage 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 https://github.com/altercation/vim-colors-solarized ~/.vim/pack/interface/start/vim-colors-solarized

Download lightline status line plugin.

$ git clone https://github.com/itchyny/lightline.vim ~/.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 satus line to reflect changes. It is based on ToggleBG funcion 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()
  endif
endfunction

" 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.

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.