Move to Neovim and dein.vim

Move to Neovim and dein.vim

VimからNeovimに移行したので作業記録を晒します。

ついでにvimのプラグインマネージャもNeoBundleからdein.vimに移行しました。

Neovimとは

Neovimとは、より軽量にリファクタされたVimクローンです。luajitでより拡張しやすくもなるようです。

Vimと互換性があり、Vimと全く異なったものではないし、そうしていく予定もない、あくまで既存のVimをベースにより使いやすくしたプロダクトのようです。

dein.vimとは

dein.vimとはNeoBundleの後継となる新しいVimのプラグインマネージャで、もちろんNeovimでも使うことが出来ます。

NeoBundleと比べより高速になり、使いやすくなっています。例えば、プラグインの管理をTOMLで出来るようになっています。

また、そもそもNeoBundleはもうバグフィックス以外はメンテされなくなるので、dein.vimへの移行が推奨されます。

Note: Active developement on NeoBundle has stopped. The only future changes will be bug fixes.

Neovimのインストール

何はともあれNeovimをインストールします。

brew install neovim/neovim/neovim

インストール後に以下のようなアドバイスが表示されます。

To run Neovim, use the "nvim" command (not "neovim"):
    nvim

After installing or upgrading, run the "CheckHealth" command:
    :CheckHealth

To use your existing Vim configuration:
    ln -s ~/.vim ~/.config/nvim
    ln -s ~/.vimrc ~/.config/nvim/init.vim
See ':help nvim' for more information.

Breaking changes (if any) are documented at:
    https://github.com/neovim/neovim/wiki/Following-HEAD

For other questions:
    https://github.com/neovim/neovim/wiki/FAQ

既存のvimの設定をそのまま使いたい場合は、.config以下に既存の.vimディレクトリと.vimrcをそれぞれnviminit.vimとしてシンボリックリンクを張れば良いみたいです。

私は完全移行したいのでシンボリックリンクを張るのではなく、コピーします。

cp -r ~/.vim ~/.config/nvim
cp ~/.vimrc ~/.config/nvim/init.vim

このコピーしたファイルをもとに作り直していきますよ!

freedesktop.orgが主導しているXDG Base Directory Specificationに準拠したため~/.config以下に設定を置くようになったぽい。

dein.vimのインストール

READMEに従って導入していきます。

dein.vimを入れるディレクトリはデフォルト値が無いので自分で指定する必要があります。ただし~/.vim/plugin~/.config/nvim/plugin以下は使えないようです。

私は~/.cache/deinにしました。以下のコマンドでインストールできます。

curl https://raw.githubusercontent.com/Shougo/dein.vim/master/bin/installer.sh > installer.sh
sh ./installer.sh ~/.cache/dein

rm installer.sh

インストールが終わるとdein.vimを動かすために必要な設定が表示されるので、~/.config/nvim/init.vimに追記します(既存のNeoBundleの設定は消します)。

  • 表示される設定は下記のように、.cache/deinのpathがフルパスなので、他の環境(ホームディレクトリ名が違う環境)でも使いまわせるように~/.cache/deinに置換しとくと良さげです
"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif

" Required:
set runtimepath+=/Users/lorentzca/.cache/dein/repos/github.com/Shougo/dein.vim

" Required:
if dein#load_state('/Users/lorentzca/.cache/dein')
  call dein#begin('/Users/lorentzca/.cache/dein')

  " Let dein manage dein
  " Required:
  call dein#add('/Users/lorentzca/.cache/dein/repos/github.com/Shougo/dein.vim')

  " Add or remove your plugins here:
  call dein#add('Shougo/neosnippet.vim')
  call dein#add('Shougo/neosnippet-snippets')

  " You can specify revision/branch/tag.
  call dein#add('Shougo/vimshell', { 'rev': '3787e5' })

  " Required:
  call dein#end()
  call dein#save_state()
endif

" Required:
filetype plugin indent on
syntax enable

" If you want to install not installed plugins on startup.
"if dein#check_install()
"  call dein#install()
"endif

"End dein Scripts-------------------------

