Vim Operators
Vim operators are commands that perform an action on a piece of text. They are often combined with motions to specify the range of text to operate on. This combination of operators and motions is a key part of Vim’s efficiency. Think of it like a verb-noun pairing: the operator is the verb (the action), and the motion is the noun (what the action is performed on).
Let’s break down this powerful feature:
Common Operators:
d
(delete): Deletes the text specified by the motion.c
(change): Deletes the text and enters insert mode.y
(yank): Copies the text into the default register. You can then paste it usingp
(put).>
(indent): Increases the indentation of the text.<
(unindent): Decreases the indentation of the text.gU
(uppercase): Converts the text to uppercase.gu
(lowercase): Converts the text to lowercase.!
(filter): Filters the text through an external command.
Combining with Motions:
The true power comes when you combine these operators with motions. Here are a few examples:
dw
: Delete a word (d + w - word).d$
: Delete to the end of the line (d + $ - end of line).d0
: Delete to the beginning of the line (d + 0 - beginning of line).dd
: Delete the entire current line (d + d - current line).ci(
: Change the text inside the parentheses (c + i( - inside parentheses).yap
: Yank the entire paragraph (y + ap - a paragraph).>>
: Indent the current line ( > + > - current line, since>
operates line-wise).guiw
: Lowercase the current word (gu + iw - inner word).
Example - Formatting Code:
Imagine you have a block of JavaScript code:
function myFunction( param1, param2 ) {
// Some code ...
if (condition) {
// More code ...
}
}
You want to remove the extra spaces inside the parentheses. Instead of manually deleting them, you can use ci(
. Place your cursor on any character within the parentheses, type ci(
, and the spaces will be removed, leaving you in insert mode to add any replacement text if needed:
function myFunction(param1, param2) {
// Some code ...
if (condition) {
// More code ...
}
}
Advanced Usage - Repeating and Combining:
Operators can be repeated using a number prefix. For example:
3dw
: Deletes three words.5dd
: Deletes five lines.
You can also combine operators with other Vim features, such as visual mode. Select a block of text using visual mode (e.g., v
), then press an operator like d
or y
to operate on the selected text.
By mastering Vim operators and motions, you can significantly speed up your editing workflow. They provide a concise and powerful way to manipulate text within Vim, reducing reliance on the mouse and improving efficiency.