lightline.vimを使ってVimのステータスラインを有効活用する

ステータスラインが一瞬でリッチに

lightline.vimVimのステータスラインをかっこよくしてくれるVimプラグインです。
作者さんによるこのプラグインについての詳細な解説はこちらです。

結果、下の画面になりました。
f:id:yuhei_kagaya:20130920221254p:plain
(カラースキーマmrkn256、フォントはRicty Regular for Powerline.ttfをつかっています)


僕は今まで、ステータスライン系のプラグインを
vim-powerline → powerline → vim-airline と流行にもれず使ってきたのですがlightline.vimに落ち着きました。簡単にかっこよくなるし、動作が軽快でカスタマイズもしやすくて素晴らしいです。感謝!

ステータスラインに表示している情報

ステータスラインを構成する要素はコンポーネント(component)と呼ばれています。
次の作者さんらの記事を読んで、コンポーネントを配置してみました。


f:id:yuhei_kagaya:20130920221029p:plain
※ステータスラインの左側から順に

表示 内容 備考
NORMAL Vimのモード 下の設定におけるMyMode()関数で返している内容。
master gitのブランチ名 下の設定におけるMyFugitive()関数で返している内容。fugitiveプラグインが必要。masterの左側の文字はPowerline用のフォント。ターミナルを起動している端末(ここではMac)にインストールして、ターミナルの表示用フォントとして設定していると表示できる。
AdminController.php バッファに表示中のファイル名 下のMyFilename()関数で返している内容。
+ ファイルを編集したら+と表示される
controller バッファに表示中のCakePHPクラスのタイプ 下のMyCakephp()関数で返している内容。cake.vimプラグインが必要。
beforeFilter カーソルがある場所の関数名など 下の設定におけるMyCurrentTag()関数で返している内容。tagbarプラグインが必要。
filter(3/3) バッファに表示中のファイル内で検索中の単語(filter)に該当した数3と、検索位置 下の設定におけるcomponent_function.anzuで返している内容(anzu#search_status()で返す文字列)。vim-anzuプラグインが必要。
unix 改行コード(unix=LF) 下の設定におけるMyFileformat()関数で返している内容。
utf-8 文字コード 下の設定におけるMyFileencoding()関数で返している内容。
php ファイルタイプ 下の設定におけるMyFiletype()関数で返している内容。
11% LN 17:23 カーソルの位置情報(全体における上から11%の場所、17行目23列) 下の設定におけるcomponent.lineinfo。LNはPowerline用のフォント(上記masterの項と同じ)。

必要なプラグインをインストールして、.vimrcに次の設定を貼り付けると動くと思います。

let g:lightline = {
      \ 'colorscheme': 'powerline',
      \ 'mode_map': {'c': 'NORMAL'},
      \ 'active': {
      \   'left': [ ['mode', 'paste'], ['fugitive', 'filename', 'cakephp', 'currenttag', 'anzu'] ]
      \ },
      \ 'component': {
      \   'lineinfo': ' %3l:%-2v',
      \ },
      \ 'component_function': {
      \   'modified': 'MyModified',
      \   'readonly': 'MyReadonly',
      \   'fugitive': 'MyFugitive',
      \   'filename': 'MyFilename',
      \   'fileformat': 'MyFileformat',
      \   'filetype': 'MyFiletype',
      \   'fileencoding': 'MyFileencoding',
      \   'mode': 'MyMode',
      \   'anzu': 'anzu#search_status',
      \   'currenttag': 'MyCurrentTag',
      \   'cakephp': 'MyCakephp',
      \ }
      \ }


function! MyModified()
  return &ft =~ 'help\|vimfiler\|gundo' ? '' : &modified ? '+' : &modifiable ? '' : '-'
endfunction

function! MyReadonly()
  return &ft !~? 'help\|vimfiler\|gundo' && &readonly ? ' ' : ''
endfunction

function! MyFilename()
  return ('' != MyReadonly() ? MyReadonly() . ' ' : '') .
        \ (&ft == 'vimfiler' ? vimfiler#get_status_string() :
        \  &ft == 'unite' ? unite#get_status_string() :
        \  &ft == 'vimshell' ? vimshell#get_status_string() :
        \ '' != expand('%:t') ? expand('%:t') : '[No Name]') .
        \ ('' != MyModified() ? ' ' . MyModified() : '')
endfunction

function! MyFugitive()
  try
    if &ft !~? 'vimfiler\|gundo' && exists('*fugitive#head') && strlen(fugitive#head())
      return ' ' . fugitive#head()
    endif
  catch
  endtry
  return ''
endfunction

function! MyFileformat()
  return winwidth(0) > 70 ? &fileformat : ''
endfunction

function! MyFiletype()
  return winwidth(0) > 70 ? (strlen(&filetype) ? &filetype : 'no ft') : ''
endfunction

function! MyFileencoding()
  return winwidth(0) > 70 ? (strlen(&fenc) ? &fenc : &enc) : ''
endfunction

function! MyMode()
  return winwidth(0) > 60 ? lightline#mode() : ''
endfunction

function! MyCurrentTag()
  return tagbar#currenttag('%s', '')
endfunction

function! MyCakephp()
  return exists('*cake#buffer') ? cake#buffer('type') : ''
endfunction


このように、コンポーネントは簡単に追加することができました。
ステータスラインにいろいろ表示させてみたくなりますね!楽しい!