LoginSignup
168
141

More than 5 years have passed since last update.

Vim を使い DarkPowerを得る

Last updated at Posted at 2017-12-09

力がほしい

Vimを愛用し初めて1年半、そろそろが欲しいと思っていたところ。

Dein.vim

Dein.vim is a dark powered Vim/Neovim plugin manager.

良いものを発見。 まさにdark powerを得るチャンス。

※イメージ →

haya14busa さんのスライドより
https://docs.google.com/presentation/d/14pViuMI_X_PiNwQD8nuGRG72GUqSeKDqoJqjAZWS39U/edit#slide=id.g28fa7f227c_0_221

NeoBundleからDeinに乗り換える。
ついでに、Plugin整理やら追加やらをし、細かく解説していきます。

Deinを使う上での構成やら基本的なことはここを参照。

構成(~/以下)

  • .vimrc
  • .vim
    • rc
      • dein.toml
      • dein_lazy.toml

簡単な設定

白紙の.vimrcにこれを書いていきます。

set encoding=utf-8
scriptencoding utf-8
" 保存時の文字コード
set fileencoding=utf-8
" 読み込み時の文字コードの自動判別. 左側が優先される
set fileencodings=ucs-boms,utf-8,euc-jp,cp932
" 改行コードの自動判別. 左側が優先される
set fileformats=unix,dos,mac
" □や○文字が崩れる問題を解決"
set ambiwidth=double
"行数を表示する
set number
set backspace=indent,eol,start
"ヤンクした時にクリップボードにコピーする
set clipboard=unnamed,autoselect

" タブ入力を複数の空白入力に置き換える
set expandtab
" 画面上でタブ文字が占める幅
set tabstop=4
" 連続した空白に対してタブキーやバックスペースキーでカーソルが動く幅
set softtabstop=4
" 改行時に前の行のインデントを継続する
set autoindent
" 改行時に前の行の構文をチェックし次の行のインデントを増減する
set smartindent
" smartindentで増減する幅"
set shiftwidth=4

" インクリメンタルサーチ. 1文字入力毎に検索を行う
set incsearch
" 検索パターンに大文字小文字を区別しない
set ignorecase
" 検索パターンに大文字を含んでいたら大文字小文字を区別する
set smartcase
" 検索結果をハイライト"
set hlsearch

" 不可視文字を可視化(タブが「▸-」と表示される)
set list listchars=tab:\▸\-

" カーソルの左右移動で行末から次の行の行頭への移動が可能になる
set whichwrap=b,s,h,l,<,>,[,],~

" カーソルラインをハイライト"
set cursorline

Plugins

初期設定

:.vimrc
" dein.vim がなければ github から落としてくる
if &runtimepath !~# '/dein.vim'
  if !isdirectory(s:dein_repo_dir)
    execute '!git clone https://github.com/Shougo/dein.vim' s:dein_repo_dir
  endif
  execute 'set runtimepath^=' . fnamemodify(s:dein_repo_dir, ':p')
endif

" 設定開始
if dein#load_state(s:dein_dir)
  call dein#begin(s:dein_dir)

  " プラグインリストを収めた TOML ファイル
  " 予め TOML ファイル(後述)を用意しておく
  let g:rc_dir    = expand('~/.vim/rc')
  let s:toml      = g:rc_dir . '/dein.toml'
  let s:lazy_toml = g:rc_dir . '/dein_lazy.toml'

  " TOML を読み込み、キャッシュしておく
  call dein#load_toml(s:toml,      {'lazy': 0})
  call dein#load_toml(s:lazy_toml, {'lazy': 1})

  " 設定終了
  call dein#end()
  call dein#save_state()
endif

" もし、未インストールものものがあったらインストール
if dein#check_install()
  call dein#install()
endif

:.dein.toml
# 基本は github.com のレポジトリーを指定するだけ
[[plugins]]
repo = 'Shougo/dein.vim'

