LoginSignup
12

More than 5 years have passed since last update.

vim-airline にカーソル下の文字コードを表示する

Last updated at Posted at 2013-08-21

vim-airline が流行ってるみたいですね。
流れに乗って私も vim-powerline から乗り換えてみました。

vim-powerline では文字コードの表示が可能だったので vim-airline でもやってみました。
(結構便利なので個人的には必須)

vim-airline 2.png

実装

文字コードを取得する部分は vim-powerline からパクry…流用しました。

以下のコードを .vimrc に書けば画像のように表示されます。

" https://github.com/Lokaltog/vim-powerline/blob/develop/autoload/Powerline/Functions.vim
function! GetCharCode()
  " Get the output of :ascii
  redir => ascii
  silent! ascii
  redir END

  if match(ascii, 'NUL') != -1
    return 'NUL'
  endif

  " Zero pad hex values
  let nrformat = '0x%02x'

  let encoding = (&fenc == '' ? &enc : &fenc)

  if encoding == 'utf-8'
    " Zero pad with 4 zeroes in unicode files
    let nrformat = '0x%04x'
  endif

  " Get the character and the numeric value from the return value of :ascii
  " This matches the two first pieces of the return value, e.g.
  " "<F>  70" => char: 'F', nr: '70'
  let [str, char, nr; rest] = matchlist(ascii, '\v\<(.{-1,})\>\s*([0-9]+)')

  " Format the numeric value
  let nr = printf(nrformat, nr)

  return "'". char ."' ". nr
endfunction

function! GetEncoding()
  let fenc = strlen(&fenc) >0 ? &fenc : ''
  let ff = strlen(&ff) > 0 ? &ff : ''
  return fenc . '[' . ff . ']'
endfunction

" Display charcode, fileencoding and fileformat.
let g:airline_section_y = '%{GetCharCode()} %{g:airline_right_alt_sep} %{GetEncoding()}'

見た目が気に入らない方は適当に弄って下さい。

デフォルトの設定を流用してみる

fileencodingfiileformat の表示がデフォルトと同じで良いなら以下のようにしても良いかも

ただし NeoBundle 限定です。

let bundle = neobundle#get('vim-airline')
function! bundle.hooks.on_source(bundle)
  let g:airline_section_y = '%{GetCharCode()} %{g:airline_right_alt_sep} ' . g:airline_section_y
  call airline#update_statusline()
endfunction
unlet bundle

この方法の場合は GetEncoding() 関数は不要になります。

もっと良い方法があれば教えて下さい。

ちなみに私の vim-airline の設定は以下のような感じになってます。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12