darudaru

だるだるしてるエンジニア

vimrcを分ける

暇だったのでvimrcを分割してキレイにしました。

やったこと

  • setコマンドの記述をvimrcから切り離す
  • TOMLでプラグインを管理する

setコマンドの記述をvimrcから切り離す

setコマンドの設定を別ファイルに記述し、vimrcでその設定ファイルを読み込みするようにします。
ファイルの読み込みは、sourceっぽいことができるruntimeを使います。

別ファイルにsetコマンドの設定をかく

userautoloadを作って設定ファイル置いてる人が多いっぽいので、そうすることにした。 設定は細かく分けてはないです。

  • 初期設定(文字コードとか)
    • ~/.vim/userautoload/init.vim
  • その他の雑多な設定
    • ~/.vim/userautoload/options.vim

runtime!する

.vimrcに追加する。

runtime! userautoload/*.vim

TOMLでプラグインを管理する

TOMLを使ってプラグインの読み込みを遅延させることで、vimの起動を早くすることもできるらしい。
Shougoさんの設定を参考にさせていただきました。

shougo-s-github/deinlazy.toml at master · Shougo/shougo-s-github · GitHub

TOMLファイルにプラグインの設定をかく

  • vim起動時に読み込むプラグイン
    • ~/.vim/dein/plugins.toml
  • あとから読み込むプラグイン
    • ~/.vim/dein/plugins-lazy.toml
plugins.toml
[[plugins]]
repo = 'Shougo/dein.vim'

[[plugins]]
repo = 'itchyny/lightline.vim'
hook_add = '''
    set laststatus=2
    set t_Co=256
    let g:lightline = {
          \ 'colorscheme': 'solarized',
          \ 'active': {
          \   'left': [ [ 'mode', 'paste' ],
          \             [ 'fugitive', 'readonly', 'filename', 'modified' ] ]
          \ },
          \ 'component': {
          \   'readonly': '%{&filetype=="help"?"":&readonly?"✖︎":""}',
          \   'modified': '%{&filetype=="help"?"":&modified?"+":&modifiable?"":"-"}',
          \   'fugitive': '%{exists("*fugitive#head")?fugitive#head():""}'
          \ },
          \ 'component_visible_condition': {
          \   'readonly': '(&filetype!="help"&& &readonly)',
          \   'modified': '(&filetype!="help"&&(&modified||!&modifiable))',
          \   'fugitive': '(exists("*fugitive#head") && ""!=fugitive#head())'
          \ }
          \ }
'''

[[plugins]]
repo = 'Yggdroot/indentLine'

[[plugins]]
repo = 'airblade/vim-gitgutter'
plugins-lazy.toml
[[plugins]]
repo = 'scrooloose/nerdtree'
# vim起動時に読み込む
hook_add = '''
    nnoremap [nerdtree]    <Nop>
    nmap     <Space>n [nerdtree]
    nnoremap <silent>[nerdtree]n :NERDTreeToggle<CR>
    nnoremap <silent>[nerdtree]f :NERDTreeFind<CR>
'''

[[plugins]]
repo = 'Shougo/denite.nvim'
# Deniteのコマンドが実行されたら読み込む
on_cmd = ['Denite']
hook_add = '''
    let g:python3_host_prog = expand('/usr/local/bin/python')
    let g:unite_enable_start_insert=0

    nmap <Space>d [denite]
    nnoremap <silent> [denite]h :<C-u>Denite file_mru<CR>
    nnoremap <silent> [denite]g  :<C-u>Denite grep -buffer-name=search-buffer-denite<CR>
    nnoremap <silent> [denite]r :<C-u>Denite -resume -buffer-name=search-buffer-denite<CR>
    nnoremap <silent> [denite]f :<C-u>Denite file_rec<CR>
    nnoremap <silent> [denite]l :<C-u>Denite line<CR>
'''
hook_post_source = '''
call denite#custom#var('file_rec', 'command', ['ag', '--follow', '--nocolor', '--nogroup', '-g', ''])
call denite#custom#var('grep', 'command', ['ag'])
call denite#custom#var('grep', 'recursive_opts', [])
call denite#custom#var('grep', 'pattern_opt', [])
call denite#custom#var('grep', 'default_opts', ['--follow', '--no-group', '--no-color'])
'''

[[plugins]]
repo ='thinca/vim-quickrun'
hook_add = '''
    nnoremap <silent><Space>r :QuickRun<CR>
    nnoremap <expr><silent> <C-c> quickrun#is_running() ? quickrun#sweep_sessions() : "\<C-c>"
'''
hook_source = '''
au FileType qf nnoremap <silent><buffer>q :quit<CR>
let g:quickrun_config = {
    \'_' : {
    \   'outputter/buffer/split' : ':botright 8sp',
    \   'outputter/error/success' : 'buffer',
    \   'outputter/error/error'   : 'quickfix',
    \   'outputter/buffer/into' : '1',
    \   'outputter/quickfix/errorformat' : '%f:%l,%m in %f on line %l',
    \   'outputter/buffer/close_on_empty' : 1,
    \   'outputter' : 'error',
    \},
    \}
'''

[[plugins]]
repo = 'tpope/vim-fugitive'

[[plugins]]
repo = 'Shougo/neosnippet.vim'
# 文字が入力されたら
on_event = 'InsertCharPre'

[[plugins]]
repo = 'Shougo/neosnippet-snippets'
# 文字が入力されたら
on_event = 'InsertCharPre'
# ファイル形式は*.snippet
on_ft = 'snippet'
hook_add = '''
    let g:neosnippet#enable_snipmate_compatibility = 1
    let g:neosnippet#snippets_directory='~/.vim/dein/vim-snippets/snippets'
'''

[[plugins]]
repo = 'Shougo/neocomplete'
# luaがあれば
if = "has('lua')"
# 挿入モードが開始されたら
on_event = 'InsertEnter'
hook_add = '''
    let g:neocomplete#enable_at_startup = 1
'''

lazyの場合、いろいろオプションが設定できますが、そんなに細かいオプションは使ってません。だいたい.vimrcに記載していた内容を持ってきただけ。

設定できるオプションはdeinのドキュメントを見るとよいです。
dein.vim/dein.txt at master · Shougo/dein.vim · GitHub

hook_addとhook_sourceの使い分けがよくわかりませんでした。
hook_sourceに追加してみて、うまく動かなければhook_addに、という地道な方法で追加していきました。Shougoさんの設定通りだとうまく動かなかったり・・・何が違うんだろうか。

キレイにしたvimrc

runtime! userautoload/*.vim

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

if dein#load_state('~/.vim/dein')
    call dein#begin('~/.vim/dein')
    call dein#load_toml('~/.vim/dein/plugins.toml', {'lazy': 0})
    call dein#load_toml('~/.vim/dein/plugins-lazy.toml', {'lazy': 1})

    call dein#end()
    call dein#save_state()
endif

filetype plugin indent on
syntax enable

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

感想

  • vimの起動速くなった。
  • vimrcにどのプラグインの設定かをコメントで書いていたが、TOMLファイルにすることでプラグインごとの設定が断然みやすくなった。
  • 初っ端insertモードに入るとき、すごいもっさりする。でも前からな気がする。

使用しているプラグインは前にまとめたので、よければ参考までに。
blog.darudaru-life.com