もた日記

くだらないことを真面目にやる

Vimメモ : vim-expand-regionでビジュアルモードの選択領域を拡大/縮小

vim-expand-region


github.com

このプラグインを使えば下図のようにビジュアルモードの選択領域を簡単なキー操作で拡大/縮小することができる。

f:id:wonder-wall:20160331220052g:plain

インストール


NeoBundleの場合は下記行をvimrcに追加。

NeoBundle 'terryma/vim-expand-region'


使い方



デフォルトでは+キーを押せば選択領域の拡大、_キーを押せば選択領域の縮小ができる。
キーマッピングを変更したい場合は下記設定をvimrcに追加。

map K <Plug>(expand_region_expand)
map J <Plug>(expand_region_shrink)


設定変更


デフォルトの設定は以下のようになっているが、g:expand_region_text_objectsを編集すれば選択領域の対象とするテキストオブジェクトを追加/削除することができる。

" デフォルト設定(vimrc記述時は下記コメント部分を削除すること)
let g:expand_region_text_objects = {
      \ 'iw'  :0,
      \ 'iW'  :0,
      \ 'i"'  :0,
      \ 'i''' :0,
      \ 'i]'  :1, " Support nesting of square brackets
      \ 'ib'  :1, " Support nesting of parentheses
      \ 'iB'  :1, " Support nesting of braces
      \ 'il'  :0, " 'inside line'. Available through https://github.com/kana/vim-textobj-line
      \ 'ip'  :0,
      \ 'ie'  :0, " 'entire file'. Available through https://github.com/kana/vim-textobj-entire
      \ }

ここで数値の1はネストされたものを対象とするかどうかを表す。例えば、'ib' :1ならネストされた括弧一つずつに対して拡大/縮小していくが、'ib' :0なら一番外側の括弧を対象とする。
また、ilの行選択とieのファイル全体選択は別プラグインのインストールが必要なので、使いたい場合は下記行をvimrcに追加(NeoBundleの場合)。

NeoBundle 'kana/vim-textobj-user'
NeoBundle 'kana/vim-textobj-line'
NeoBundle 'kana/vim-textobj-entire'



その他、特定のファイルタイプに対して指定したい場合は、g:expand_region_text_objects_{ft}のように指定する。以下はRubyの場合の例。

" Rubyの場合の設定例(vimrc記述時は下記コメント部分を削除すること)
let g:expand_region_text_objects_ruby = {
      \ 'im' :0, " 'inner method'. Available through https://github.com/vim-ruby/vim-ruby
      \ 'am' :0, " 'around method'. Available through https://github.com/vim-ruby/vim-ruby
      \ }