dein.vimを導入

いつまでもフワフワ vim を使っていたため、入れているプラグインも Vaffle というファイラーのみ。

手動で入れたがそれが割とめんどくさくて、使ってみたいと思ったプラグインも導入の時点で諦めてしまっていたのです。

そんな状況を打開したく、何かとよく聞く dein.vim を入れてみよう!と。

dein.vim とは

vimプラグインマネージャです。
vim 8.0 以降と git が使用可能な環境で動作します。

プラグインの導入と管理がかなり楽になるとか?

インストール

インストールスクリプトが用意されているのでそれを走らせるだけです。
このスクリプトの引数にインストール先ディレクトリを指定します。

~/.vim/plugin~/.config/nvim/plugin を指定してはいけないらしいです。

今回は ~/.vim/bundles にインストールします。

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

ちなみにこのスクリプトは、指定されたディレクトリを作成し、そこに dein.vim を clone しているだけでした。

Please add the following settings の文章と共に、 dein.vim の設定スクリプトが出力されます。

~/.vimrc の先頭に出力された設定を追記しましょう。

vimを開いて以下のコマンドを実行し、 dein.vim をインストールします。

:call dein#install()

プラグインの追加

私の vim に入っている Vaffle を一度削除し、deinを使用して入れ直してみます。
Vaffle は手動で入れたので ~/.vim 以下にファイルが散乱して落ち着かなかったのです。

deinの部分を抜粋して私の .vimrc を以下に示します。

"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif

" Required:
set runtimepath+=/home/stay/.vim/bundles/repos/github.com/Shougo/dein.vim

" Required:
if dein#load_state('/home/stay/.vim/bundles')
  call dein#begin('/home/stay/.vim/bundles')

  " Let dein manage dein
  " Required:
  call dein#add('/home/stay/.vim/bundles/repos/github.com/Shougo/dein.vim')

  " Add Vaffle as filer
  call dein#add('cocopon/vaffle.vim')

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

  " 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

これで、 vim を開いてインストールコマンドを実行すれば Vaffle がインストールされるのです。

行終端近くにあるコードのコメントアウトを外すと、インストールされていないプラグインを自動的にインストールしてくれます。

プラグインの削除

削除したいプラグインの dein#add を取り消し、以下のコマンドを順に実行します。

:call map(dein#check_clean(), "delete(v:val, 'rf')")
:call dein#recache_runtimepath()

vim 標準の map 関数は第1引数に指定された配列の要素それぞれに対して、第2引数で指定された処理を実行し、その結果で置換します。

dein#check_clean() 関数で、add されていないが dein 管理下にあるプラグインのルートディレクトリを配列として取得し、それぞれに削除処理を実行しているのです。

後の dein#recache_runtimepath() は削除を dein に反映させています。