# git clone 後、実行すべきコマンドがある場合はこんな感じ
[[plugins]]
repo = 'Shougo/vimproc.vim'
hook_post_update = '''
  if dein#util#_is_windows()
    let cmd = 'tools\\update-dll-mingw'
  elseif dein#util#_is_cygwin()
    let cmd = 'make -f make_cygwin.mak'
  elseif executable('gmake')
    let cmd = 'gmake'
  else
    let cmd = 'make'
  endif
  let g:dein#plugin.build = cmd
'''

これで準備完了!

vim-airline/vim-airline

:dein.toml
[[plugins]]
repo    = 'vim-airline/vim-airline'
depends = ['vim-airline-themes']

[[plugins]]
repo = 'vim-airline/vim-airline-themes'

:.vimrc
let g:airline_theme = 'molokai'

ステータスラインのテーマを変更する。

GitHub

tomasr/molokai

:dein.toml
[[plugins]]
repo = 'tomasr/molokai'

コードの色をいい感じにしてくれる。
GitHub

スクリーンショット 2017-12-07 17.23.09.png

Shougo/vimfiler

:dein.toml
#vimfilerを使うために必要
[[plugins]]
repo =  'Shougo/unite.vim'

#ディレクトリ表示
[[plugins]]
repo = 'Shougo/vimfiler'

ディレクトリを表示し、移動が出来る。

:.vimrc
:let g:vimfiler_as_default_explorer = 1

をしておけばVim起動時に毎回起動する。

Townk/vim-autoclose

:dein.toml
[[plugins]]
repo = 'Townk/vim-autoclose'

括弧補完
" を打つと ""に自動補完してくれる。
https://github.com/Townk/vim-autoclose

scrooloose/syntastic

:dein.toml
[[plugins]]
repo =  'scrooloose/syntastic'

syntax のチェックをしてくれる。
エラーの箇所に >> が入るためわかりやすい。

Yggdroot/indentLine

:dein.toml
[[plugins]]
repo = 'Yggdroot/indentLine'
:.vimrc
let g:indentLine_char = '¦'

インデントを可視化する

3__vim.png

bronson/vim-trailing-whitespace

:dein.toml
[[plugins]]
repo = 'bronson/vim-trailing-whitespace'

末尾の全角と半角の空白文字を赤くハイライト
:FixWhitespace で全て削除できる。
vimwhite3.gif

tpope/vim-fugitive airblade/vim-gitgutter

:dein.toml
[[plugins]]
repo = 'tpope/vim-fugitive'
[[plugins]]
repo = 'airblade/vim-gitgutter'

git関連のプラグイン
gitの便利操作がvimで出来る
また、変更箇所に ~
追加箇所に +
削除箇所に -
と表示される。
お勧めは Gblame
誰のコミットかわかる。
vimgit.gif

vimgit2.gif

https://github.com/tpope/vim-fugitive
https://github.com/airblade/vim-gitgutter

junegunn/vim-easy-align

:dein.toml

[[plugins]]
repo = 'junegunn/vim-easy-align'
:.vimrc
"enterで整形設定に行くようにする
vmap <Enter> <Plug>(EasyAlign)

細かい整形が出来るようなる
vimeasyalign2.gif

整形箇所を選択→enter→ = → enter で整形される。
※ = 以外でも可能

ctrlpvim/ctrlp.vim

:dein.toml
[[plugins]]
repo = 'ctrlpvim/ctrlp.vim'

ctrl+p でファイルあいまい検索ができる

https://github.com/ctrlpvim/ctrlp.vim
vimctrlp2.gif

vimctrlp3.gif

ctrl + tで新しいタブにてファイルを開ける。

Shougo/neocomplcache,neosnippet,neosnippet-snippets

:dein.toml
#入力補完
[[plugins]]
repo = 'Shougo/neocomplcache'

# スニペットの補完機能
[[plugins]]
repo = 'Shougo/neosnippet'

# スニペット集
[[plugins]]
repo = 'Shougo/neosnippet-snippets'

