Vim Text Objects
Vim text objects allow you to work with structural units of text, like words, sentences, paragraphs, or even blocks enclosed in parentheses, brackets, or quotes. This enables more efficient editing than character-by-character or line-by-line navigation. Instead of visually selecting text or counting words, you can use text objects to operate on precise blocks of text with single commands.
Here’s a breakdown of how text objects work in Vim:
Basic Syntax:
The general syntax for using text objects is:
[operator][text object]
- Operator: This is the action you want to perform, such as
d
(delete),c
(change),y
(yank/copy),v
(visually select), or>
(indent). - Text Object: This specifies the structural unit of text you want to operate on.
Common Text Objects:
Text Object | Description | Example (with d operator) |
---|---|---|
w |
A word (including trailing whitespace) | dw (delete a word) |
W |
A WORD (separated by whitespace) | dW (delete a WORD) |
s |
A sentence | ds (delete a sentence) |
p |
A paragraph | dp (delete a paragraph) |
' |
A single-quoted string | da' (delete a single-quoted string, including the quotes) |
" |
A double-quoted string | da" (delete a double-quoted string, including the quotes) |
( |
Text within parentheses (...) |
da( (delete text inside parentheses, including the parentheses) |
) |
Text within parentheses (...) |
da) (delete text inside parentheses, including the parentheses) |
{ |
Text within curly braces {...} |
da{ (delete text inside curly braces, including the braces) |
} |
Text within curly braces {...} |
da} (delete text inside curly braces, including the braces) |
[ |
Text within square brackets [...] |
da[ (delete text inside square brackets, including the brackets) |
] |
Text within square brackets [...] |
da] (delete text inside square brackets, including the brackets) |
< |
Text within angle brackets <...> |
da< (delete text inside angle brackets, including the brackets) |
> |
Text within angle brackets <...> |
da> (delete text inside angle brackets, including the brackets) |
t |
Text from the cursor up to, but not including, the next occurrence of a given character | dt; (delete text from the cursor up to the next semicolon) |
f |
Text from the cursor up to, and including, the next occurrence of a given character | df; (delete text from the cursor to and including the next semicolon) |
Inner Text Objects:
You can use i
(inner) with most text objects to operate on the content within the delimiters.
Example | Description |
---|---|
di( |
Delete the text inside the parentheses, excluding the parentheses. |
ci[ |
Change the text inside the square brackets, excluding the brackets. |
yi" |
Yank (copy) the text inside the double quotes, excluding the quotes. |
vi{ |
Visually select the text inside the curly braces, excluding the braces. |
Example in JavaScript:
Imagine you have the following JavaScript code:
function greet(name) {
console.log("Hello, " + name + "!"); // Greeting message
}
ci"
: Changes the greeting message inside the double quotes.da(
: Deletes the entire function argument list, including the parentheses.di{
: Deletes the code block inside the curly braces, but keeps the braces.
By mastering text objects, you can significantly improve your editing speed and precision in Vim. They allow you to work with logical units of text rather than individual characters, making your workflow more efficient and intuitive.