NeoVim + dein の導入

はじめに

Mac にドキュメントエディタとして neovim を、neovim のプラグインマネージャとして dein をインストールします。

環境

$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.14.6
BuildVersion:   18G4032

1. neovim のインストール

homebrew で一発インストール

$ brew install nvim
$ nvim -version
NVIM v0.4.3
Build type: Release
LuaJIT 2.0.5
Compilation: /usr/local/Homebrew/Library/Homebrew/shims/mac/super/clang -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 -DNDEBUG -DMIN_LOG_LEVEL=3 -Wall -Wextra -pedantic -Wno-unused-parameter -Wstrict-prototypes -std=gnu99 -Wshadow -Wconversion -Wmissing-prototypes -Wimplicit-fallthrough -Wvla -fstack-protector-strong -fdiagnostics-color=auto -DINCLUDE_GENERATED_DECLARATIONS -D_GNU_SOURCE -DNVIM_MSGPACK_HAS_FLOAT32 -DNVIM_UNIBI_HAS_VAR_FROM -I/tmp/neovim-20191107-13403-1or2rj3/neovim-0.4.3/build/config -I/tmp/neovim-20191107-13403-1or2rj3/neovim-0.4.3/src -I/usr/local/include -I/tmp/neovim-20191107-13403-1or2rj3/neovim-0.4.3/deps-build/include -I/usr/local/opt/gettext/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include -I/tmp/neovim-20191107-13403-1or2rj3/neovim-0.4.3/build/src/nvim/auto -I/tmp/neovim-20191107-13403-1or2rj3/neovim-0.4.3/build/include
Compiled by brew@Mojave.local

Features: +acl +iconv +tui
See ":help feature-compile"

      システム vimrc: "$VIM/sysinit.vim"
       省略時の $VIM: "/usr/local/Cellar/neovim/0.4.3/share/nvim"

Run :checkhealth for more info

2. neovim の設定

  • ~/.vimrc ではなく、~/.config/nvim/init.vim に設定を書きます。
  1. Configuration nvim-configuration

  2. Use $XDG_CONFIG_HOME/nvim/init.vim instead of .vimrc for configuration.

  3. Use $XDG_CONFIG_HOME/nvim instead of .vim to store configuration files.
  4. Use $XDG_DATA_HOME/nvim/shada/main.shada instead of .viminfo for persistent session information. |shada|

XDG_CONFIG_HOME

  • ユーザー個別の設定が書き込まれるディレクトリ (/etc と類似)。
  • デフォルトは $HOME/.config です。
$ mkdir ~/.config/nvim
  • ~/.config/nvim/init.vim の作成
" Configuration
colorscheme desert
filetype plugin indent on
syntax on
syntax enable
set termguicolors
set tabstop=4 softtabstop=4 expandtab shiftwidth=4 smarttab
set list
set listchars=tab:»-,trail:-,eol:,extends:»,precedes:«,nbsp:%
set number
set autoindent
set fenc=utf-8
set noswapfile
set autoread
set hidden
set showcmd
set cursorline
set cursorcolumn
set visualbell
set showmatch
set laststatus=2
set wildmode=list:longest

set ignorecase
set smartcase
set incsearch
set wrapscan

"" FileType
autocmd FileType python setl autoindent
autocmd FileType python setl smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
autocmd FileType python setl tabstop=8 expandtab shiftwidth=4 softtabstop=4
autocmd FileType r setl tabstop=4 expandtab shiftwidth=2 softtabstop=2
autocmd BufNewFile,BufRead *.gvy setfiletype groovy
autocmd BufNewFile,BufRead *.ts setfiletype typescript
autocmd BufNewFile,BufRead *.tsx,*.jsx setfiletype=typescript.tsx

"" Map Leader
let mapleader = "\<Space>"
let maplocalleader = "\<Space>"

"" Custom Key Mapping
nnoremap ; :
nnoremap : ;
inoremap <silent> jj <ESC>
nnoremap <Leader>o :CtrlP<CR>
nnoremap <Leader>w :w<CR>
vmap <Leader>y "+y
vmap <Leader>d "+d
nmap <Leader>p "+p
nmap <Leader>P "+P
vmap <Leader>p "+p
vmap <Leader>P "+P
nmap <Leader><Leader> V
map <C-n> ;NERDTreeToggle<CR>
nnoremap s <Nop>
nnoremap sj <C-w>j
nnoremap sk <C-w>k
nnoremap sl <C-w>l
nnoremap sh <C-w>h
nnoremap sJ <C-w>J
nnoremap sK <C-w>K
nnoremap sL <C-w>L
nnoremap sH <C-w>H
nnoremap sn gt
nnoremap sp gT
nnoremap sr <C-w>r
nnoremap s= <C-w>=
nnoremap sw <C-w>w
nnoremap so <C-w>_<C-w>|
nnoremap sO <C-w>=
nnoremap sN :<C-u>bn<CR>
nnoremap sP :<C-u>bp<CR>
nnoremap st :<C-u>tabnew<CR>
nnoremap sT :<C-u>Unite tab<CR>
nnoremap ss :<C-u>sp<CR>
nnoremap sv :<C-u>vs<CR>
nnoremap sq :<C-u>q<CR>
nnoremap sQ :<C-u>bd<CR>

3. dein.vim のインストール

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

4. dein.vim の設定

  • インストール後に表示される通り、以下の設定を ~/.config/nvim/init.vim に追記します。
"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif

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

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

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

  " 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-------------------------
if dein#load_state('~/.cache/dein')
  call dein#begin('~/.cache/dein')

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

+ call dein#load_toml('~/.config/nvim/dein.toml')
+ call dein#load_toml('~/.config/nvim/dein_lazy.toml', { 'lazy': 1 })

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