code smith

開発で日々の生活をもっと楽しく

neovim入門

f:id:gaku3601:20180923114142j:plain Gakuです。
世間はすっかり秋ですね〜。いい気候です!

世間のいい感じの気候はさておき、僕のプログラミングに対するモチベーションは絶賛だだ下がり中です。
こういうときはEditorを変更するに限る!ってことで、何を血迷ったかVisualStudioCodeを入れてみたのですが、vimに囲われている僕としては使いにくい。(かっこいいんですけどね〜
なので、neovim触ってみたいと思います。

install

HomeBrewが入っている前提です。

brew install neovim
// 起動
nvim

f:id:gaku3601:20180923102817p:plain 入りました(´・ω・`)b
うん。vimと何ら変わらないっすね。

頑張って設定する

neovimは~/.config/nvim/init.vimが設定ファイルになるので作成しておきます。

nvim touch init.vim

プラグインを入れるためにに、プラグインマネージャが必要なので、dein.vimなるものを導入します。 github.com

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

そしたら、先程作成したinit.vimを開きpluginをinstallできるように以下のように記述します。

if &compatible
  set nocompatible
endif
set runtimepath+=~/.cache/dein/repos/github.com/Shougo/dein.vim

call dein#begin(expand("~/.cache/dein"))
"ここにpluginを追加していく
"call dein#add('インストールしたいGitHubリポジトリの作者/リポジトリ名')

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

これでOKです(´・ω・`)b

pluginを追加していく。

NERDTree の導入

手始めにいつもvimで使用しているNERDTreeを入れてみます。
NERDTreeはいい感じのFileTreeを使用できるpluginです。
github.com

if &compatible
  set nocompatible
endif
set runtimepath+=~/.cache/dein/repos/github.com/Shougo/dein.vim

call dein#begin(expand("~/.cache/dein"))
call dein#add('scrooloose/nerdtree') " こんな感じで追加

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

そしたらnvimを一旦閉じて再起動します。
再起動するとpluginのinstallが開始され、NERDTreeを使用できるようになります。
Nomalモードで「:NERDTreeToggle」を叩くとこうなります。 f:id:gaku3601:20180923105520p:plain はい。かっくいい(´・ω・`)b
このままだと、FileTreeを開くのに、毎回「:NERDTreeToggle」を打たないといけなくて、ものすごくめんどくさいです。
なので、Tabを押すとFireTreeが開くように設定します。

if &compatible
  set nocompatible
endif
set runtimepath+=~/.cache/dein/repos/github.com/Shougo/dein.vim

call dein#begin(expand("~/.cache/dein"))
call dein#add('scrooloose/nerdtree')

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

" NERDTreeの設定
map <TAB> :NERDTreeToggle<CR>

これでNomalモード時にTABを押せばFileTreeが表示されるようになります。

その他設定

行数表示したりinsertモード時のTABの挙動を半角スペース2つにしたり、保存してないバッファがあっても移動できるようにしたり、方向キーは使えないようにしたりしたのが以下の設定ファイル。

if &compatible
  set nocompatible
endif
set runtimepath+=~/.cache/dein/repos/github.com/Shougo/dein.vim

call dein#begin(expand("~/.cache/dein"))
call dein#add('scrooloose/nerdtree')

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

" NERDTreeの設定
map <TAB> :NERDTreeToggle<CR>

" etc setting
" 行数の表示
set number
" TABキーを押した際にタブ文字の代わりにスペースを入れる
set expandtab
set tabstop=2
set shiftwidth=2
" 保存なしでバッファファイルの移動を可能とする
set hidden

" 方向キーは使用しない設定(初心者用)
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>
inoremap <Up> <Nop>
inoremap <Down> <Nop>
inoremap <Left> <Nop>
inoremap <Right> <Nop>

で、こんな感じになりました。 f:id:gaku3601:20180923111804p:plain 最小構成だけど、まぁ、一応使えるvimになるはず(´・ω・`)b

おわりに

まだ全然neovimを使ってないのでわかりませんが、いつものvimと同じような感じで触っていける気がします。
今までサクラエディタ→VSStudio→SublimeAtomVimVSCode(あかん使えへん。。。)→VimVSCode(やっぱあかん。。。)→NeoVimみたいなエディタ経歴のある僕ですが、

Vim使ったら他のGUIエディタ使えへん。。。

体になってしまうので、Vimに入門する人はそれなりの心構えを持って入門した方がいいですよ!と忠告しておきます。(´・ω・`)
Vimは慣れるまで辛いけど、慣れたら素晴らしいエディタです。
Vimやりたい人はこの本おすすめです。

B00HWLJI3U実践Vim 思考のスピードで編集しよう! (アスキー書籍)

便利な基本コマンド盛りだくさんなので、Vimと友達になれるはず。

参考文献

qiita.com