Operator-Pending Mappings

Operator-Pending Mappings

Content #

An operator is a command that waits for you to enter a movement command, and then does something on the text between where you currently are and where the movement would take you. Some examples of operators are: d, y, c.

Movement Mappings #

:onoremap p i(

在代码的函数参数中按dp,可删除所有参数。按cp会删除后进入插入模式。

:onoremap b /return<cr>

在函数体代码中按db,可删除从当前位置开始直到return所在位置之间的所有代码。

定义新的operator-pending movement的步骤如下:

  1. Start at the cursor position.
  2. Enter visual mode(charwise).
  3. …mapping keys go here…
  4. All the text you want to include in the movement should now be selected.

Changing the Start #

:onoremap in( :<c-u>normal! f(vi(<cr>

“<c-u>”: remove the range that Vim may insert. “normal!”: simulate pressing keys in normal mode. “<cr>”: executes the :normal! command. 按下cin(,会删除当前行的小括号中的内容,并进入插入模式。

inside last parentheses

:onoremap il( :<c-u>normal! F)vi(<cr>

上一个括号。

General Rules #

  1. If your operator-pending mapping ends with some text visually selected, Vim will operate on that text.
  2. Otherwise, Vim will operate on the text between the original cursor position and the new position.

From #

:help omap-info