開発したアプリなど一覧

WSL と Windows で Vim の設定を共通化して dein.vim, denite.nvim を利用する

アフィリエイトリンクを含む場合があります

Windows Subsystem for Linux 上でも Windows 上でもエディタには Vim を利用しているが、困るのが vimrc の置き場所と書き方だ。単に .vimrc を共有するだけでなく、 OS 毎に処理を書き分ける必要がある。

なので共通化した際のメモをここに記載しておこう。

シンボリックリンクで設定ファイルを共通化する

同じ設定を二箇所に書くのは二度手間なので WSL 側はシンボリックリンクで Windows 上の .vimrc を読み込むようにする。ついでに .vim ディレクトリもリンクを張る。

~$ ln -s /mnt/c/Users/ryomatsu/.vimrc .vimrc ~$ ln -s /mnt/c/Users/ryomatsu/.vim .vim

dein.vim のインストール

vim のプラグインは dein.vim で管理する。

GitHub - Shougo/dein.vim: Dark powered Vim/Neovim plugin manager

dein.vim を実行するには Git が必要となるので WSL と Windows 両方に Git を入れておく。

Git for Windows

Ubuntu なら sudo apt-get install git を実行すればインストールできる。

Windows 版の Git をインストールする際、文字コードの自動変換を有効にしてしまうと WSL から使う時に面倒なのでオフにしておこう。

Git の準備ができたら dein.vim をインストールしよう。

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

このとき dein.vim を読み込むための Vim Script が画面上に表示されるが、パスを指定している部分があるためそのままでは Windows と Linux で共用できない。なので以下のようにプラットフォーム毎に読み込むパスを変更させる。

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

let s:dein_dir = '/home/ryomatsu/.vim/dein' if has('win32') || has('win64') let s:dein_dir = 'C:\Users\ryomatsu\.vim\dein' endif

if has('unix') set runtimepath+=/home/ryomatsu/.vim/dein/repos/github.com/Shougo/dein.vim endif if has('win32') || has('win64') set runtimepath+=C:\Users\ryomatsu\.vim\dein\repos\github.com\Shougo\dein.vim endif

" Required: if dein#load_state(s:dein_dir) call dein#begin(s:dein_dir)

" Let dein manage dein " Required: "let s:dein_repo_path = s:dein_dir + '/repos/github.com/Shougo/dein.vim' "call dein#add(s:dein_repo_path) call dein#add(s:dein_dir)

" 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/deol.nvim', { 'rev': '01203d4c9' })

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

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

" Required: filetype plugin indent on syntax enable

こんな感じに has('unix') や has('win64') で書き分ける。

編集したら :call dein#install() を実行して正常にプラグインがインストールできれば OK だ。

denite.nvim のインストール

Vim でファイル選択がとても楽になるプラグイン denite.nvim もついでに入れておく。

GitHub - Shougo/denite.nvim: Dark powered asynchronous unite all interfaces for Neovim/Vim8

denite.nvim には Python3 が必要となる。正確にいうと Vim 上で has('python3') が 1 を返さなければならない。

WSL 側は最初から有効になっていたので Windows に Python3 をインストールして使えるようにする。

Python Releases for Windows | Python.org

注意点だが、Windows の Vim のビルドにあったものをインストールする必要がある。自分がインストールした kaoriya 版 Vim 8.1 では現時点での最新版である Python 3.7 では正常に動作せず、Python 3.5 64bit 版をインストールする必要があった。

インストールできたら以下のコマンドで neovim をインストールする。

pip3 install neovim

Python3 がインストールできたら .vimrc の dein.vim の箇所で以下のように denite.nvim を読み込もう。

call dein#add('Shougo/denite.nvim')

この状態で :call dein#install() を実行すると denite.nvim がインストールされるはずだ。

denite.nvim の設定はまだ試行錯誤中だが、とりあえず以下のような感じにしている。

nmap <silent> <C-u><C-f> :<C-u>DeniteBufferDir file<CR> nmap <silent> <C-u><C-m> :<C-u>Denite file_old<CR> nmap <silent> <C-u><C-b> :<C-u>Denite buffer<CR> nmap <silent> <C-u><C-l> :<C-u>Denite line<CR> nmap <silent> <C-u><C-g> :<C-u>DeniteBufferDir grep<CR> nmap <silent> <C-u><C-p> :<C-u>DeniteProjectDir file_rec directory_rec<CR> "nmap <silent> <C-u><C-g> :<C-u>DeniteProjectDir grep<CR>

call denite#custom#map('insert', '<C-n>', '<denite:move_to_next_line>') call denite#custom#map('insert', '<C-p>', '<denite:move_to_previous_line>') call denite#custom#map('insert', '<C-t>', '<denite:do_action:tabopen>') call denite#custom#map('insert', '<C-s>', '<denite:do_action:split>') call denite#custom#map('insert', '<C-v>', '<denite:do_action:vsplit>')

とりあえずファイルの一覧が見られれば良いという感じ。

Sponsored Link

コメント

タイトルとURLをコピーしました