5
10

More than 5 years have passed since last update.

新年度から使いたい Vim プラグイン:operator プラグイン編

Last updated at Posted at 2017-06-01

ブログ記事からの転載です。

前回

はじめに : Vim プラグインを管理するプラグイン

プラグインを導入する前にプラグインを管理するプラグインを入れましょう。

使い方などはドキュメントやここら辺を参考にするとよいでしょう。
また、他のプラグイン管理プラグインには以下のようなものもあります。

neobundle.vim があわないと感じたらこの2つも試してみるとよいと思います。

特に vim-pathogen は単純な管理システムで git に依存しないので git を使いたくない方は vim-pathogen を使うとよいでしょう。




あと今回は紹介するプラグインはプラグインに依存しているので、予め導入しておくとよいでしょう。

NeoBundle "kana/vim-operator-user"

kana/vim-operator-replace

NeoBundle "kana/vim-operator-replace"

operator-replace は textobj のテキストにヤンクレジスタのテキストをはりつける operator です。

かなり有名な operator 。

ビジュアルモードからテキストをペーストするのとは違い、ヤンクレジスタを汚染しないのが特徴です。

ヤンクレジスタを汚染しないので dot repeat 等で連続して同じテキストをペーストしたい場合に有効です。

設定例

nmap s <Plug>(operator-replace)

rhysd/vim-operator-surround

NeoBundle "rhysd/vim-operator-surround"

operator-surround は対象の textobj を括弧で囲むための operator です。

surround.vim の operator 版のようなプラグインです。

" 括弧を追加する
map <silent> sa <Plug>(operator-surround-append)
" 括弧を削除する
map <silent> sd <Plug>(operator-surround-delete)
" 括弧を入れ替える
map <silent> sr <Plug>(operator-surround-replace)

例えば、saiw(sa + iw + ()と入力することでカーソル下の単語を () で囲う事ができます。

また、この operator は前回紹介した textobj-multiblock と組み合わせることでより強力になります。

" カーソル位置から一番近い括弧を削除する
nmap <silent> sdd <Plug>(operator-surround-delete)<Plug>(textobj-multiblock-a)

" カーソル位置から一番近い括弧を変更する
nmap <silent> srr <Plug>(operator-surround-replace)<Plug>(textobj-multiblock-a)

もうちょっと便利な設定

上記の使い方でも十分便利なのですが、branch/input_in_advance を使うことでもっと便利になります。

NeoBundle "rhysd/vim-operator-surround", {
\   "rev" : "input_in_advance"
\}

これで <Plug>(operator-surround-append-input-in-advance) が使えるようになります。

operator-surround は通常

  • operator の入力→ textobj の入力→括弧の入力

という順番で処理されます。

<Plug>(operator-surround-append-input-in-advance) を使用する場合、この順番がちょっと異なり。

  • operator の入力→括弧の入力→ textobj の入力

と、いう風に括弧を入力するタイミングを先にすることができます。

これにより何が便利なのかというと例えば、以下のように設定すると

nmap sa <Plug>(operator-surround-append-input-in-advance)
nmap s( <Plug>(operator-surround-append-input-in-advance)(
nmap sb <Plug>(operator-surround-append-input-in-advance)(
nmap s{ <Plug>(operator-surround-append-input-in-advance){
nmap s[ <Plug>(operator-surround-append-input-in-advance)[
nmap s" <Plug>(operator-surround-append-input-in-advance)"
nmap s' <Plug>(operator-surround-append-input-in-advance)'

s + 任意の括弧 + testobj みたいな入力を行う事ができるようになります。

osyo-manga/vim-operator-stay-cursor

NeoBundle "osyo-manga/vim-operator-stay-cursor"

operator を実行するとカーソル位置が対象の textobj の先頭へと移動します。

operator-stay-cursor は任意の operator を実行した場合にカーソルを移動させないようにするための operator になります。

例えば、ヤンク operator 時にカーソルを移動しないようにする場合は

map y <Plug>(operator-stay-cursor-yank)

と、することで y 時にカーソルが移動しなくなります。

地味なんですが結構便利です。

関連

oyos-manga/vim-operator-exec_command

NeoBundle "oyos-manga/vim-operator-exec_command"

operator-exec_command は任意のコマンドを実行する operator です。

例えば、以下のように設定すると textobj のテキストを help する事ができるようになります。

" %t が textobj の範囲のテキストに置き換わる
nmap <expr> K operator#exec_command#mapexpr(":help %t")

他には

nmap <expr> <Space>* operator#exec_command#mapexpr("Unite line -input=%t")

と、することで絞りこみをした状態で unite-line を起動したりすることもできます。

単純な機能で結構応用が効くので便利です。

関連

まとめ

と、いう感じで日頃便利に使っている operator 系プラグインをいくつか紹介してみました。

textobj と同様に operator も Vim 独自の機能のひとつなのでぜひ使いこなせるようになるとよいと思います。

参照

5
10
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
5
10