Categories
SysOps

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

I am using the lightline status line with the Solarized color scheme inside Vim text editor.

Light/Dark Solarized theme with lightline status line

Prerequisites

Ensure that terminal is set to the Solarized color scheme.

Terminal color scheme settings

This tutorial requires at least the 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. Still, you can use the older version with pathogen, bundle, 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 a directory that will be used to load main color scheme and status line plugin during application startup.

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

Download the 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 the background color and indirectly main color scheme, then update the status line to reflect changes. It is based on ToggleBG function from a 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.

It looks great.