デフォルトではプラグインは以下のように記述することでインストールできるみたいですね。

call dein#add('Shougo/neosnippet.vim')
call dein#add('Shougo/neosnippet-snippets')

追記したらNeovimを起動し、dein.vimをインストールします。

:call dein#install()

プラグインはTOMLで管理できるようになったので、続けてやっていきます。

プラグインをTOMLで管理する

まずNeovim側の設定をしていきます。先程dein.vimをインストールした際に追記したdein.vimの設定を書き換えて、TOMLファイルからプラグインを読み込めるようにします。

  • TOMLファイルのpathはとりあえずinit.vimと同じpathにしてみた
  • ファイル名はdein-plugins.tomlにしてみた
  • dein.vimneosnippet.vimneosnippet-snippetsの行を削除しているのは、これらもtomlファイルから読み込めばよいため
    • neosnippet.vimneosnippet-snippetsに関してはrequiredなプラグインでないので消してしまってもok。多分プラグイン導入の書き方の例として書いてあるだけ
  • let s:tomlでtomlという名の変数にtomlファイルのpathを代入している
  • call dein#load_tomlでdeinのload_toml関数を呼び出し、tomlに書かれたブラグインを読み込んでいる
-  " Let dein manage dein
-  " Required:
-  call dein#add('~/.cache/dein/repos/github.com/Shougo/dein.vim')
-
   " Add or remove your plugins here:
-  call dein#add('Shougo/neosnippet.vim')
-  call dein#add('Shougo/neosnippet-snippets')
+  let s:toml = '~/.config/nvim/dein-plugins.toml'
+  call dein#load_toml(s:toml, {'lazy': 0})

TOMLファイルは以下のように書きます。

[[plugins]]
repo = 'Shougo/dein.vim'

[[plugins]]
repo = 'Shougo/neosnippet.vim'

[[plugins]]
repo = 'Shougo/neosnippet-snippets'

インストールします。

:call dein#install()

インストールが成功するとプラグインは~/.cache/dein以下に配置されていきます。

あとはひたすらNeoBundleでインストールしていたプラグインをTOMLファイルに追記していきます。NeoBundleでインストールしたプラグイン一覧は、.vimrcをgrepすればよさそうですね。

$ grep "^NeoBundle\ " ~/Dropbox/dotfiles/vimrc
NeoBundle 'vim-scripts/gtags.vim'
NeoBundle 'scrooloose/nerdtree'
NeoBundle 'kana/vim-operator-user'
...

その他

エイリアス

.zshrcでvimをnvimにエイリアスします!完全移行だ!

alias vim='nvim'

カラースキーム

カラースキームは.config/nvim/color以下にあればOK。

cp -r .cache/dein/repos/github.com/flazz/vim-colorschemes/colors .config/nvim

powerline(まだうまくいっていない)

powerlineと連携させるためにはNeovimがpythonプラグインを読めるように、pythonクライアントを追加してあげる必要があります。

pip install neovim

しかし、これだけではNeovim実行時に以下のエラーが出てしまいます。

Error detected while processing function provider#python#Call:
line   18:
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/powerline/vim.py", line 15, in <module>
    from powerline.bindings.vim import vim_get_func, vim_getvar, get_vim_encoding, python_to_vim
  File "/usr/local/lib/python2.7/site-packages/powerline/bindings/vim/__init__.py", line 195, in <module>
    lambda value: dict((
AttributeError: 'LegacyVim' object has no attribute 'bindeval'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'powerline_setup' is not defined
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'powerline_setup' is not defined

この問題についてはすぐに解決策が見つからず、目下調査中です。。。

感想

ほぼNeovimに移行できた! ✨

あとはdein-plugins.tomlをもっといい感じに使っていこうと思っています(プラグインの遅延読み込みとか)。powerline問題も解決する。

プラグインの情報とメインの設定ファイルをわけられたのでメインの設定ファイルがだいぶスッキリして良い感じです。

参考リンク

Neovim関連。

dein.vim関連。

XDG Base Directory Specificationについて。

LuaJITについて。