Memento memo.

Today I Learned.

Vim再入門 Operator, Motion, TextObjectの理解とドットコマンド

Vimを使う上で "."コマンドが重要です。
"."コマンドは直前の操作の繰り返しを行うコマンドです。

例えばdawコマンドはカーソル下の単語を削除する操作ですが、ここで"."キーを押下すると再び他の単語を削除することができます。

1単語を削除する、等の定形操作は2,3回続けて行うケースが非常に多いので、ドットコマンドを癖にしておくと効率よくtext editすることができて大変気分が良いです。

"."コマンドは超強力ですが、使いこなすためには少なくとも次の3つの概念を理解することが必要不可欠です。

  • Operator
  • Motion
  • TextObject

上記3つが必要な理由としては、Vimにおける1つの操作単位が、基本的には以下の組み合わせで考えられるためです。(Visualモード等は今回考えないものとします。)

  • Operator + TextObject
  • Operator + Motion

Operator + TextObjectの例

いわゆる有名なdawciw等がこれにあたります。

それぞれdcがOperatorで、awiwがTextObjectに対応します。

Operatorで削除や変更といった操作を指定し、TextObjectで操作範囲を指定しています。

Operator + Motionの例

gU$=G等がこれにあたります。

それぞれgU=がOperatorで、$GがMotionに対応します。

Operatorで大文字化やインデント整形といった操作を指定し、Motionで操作範囲を指定しています。

基本的にはOperator + TextObjectのパターンと似ています。

"."コマンド

上記のOperator + TextObject、Operator + Motionが一つの操作の単位になります。 すなわち、上記コマンド実行後、"."コマンドを実行することで3~4キーストローク必要な操作を1キーストロークで繰り返すことができます。最初の方の繰り返しになりますが、同じ操作を数回繰り返す、ということはtext editにおいてはよくあるケースなので、"."コマンドは多用するように心がけていくのが良いです。

今回深くは踏み込みませんが、insertモードに入ってからnormalモードに戻るまで、というのも一つの操作の単位となり、"."コマンドで繰り返すことができます。

Operator

Operatorはtextを操作するものです。MotionやTextObjectと組み合わせて使います。 操作とは以下のようなものがあります。

  • 削除
  • 変更
  • 大文字小文字の入れ替え
  • テキストフォーマット
  • インデントの変更

上記の操作に対応するOperatorはd(削除) 、c(変更)等です。 Operatorの一覧を見るのが手っ取り早いと思うので、一覧を表示してみましょう。Vimを立ち上げて徐ろに以下のコマンドを打つと具体的なOperatorのリストが表示されます。

:h operator

困ったらヘルプ(:h or :help)を使いましょう。

:help operator

1. Motions and operators             *operator*

The motion commands can be used after an operator command, to have the command
operate on the text that was moved over.  That is the text between the cursor
position before and after the motion.  Operators are generally used to delete
or change text.  The following operators are available:

c   change
d   delete
y   yank into register (does not change the text)
~   swap case (only if 'tildeop' is set)
g~  swap case
gu  make lowercase
gU  make uppercase
!   filter through an external program
=   filter through 'equalprg' or C-indenting if empty
gq  text formatting
g?  ROT13 encoding
>    shift right
<    shift left
zf  define a fold
g@  call function set with the 'operatorfunc' option

g~で大文字小文字変換、zfで折りたたみの定義ができることがわかりますね。

TextObject

TextObjectとは意味のあるtextの固まりです。 通常、単語やカッコの中身を操作対象にすることが多いと思います。TextObjectはそれらをうまく表現するための仕組みです。 具体的には以下のようなものがあります。

  • 単語
  • 段落
  • カッコ内のtext
  • 引用符内のtext

例の如く、以下のコマンドで一覧を確認することができます。

:h objects

:h objects

2.1 Text objects                     *objects*

These can be used after an operator or in Visual mode to select an object.

command        action in op-pending and Visual mode ~
------------------------------------------------------------------------------
a"         double quoted string
a'         single quoted string
a(         same as ab
a)         same as ab
a<          "a <>" from '<' to the matching '>'
a>          same as a<
aB         "a Block" from "[{" to "]}" (with brackets)
aW         "a WORD" (with white space)
a[         "a []" from '[' to the matching ']'
a]         same as a[
a`         string in backticks
ab         "a block" from "[(" to "])" (with braces)
ap         "a paragraph" (with white space)
as         "a sentence" (with white space)
at         "a tag block" (with white space)
aw         "a word" (with white space)
a{         same as aB
a}         same as aB
i"         double quoted string without the quotes
i'         single quoted string without the quotes
i(         same as ib
i)         same as ib
i<          "inner <>" from '<' to the matching '>'
i>          same as i<
iB         "inner Block" from "[{" and "]}"
iW         "inner WORD"
i[         "inner []" from '[' to the matching ']'
i]         same as i[
i`         string in backticks without the backticks
ib         "inner block" from "[(" to "])"
ip         "inner paragraph"
is         "inner sentence"
it         "inner tag block"
iw         "inner word"
i{         same as iB
i}         same as iB

よく使うのはawiwibi"等でしょうか。

Motion

いわゆるh, j, k, lでカーソル移動、0, $で文頭文末移動gg, G`で行頭行末移動、等です。Vim始めて最初に覚えるやつですね。

こちらも以下のコマンドで確認できますが、量が膨大なのでこのページ上では割愛します。

:h motion.txt

まとめ

Vimの機能を深く知るためには:helpを使いこなしましょう。

実践Vim 思考のスピードで編集しよう!

実践Vim 思考のスピードで編集しよう!

実践Vimは1000回くらい読みましょう。