.vimrc
"====================neocomplcache====================
" ~Disable AutoComplPop. neocomplcashe~
let g:acp_enableAtStartup = 0
" Use neocomplcache.
let g:neocomplcache_enable_at_startup = 1
" Use smartcase.
let g:neocomplcache_enable_smart_case = 1
" Set minimum syntax keyword length.
let g:neocomplcache_min_syntax_length = 3
let g:neocomplcache_lock_buffer_name_pattern = '\*ku\*'

" Define dictionary.
let g:neocomplcache_dictionary_filetype_lists = {
    \ 'default' : ''
    \ }

" Plugin key-mappings.
inoremap <expr><C-g>     neocomplcache#undo_completion()
inoremap <expr><C-l>     neocomplcache#complete_common_string()

" Recommended key-mappings.
" <CR>: close popup and save indent.
inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR>
function! s:my_cr_function()
  return neocomplcache#smart_close_popup() . "\<CR>"
endfunction
" <TAB>: completion.
inoremap <expr><TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"
" <C-h>, <BS>: close popup and delete backword char.
inoremap <expr><C-h> neocomplcache#smart_close_popup()."\<C-h>"
inoremap <expr><BS> neocomplcache#smart_close_popup()."\<C-h>"
inoremap <expr><C-y>  neocomplcache#close_popup()
inoremap <expr><C-e>  neocomplcache#cancel_popup()
"~neocomplecas~


"====================neosnippet====================
" Plugin key-mappings.
" Note: It must be "imap" and "smap".  It uses <Plug> mappings.
imap <C-k>     <Plug>(neosnippet_expand_or_jump)
smap <C-k>     <Plug>(neosnippet_expand_or_jump)
xmap <C-k>     <Plug>(neosnippet_expand_target)

" SuperTab like snippets behavior.
" Note: It must be "imap" and "smap".  It uses <Plug> mappings.
imap <C-k>     <Plug>(neosnippet_expand_or_jump)
"imap <expr><TAB>
" \ pumvisible() ? "\<C-n>" :
" \ neosnippet#expandable_or_jumpable() ?
" \    "\<Plug>(neosnippet_expand_or_jump)" : "\<TAB>"
smap <expr><TAB> neosnippet#expandable_or_jumpable() ?
\ "\<Plug>(neosnippet_expand_or_jump)" : "\<TAB>"

" For conceal markers.
if has('conceal')
  set conceallevel=2 concealcursor=niv
endif

" ~ファイルタイプ毎 & gitリポジトリ毎にtagsの読み込みpathを変える~
function! ReadTags(type)
    try
        execute "set tags=".$HOME."/dotfiles/tags_files/".
              \ system("cd " . expand('%:p:h') . "; basename `git rev-parse --show-toplevel` | tr -d '\n'").
              \ "/" . a:type . "_tags"
    catch
        execute "set tags=./tags/" . a:type . "_tags;"
    endtry
endfunction

augroup TagsAutoCmd
    autocmd!
    autocmd BufEnter * :call ReadTags(&filetype)
augroup END

vimneo.gif

ctrl + k で次の入力するところに飛べる(便利だ・・・)

https://github.com/Shougo/neocomplcache.vim
https://github.com/Shougo/neosnippet.vim
https://github.com/Shougo/neosnippet-snippets

cohama/lexima.vim

dein.toml
[[plugins]]
repo = 'cohama/lexima.vim'

閉じ括弧補完

https://github.com/cohama/lexima.vim

osyo-manga/vim-anzu

