極楽とんぼのロボット製作記

情報工学系大学院生がロボットとその周辺技術や身の回りの出来事について紹介するブログ

新しいVim、"Neovim"を導入する

Neovim導入の仕方を説明します。

Neovimのインストール

参考サイト
github.com
Neovimをインストールする手順は以下の通りです。

$ sudo add-apt-repository ppa:neovim-ppa/stable
$ sudo apt-get update
$ sudo apt-get install neovim

インストールが終了したら

$ nvim

と打ってneovimを起動してみましょう。
f:id:gokuraku104robot:20171130152850p:plain

vimやviでnvimを起動するようにする

お好みですが、vimやviなどでneovimを起動するには以下の設定をします。

$ sudo update-alternatives --install /usr/bin/vi vi /usr/bin/nvim 60
$ sudo update-alternatives --config vi
$ sudo update-alternatives --install /usr/bin/vim vim /usr/bin/nvim 60
$ sudo update-alternatives --config vim

.vimrcの引っ越し

NeovimでもこれまでのVimの設定をほぼそのまま利用することができます。Neovimでは設定ファイルはinit.vimで、~/.config/nvim/以下に置かれます。
VimとNeovimで同じ設定をしたい場合は

ln -s ~/.vimrc ~/.config/nvim/init.vim

としてシンボリックリンクを貼ればNeovimが読み込んでくれます。
NeovimとVimの設定を分けたい場合はコピーすれば可能です。

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

VimからNeovimに以降するにあたって、プラグイン管理のプラグインを変更するため、私はVimとNeovimの設定は分けて管理することにしています。

プラグインを管理する

VimではNeoBundleを使用していましたが、Neovimではdein.vimを使用します。
参考サイト
github.com
qiita.com

dein.vimのダウンロード方法

dein.vimをダウンロードするには以下のようにします。

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

ダウンロード先はお好みですが"~/.vim/plugin" や "~/.config/nvim/plugin"は禁止されています。
インストールはNeovim上で

:call dein#install()

とします。

dein.tomlの書き方

管理するプラグインは通常dein.tomlに書きます。以下に一例を示します。

# dein.toml
# deoplete
[[plugins]]
repo = 'Shougo/deoplete.nvim'

# denite
[[plugins]]
repo = 'Shougo/denite.nvim'

# easy commentout
[[plugins]]
repo = 'tomtom/tcomment_vim'

# expantion copy and paste
[[plugins]]
repo = 'LeafCage/yankround.vim'
hook_add = '''
	nmap p <Plug>(yankround-p)
	xmap p <Plug>(yankround-p)
	nmap P <Plug>(yankround-P)
	nmap gp <Plug>(yankround-gp)
	xmap gp <Plug>(yankround-gp)
	nmap gP <Plug>(yankround-gP)
	nmap <C-p> <Plug>(yankround-prev)
	nmap <C-n> <Plug>(yankround-next)
	'''

基本的にはNeovimと同様にGithubのリポジトリを指定してやれば自動的にインストールされます。プラグインごとの設定はhook_addを利用して設定できます。
dein.tomlの保存場所は基本的に自由ですが、私は~/.config/nvim/dein/に保存しています。

dein.tomlをダウンロードし、読み込むinit.vimの書き方

dein.tomlを書いただけではNeovimは読みにいってくれないので、init.vimに読み込む設定を書きます。ついでなので、dein.tomlがなかったらインストールする設定も書いてしまいます。

" init.vim
" プラグインが実際にインストールされるディレクトリ
let s:dein_dir = expand('~/.cache/dein')
" dein.vim 本体
let s:dein_repo_dir = s:dein_dir . '/repos/github.com/Shougo/dein.vim'

" dein.vim がなければ github から落としてくる
if &runtimepath !~# '/dein.vim'
  if !isdirectory(s:dein_repo_dir)
    execute '!git clone https://github.com/Shougo/dein.vim' s:dein_repo_dir
  endif
  execute 'set runtimepath^=' . fnamemodify(s:dein_repo_dir, ':p')
endif

" 設定開始
if dein#load_state(s:dein_dir)
  call dein#begin(s:dein_dir)
  call dein#add('Shougo/dein.vim')
  call dein#add('Shougo/vimproc.vim', {'build': 'make'})
  call dein#add('Shougo/neocomplete.vim')
  call dein#add('Shougo/neomru.vim')
  call dein#add('Shougo/neosnippet-snippets')
  call dein#add('LeafCage/yankround.vim')
  call dein#add('tomasr/molokai')
  " プラグインリストを収めた TOML ファイル
  " 予め TOML ファイル(後述)を用意しておく
  let g:rc_dir    = expand('~/.config/nvim/dein')
  let s:toml      = g:rc_dir .'/dein.toml'
  "let s:lazy_toml = g:rc_dir . '/dein_lazy.toml'

  " TOML を読み込み、キャッシュしておく
  call dein#load_toml(s:toml,      {'lazy': 0})
  "call dein#load_toml(s:lazy_toml, {'lazy': 1})

  " 設定終了
  call dein#end()
  call dein#save_state()
endif

if dein#check_install()
  call dein#install()
endif

保存して、新たにnvimすれば自動的にdein.tomlとその他プラグインがインストールされます。


参考サイト
Installing Neovim · neovim/neovim Wiki · GitHub
GitHub - Shougo/dein.vim: Dark powered Vim/Neovim plugin manager
NeoBundle から dein.vim に乗り換えたら爆速だった話 - Qiita