:dein.toml
[[plugins]]
repo = 'osyo-manga/vim-anzu'
:.vimrc
nmap n <Plug>(anzu-n-with-echo)
nmap N <Plug>(anzu-N-with-echo)
nmap * <Plug>(anzu-star-with-echo)
nmap # <Plug>(anzu-sharp-with-echo)
" clear status
nmap <Esc><Esc> <Plug>(anzu-clear-search-status)
" statusline
set statusline=%{anzu#search_status()}

検索結果の件数を表示

rhysd/clever-f.vim

:dein.toml
[[plugins]]
repo = 'rhysd/clever-f.vim'

fで検索後移動できるようにする
ハイライトされるのがホント便利

vim-operator-flashy

:dein.toml
[[plugins]]
repo = 'kana/vim-operator-user'

[[plugins]]
repo = 'haya14busa/vim-operator-flashy'

:.vimrc
map y <Plug>(operator-flashy)
nmap Y <Plug>(operator-flashy)$

コピーしたところが一瞬ハイライトされる

mattn/emmet-vim

[[plugins]]
repo = 'mattn/emmet-vim'
:.vimrc
let g:user_emmet_leader_key='<C-t>'


vimhtml.gif

html補完

ctrl + t , で展開されるように設定

majutsushi/tagbar

[[plugins]]
repo = 'majutsushi/tagbar'

vimtagbar.gif

tagbarがでて関数やclass一覧などがみえる。

まとめ

dein.toml

# 基本は github.com のレポジトリーを指定するだけ
[[plugins]]
repo = 'Shougo/dein.vim'

# git clone 後、実行すべきコマンドがある場合はこんな感じ
[[plugins]]
repo = 'Shougo/vimproc.vim'
hook_post_update = '''
  if dein#util#_is_windows()
    let cmd = 'tools\\update-dll-mingw'
  elseif dein#util#_is_cygwin()
    let cmd = 'make -f make_cygwin.mak'
  elseif executable('gmake')
    let cmd = 'gmake'
  else
    let cmd = 'make'
  endif
  let g:dein#plugin.build = cmd
'''
#ステータスラインのテーマ
[[plugins]]
repo    = 'vim-airline/vim-airline'
depends = ['vim-airline-themes']

#ステータスラインのテーマ
[[plugins]]
repo = 'vim-airline/vim-airline-themes'

#コードの色
[[plugins]]
repo = 'tomasr/molokai'

#vimfilerを使うために必要
[[plugins]]
repo =  'Shougo/unite.vim'

#ディレクトリ表示
[[plugins]]
repo = 'Shougo/vimfiler'

#ディレクトリ表示
[[plugins]]
repo = 'scrooloose/nerdtree'

#括弧補完
[[plugins]]
repo = 'Townk/vim-autoclose'

#syntax check
[[plugins]]
repo =  'scrooloose/syntastic'

# インデントの可視化 上手く機能してない?
[[plugins]]
repo = 'Yggdroot/indentLine'

# インデントの可視化
[[plugins]]
repo = 'nathanaelkane/vim-indent-guides'

# 末尾の全角と半角の空白文字を赤くハイライト
[[plugins]]
repo = 'bronson/vim-trailing-whitespace'

[[plugins]]
repo = 'szw/vim-tags'

#gitの便利操作がvimで出来る Gblameなど
[[plugins]]
repo = 'tpope/vim-fugitive'

#gitの変更箇所を左側に表示してくれる
[[plugins]]
repo = 'airblade/vim-gitgutter'

#tagbarがでる関数一覧などが出る
[[plugins]]
repo = 'majutsushi/tagbar'

#細かい整形が出来るようなる
[[plugins]]
repo = 'junegunn/vim-easy-align'

#ctrl+p でファイルあいまい検索ができる
[[plugins]]
repo = 'ctrlpvim/ctrlp.vim'

#入力補完
[[plugins]]
repo = 'Shougo/neocomplcache'

# スニペットの補完機能
[[plugins]]
repo = 'Shougo/neosnippet'

# スニペット集
[[plugins]]
repo = 'Shougo/neosnippet-snippets'

#閉じ括弧補完
[[plugins]]
repo = 'cohama/lexima.vim'

#検索結果の件数を表示
[[plugins]]
repo = 'osyo-manga/vim-anzu'

#fで検索後移動できるようにする
[[plugins]]
repo = 'rhysd/clever-f.vim'

[[plugins]]
repo = 'kana/vim-operator-user'

#コピーしたところが一瞬ハイライトされる 入らなかった
[[plugins]]
repo = 'haya14busa/vim-operator-flashy'

#html補完
[[plugins]]
repo = 'mattn/emmet-vim'

.vimrc

set encoding=utf-8
scriptencoding utf-8
" 保存時の文字コード
set fileencoding=utf-8
" 読み込み時の文字コードの自動判別. 左側が優先される
set fileencodings=ucs-boms,utf-8,euc-jp,cp932
" 改行コードの自動判別. 左側が優先される
set fileformats=unix,dos,mac
" □や○文字が崩れる問題を解決"
set ambiwidth=double

set number
set backspace=indent,eol,start
"クリップボードにコピーする
set clipboard=unnamed,autoselect

" タブ入力を複数の空白入力に置き換える
set expandtab
" 画面上でタブ文字が占める幅
set tabstop=4
" 連続した空白に対してタブキーやバックスペースキーでカーソルが動く幅
set softtabstop=4
" 改行時に前の行のインデントを継続する
set autoindent
" 改行時に前の行の構文をチェックし次の行のインデントを増減する
set smartindent
" smartindentで増減する幅"
set shiftwidth=4

" インクリメンタルサーチ. 1文字入力毎に検索を行う
set incsearch
" 検索パターンに大文字小文字を区別しない
set ignorecase
" 検索パターンに大文字を含んでいたら大文字小文字を区別する
set smartcase
" 検索結果をハイライト"
set hlsearch

" 不可視文字を可視化(タブが「▸-」と表示される)
set list listchars=tab:\▸\-

" カーソルの左右移動で行末から次の行の行頭への移動が可能になる
set whichwrap=b,s,h,l,<,>,[,],~

" カーソルラインをハイライト"
set cursorline

" 行が折り返し表示されていた場合、行単位ではなく表示行単位でカーソルを移動する
nnoremap j gj
nnoremap k gk
nnoremap <down> gj
nnoremap <up> gk

" tagsジャンプの時に複数ある時は一覧表示
"nnoremap <C-]> g<C-]>
nnoremap <C-h> :vsp<CR> :exe("tjump ".expand('<cword>'))<CR>
nnoremap <C-k> :split<CR> :exe("tjump ".expand('<cword>'))<CR>

" 括弧の対応関係を一瞬表示する
set showmatch
source $VIMRUNTIME/macros/matchit.vim " Vimの「%」を拡張する "

" コマンドモードの補完
set wildmenu
" 保存するコマンド履歴の数  "
set history=5000


"=================dein================
" プラグインが実際にインストールされるディレクトリ
let s:dein_dir = expand('~/.cache/dein')
" dein.vim 本体
let s:dein_repo_dir = s:dein_dir . '/repos/github.com/Shougo/dein.vim'

" dein.vim がなければ github から落としてくる
if &runtimepath !~# '/dein.vim'
  if !isdirectory(s:dein_repo_dir)
    execute '!git clone https://github.com/Shougo/dein.vim' s:dein_repo_dir
  endif
  execute 'set runtimepath^=' . fnamemodify(s:dein_repo_dir, ':p')
endif

" 設定開始
if dein#load_state(s:dein_dir)
  call dein#begin(s:dein_dir)

  " プラグインリストを収めた TOML ファイル
  " 予め TOML ファイル(後述)を用意しておく
  let g:rc_dir    = expand('~/.vim/rc')
  let s:toml      = g:rc_dir . '/dein.toml'
  let s:lazy_toml = g:rc_dir . '/dein_lazy.toml'

  " TOML を読み込み、キャッシュしておく
  call dein#load_toml(s:toml,      {'lazy': 0})
  call dein#load_toml(s:lazy_toml, {'lazy': 1})

  " 設定終了
  call dein#end()
  call dein#save_state()
endif

" もし、未インストールものものがあったらインストール
if dein#check_install()
  call dein#install()
endif


"色を付ける
syntax on
colorscheme molokai



"==============プラグイン関係の設定==============


let g:airline_theme = 'molokai'
"====================gitgutter====================
"変更箇所のハイライト
let g:gitgutter_highlight_lines = 0

"====================junegunn/vim-easy-align====================
"enterで整形設定に行くようにする
vmap <Enter> <Plug>(EasyAlign)

"====================neocomplcache====================
" ~Disable AutoComplPop. neocomplcashe~
let g:acp_enableAtStartup = 0
" Use neocomplcache.
let g:neocomplcache_enable_at_startup = 1
" Use smartcase.
let g:neocomplcache_enable_smart_case = 1
" Set minimum syntax keyword length.
let g:neocomplcache_min_syntax_length = 3
let g:neocomplcache_lock_buffer_name_pattern = '\*ku\*'

" Define dictionary.
let g:neocomplcache_dictionary_filetype_lists = {
    \ 'default' : ''
    \ }

" Plugin key-mappings.
inoremap <expr><C-g>     neocomplcache#undo_completion()
inoremap <expr><C-l>     neocomplcache#complete_common_string()

" Recommended key-mappings.
" <CR>: close popup and save indent.
inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR>
function! s:my_cr_function()
  return neocomplcache#smart_close_popup() . "\<CR>"
endfunction
" <TAB>: completion.
inoremap <expr><TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"
" <C-h>, <BS>: close popup and delete backword char.
inoremap <expr><C-h> neocomplcache#smart_close_popup()."\<C-h>"
inoremap <expr><BS> neocomplcache#smart_close_popup()."\<C-h>"
inoremap <expr><C-y>  neocomplcache#close_popup()
inoremap <expr><C-e>  neocomplcache#cancel_popup()
"~neocomplecas~


"====================neosnippet====================
" Plugin key-mappings.
" Note: It must be "imap" and "smap".  It uses <Plug> mappings.
imap <C-k>     <Plug>(neosnippet_expand_or_jump)
smap <C-k>     <Plug>(neosnippet_expand_or_jump)
xmap <C-k>     <Plug>(neosnippet_expand_target)

" SuperTab like snippets behavior.
" Note: It must be "imap" and "smap".  It uses <Plug> mappings.
imap <C-k>     <Plug>(neosnippet_expand_or_jump)
"imap <expr><TAB>
" \ pumvisible() ? "\<C-n>" :
" \ neosnippet#expandable_or_jumpable() ?
" \    "\<Plug>(neosnippet_expand_or_jump)" : "\<TAB>"
smap <expr><TAB> neosnippet#expandable_or_jumpable() ?
\ "\<Plug>(neosnippet_expand_or_jump)" : "\<TAB>"

" For conceal markers.
if has('conceal')
  set conceallevel=2 concealcursor=niv
endif

" ~ファイルタイプ毎 & gitリポジトリ毎にtagsの読み込みpathを変える~
function! ReadTags(type)
    try
        execute "set tags=".$HOME."/dotfiles/tags_files/".
              \ system("cd " . expand('%:p:h') . "; basename `git rev-parse --show-toplevel` | tr -d '\n'").
              \ "/" . a:type . "_tags"
    catch
        execute "set tags=./tags/" . a:type . "_tags;"
    endtry
endfunction

augroup TagsAutoCmd
    autocmd!
    autocmd BufEnter * :call ReadTags(&filetype)
augroup END


"====================osyo-manga/vim-anzu====================
" mapping
nmap n <Plug>(anzu-n-with-echo)
nmap N <Plug>(anzu-N-with-echo)
nmap * <Plug>(anzu-star-with-echo)
nmap # <Plug>(anzu-sharp-with-echo)
" clear status
nmap <Esc><Esc> <Plug>(anzu-clear-search-status)
" statusline
set statusline=%{anzu#search_status()}



" ====================indentLine====================
let g:indentLine_char = '¦' "use ¦,or"====================haya14busa/vim-operator-flashy====================
map y <Plug>(operator-flashy)
nmap Y <Plug>(operator-flashy)$


"====================EmmetHmtl"====================
let g:user_emmet_leader_key='<C-t>'


これで皆さんも Dark Powerを得れたのではないでしょうか?

168
141
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
168
141