Operators, Motions, and Text Objects
Vim's editing grammar: operators like d, c, y, gU and = combined with motions and text objects such as iw, i(, i" and it, shown on real code.
By The Dark DebuggerUpdated 19 min read
This article turns the separate commands from Editing Text into one system: an operator (what to do) followed by a motion or text object (what to do it to). It covers every common operator, counts, the difference between linewise and characterwise edits, and the full set of text objects, with real code before and after. You should be comfortable with the motions in Modes and Moving Around first.
Why a grammar beats a list of shortcuts
Here is a typical refactoring line in Python:
send_email(user.email, "Order shipped", render(event, "plain"), retries=3)You need to replace the arguments of render(...). In a mouse-driven editor that means aiming for just after (, dragging to just before the ), checking you did not grab the parenthesis, and typing. In Vim, with the cursor anywhere on event, you type ci(, "change inside parentheses", and then the new text. With job, "html" typed and Esc:
send_email(user.email, "Order shipped", render(job, "html"), retries=3)You do not have to memorize ci( as a shortcut. It is a sentence: c (change) + i( (inside parentheses). Learn five operators and ten objects and you can say fifty things, and each new piece you learn combines with everything you already know.
Operators
An operator waits for you to say what it should act on. Type the operator, then a motion or a text object.
| Operator | Does | Doubled (acts on the current line) |
|---|---|---|
d | Delete (and store the text in a register) | dd |
c | Change: delete, then Insert mode | cc |
y | Yank (copy) | yy |
> / < | Shift right / left by shiftwidth | >> / << |
= | Re-indent using the file type's rules | == |
gU / gu / g~ | Uppercase / lowercase / toggle case | gUU / guu / g~~ |
gq | Re-wrap text to textwidth | gqq |
! | Filter the lines through a shell command | !! |
A few quick ones on a Python settings file:
max_retries = 3
ALLOWED_HOSTS = ["api.example.com", "admin.example.com", "localhost"]gUiw on line 1 (uppercase inner word) turned it into a constant:
MAX_RETRIES = 32GguiW lowercased the whole WORD on line 2: allowed_hosts = [...]. g~~ toggled the case of the entire first line, also giving MAX_RETRIES = 3.
! hands lines to any Unix filter. In a requirements note, 3G!ip followed by sort and Enter sorted just that block of pins:
Runtime dependencies:
boto3==1.35.20
click==8.1.7
psycopg[binary]==3.2.1
requests==2.32.3
Pin everything.gq re-wraps prose. With :set textwidth=50, gqip on a long Markdown paragraph rewrapped it at 50 columns:
Check the current release with `kubectl rollout
history`. Pick the previous revision number. Then
run the undo command.Counts
A count can go before the operator, before the motion, or both; Vim multiplies them. On this line:
The deploy is is blocked on the DNS change for api.example.com today.d3w and 3dw both deleted three words and gave the same result:
is blocked on the DNS change for api.example.com today.2d2w deleted 2 × 2 = 4 words:
blocked on the DNS change for api.example.com today.I put counts on the motion (d3w), because it reads as "delete three words".
What a motion covers: exclusive, inclusive, linewise
Every motion you learned works after an operator, and the result depends on how the motion treats its end point.
Exclusive motions stop just before the target. Inclusive motions include it. w is exclusive, e is inclusive. With the cursor on the first is in the sentence above:
| Keys | Result |
|---|---|
dw | The deploy is blocked on the DNS change for api.example.com today. |
de | The deploy is blocked on the DNS change for api.example.com today. |
dw removed is up to the start of the next word, space included. de removed only is and left two spaces. f is inclusive and t is exclusive. From the . in api.example.com:
| Keys | Deleted | Result |
|---|---|---|
dfm | .exam (through the m) | ...for apiple.com today. |
dtm | .exa (up to the m) | ...for apimple.com today. |
d/today Enter | .example.com | ...for apitoday. |
A search is a motion too, and an exclusive one, which makes d/word a precise way to delete up to a landmark you can see.
Linewise motions act on whole lines no matter where the cursor is in them: j, k, G, gg and the doubled operators. This catches people out: dj deletes two lines, the current one and the one below. Here is a Markdown runbook:
## Rolling back
Check the current release with `kubectl rollout history`. Pick the
previous revision number. Then run the undo command.
This section is obsolete. We moved to Argo CD in March and rollbacks
now happen from the Argo UI.
## Escalation3Gdj removed both lines of the first paragraph (lines 3 and 4), leaving the blank lines around it.
Text objects: i and a
A motion goes from the cursor to somewhere. A text object selects a thing that surrounds the cursor, whatever part of it the cursor is on. Every object comes in two forms:
i(inner): the contents only.a("a" or around): the contents plus the delimiters or surrounding whitespace.
| Object | Inner (i) | Around (a) |
|---|---|---|
iw / aw | the word | the word plus one side's whitespace |
iW / aW | the WORD (space-separated) | plus whitespace |
is / as | the sentence | plus trailing whitespace |
ip / ap | the paragraph | plus the blank line(s) after it |
i( ib / a( ab | inside ( ) | including the parentheses |
i[ / a[ | inside [ ] | including the brackets |
i{ iB / a{ aB | inside { } | including the braces |
i< / a< | inside < > | including the angle brackets |
i" i' i` / a" a' a` | inside the quotes | including the quotes and trailing (or leading) whitespace |
it / at | inside an HTML/XML tag pair | including the tags |
Closing and opening brackets are interchangeable: i( and i) select the same thing.
Text objects only work after an operator (or in Visual mode). On their own, iw would just mean "insert a w".
Words: iw vs aw
In the sentence with the duplicated is, with the cursor on the second is:
| Keys | Result |
|---|---|
diw | The deploy is blocked on ... (two spaces left behind) |
daw | The deploy is blocked on ... |
ciwwas Esc | The deploy is was blocked on ... |
Delete a word with daw, and change one with ciw (when you change a word you want the spaces to stay).
Parentheses: i( and a(
The send_email line has two sets of parentheses, one inside the other:
send_email(user.email, "Order shipped", render(event, "plain"), retries=3)| Cursor on | Keys | Result |
|---|---|---|
user | ci( + order.customer_email, subject, body | send_email(order.customer_email, subject, body) |
event | ci( + job, "html" | ...render(job, "html"), retries=3) |
event | c2i( + order.customer_email, subject, body | send_email(order.customer_email, subject, body) |
event | dib | ...render(), retries=3) |
the e of send | da( | send_email (the whole argument list, parentheses included) |
The count picks the nesting level: from inside render(...), i( is render's arguments and 2i( is send_email's. ib is a synonym for i(.
The last row shows something useful: the cursor was on send, before the first (, and Vim 9.1 still found the parentheses further along the line. In my tests, ci( typed in column 1 of that line changed send_email's arguments too.
Quotes: i" and a"
Quote objects never span lines, so Vim looks for the quotes on the current line. With the cursor in column 1:
| Keys | Result |
|---|---|
ci" + Order delivered | send_email(user.email, "Order delivered", render(... |
di" (cursor on Order) | send_email(user.email, "", render(... |
da" (cursor on Order) | send_email(user.email,, render(... |
ci" from the start of the line jumped to the first quoted string. Note the da" result. a" includes trailing whitespace, and when there is none (here a , follows the closing quote) it takes the leading whitespace instead. That left ,, with no space. If you are deleting a whole argument, delete the comma too, or use di" and fix the rest by hand.
Tags: it and at
This HTML toolbar:
<div class="toolbar">
<button class="btn btn-primary" data-action="save">Save draft</button>
<a href="/orders/4417" title="Open order">Order #4417</a>
</div>| Keys | Line 2 or 3 becomes |
|---|---|
2GfScitPublish Esc | <button class="btn btn-primary" data-action="save">Publish</button> |
2G0citPublish Esc (from column 1) | the same |
2Gdit | <button class="btn btn-primary" data-action="save"></button> |
2Gdat | the button is gone; the line is just its two spaces of indentation |
3Gfoci"View order 4417 Esc | <a href="View order 4417" title="Open order">Order #4417</a> |
The last row is a warning about ci": with the cursor on the o of /orders, the quotes around it belonged to href, so that is what changed. Quote objects pick the quotes around the cursor, not the ones you meant.
Braces and brackets: i{ and i[
When the braces sit on their own lines, i{ works on whole lines. A JavaScript config object:
const config = {
apiUrl: 'http://localhost:8080',
timeoutMs: 5000,
retries: 3,
};3Gdi{ (cursor on any line inside) removed the three property lines and kept both brace lines:
const config = {
};3Gda{ took the braces too, leaving const config = ;. di{ with the cursor in column 1 of line 1 did the same as from inside, because Vim found the { further along the line.
Square brackets work the same way. On ALLOWED_HOSTS = ["api.example.com", "admin.example.com", "localhost"]:
| Keys | Result |
|---|---|
fadi[ | ALLOWED_HOSTS = [] |
fada] | ALLOWED_HOSTS = |
faci]"api.example.com" Esc | ALLOWED_HOSTS = ["api.example.com"] |
Sentences and paragraphs: is, as, ip, ap
These shine in prose: Markdown docs, commit messages, comments. In the runbook above, the second paragraph is obsolete. With the cursor anywhere in it, dap deletes the paragraph and the blank line after it:
## Rolling back
Check the current release with `kubectl rollout history`. Pick the
previous revision number. Then run the undo command.
## Escalationdip deleted only the text lines and left two blank lines in a row. Use dap to remove a paragraph and cip to rewrite one.
Sentence objects end at ., ! or ? followed by a space or end of line. With the cursor in the first sentence of line 3, das deleted Check the current release with `kubectl rollout history`. and the space after it, so line 3 started with Pick the. dis left that space behind: Pick the.
Paragraph objects pair well with other operators. >ap indented the obsolete paragraph by 4 columns (the blank line after it stays empty), yapP duplicated it above itself, and gqap re-wraps it after an edit.
Why ci" beats manual selection
Compare two ways to change Order shipped in the send_email line, starting from column 1:
| Approach | Keys before typing the new text |
|---|---|
Move then change: f" l ct" | 5 |
Text object: ci" | 3 |
Saving two keystrokes is the small win. The bigger wins:
- The cursor can be anywhere in or before the target. You do not have to land precisely before you act.
- It cannot overshoot.
ci"stops at the closing quote whether the string is 3 characters or 300. Manual selections are where stray quotes and half-deleted brackets come from. - It repeats.
ci"+ text is one change. Move to the next string, press., and it is replaced the same way, however long that string is. A "move then change" sequence repeats only its last part.
Combining operators and objects with dot
Because an operator plus an object is a single change, the dot command repeats it. A status table where two services need updating:
<tr><td>api</td><td>degraded</td></tr>
<tr><td>auth</td><td>operational</td></tr>
<tr><td>billing</td><td>degraded</td></tr>/degraded and Enter puts the cursor on the first match. citoperational and Esc changes that cell, and n. finds the next match and repeats the change:
<tr><td>api</td><td>operational</td></tr>
<tr><td>auth</td><td>operational</td></tr>
<tr><td>billing</td><td>operational</td></tr>Case operators repeat as well. In the sentence from earlier, w then gUiw, then w. w., gave The DEPLOY IS IS blocked ....
Re-quoting strings
Two common jobs with no built-in command:
Change single quotes to double quotes. On apiUrl: 'http://localhost:8080',, type f'r" to replace the first quote, then ;.: ; repeats the f' jump to the closing quote and . repeats the r":
apiUrl: "http://localhost:8080",Wrap a word in quotes. ciw deletes the word into the small-delete register -. In Insert mode, Ctrl-R - types that register back. So ciw" + Ctrl-R - + " + Esc puts quotes around the word. Since all of it is one change, . wraps the next word too. In the config object, 3Gciw" Ctrl-R - " Esc, then 4G.:
const config = {
apiUrl: 'http://localhost:8080',
"timeoutMs": 5000,
"retries": 3,
};When you repeat it, . re-inserts whatever the new ciw deleted, so each word keeps its own name. Registers are covered in Macros and Registers.
Re-indenting with =
= re-indents lines according to the file type's indent rules. A C function pasted from somewhere that lost its indentation:
int parse_port(const char *s) {
int port = atoi(s);
if (port <= 0 || port > 65535) {
return -1;
}
return port;
}:set shiftwidth=4 expandtab, then gg=G (re-indent from the first line to the last):
int parse_port(const char *s) {
int port = atoi(s);
if (port <= 0 || port > 65535) {
return -1;
}
return port;
}Without the :set, gg=G used Vim's defaults and indented with tab characters, one tab per level. =i{ re-indents just the body of the block the cursor is in, and == the current line.
Where you'll use this
Refactoring an API call is the typical job: change a URL, swap a header, reword an error and switch a method, each a small precise edit inside dense punctuation. Before:
async function loadOrder(id) {
const res = await fetch(`/api/v1/orders/${id}`, { method: 'GET', headers: { Accept: 'text/plain' } });
if (!res.ok) throw new Error('lookup failed');
return res.text();
}The edits, one per row:
| Keys | Edit |
|---|---|
2Gf` then ci` + /api/v2/orders/${id}?expand=items Esc | Change the template-string URL |
fA then ci{ + Accept: 'application/json' Esc | Replace the inner headers object (the cursor is inside the inner braces) |
3Gci' + order lookup failed Esc | Reword the error message (ci' found the quotes further along the line) |
4G3wciwjson Esc | 3w from return lands on text; change it to json |
After, exactly as Vim saved it:
async function loadOrder(id) {
const res = await fetch(`/api/v2/orders/${id}?expand=items`, { method: 'GET', headers: { Accept: 'application/json' } });
if (!res.ok) throw new Error('order lookup failed');
return res.json();
}Note the second row. fA put the cursor on Accept, inside the inner pair of braces, so ci{ changed only the headers object. With the cursor on method instead, the same command would have replaced the whole options object. Text objects choose the innermost pair around the cursor.
The same patterns cover most daily edits:
- HTML and templates:
citfor button labels and table cells,ci"for attribute values,datto remove an element. - Function calls:
ci(to rewrite arguments,c2i(for the outer call,da(to drop a call's argument list. - Config files:
ci"andci'for values,di[to empty a list,ci{for an inline object. - Docs and commit messages:
dap,cip,das, andgqapto rewrap after editing.
Common mistakes
diw leaves a double space. diw deletes only the word, so the spaces on either side remain: The deploy is blocked. When removing a word, use daw. Keep ciw for replacing a word.
da" removes the wrong whitespace. With no whitespace after the closing quote, a" takes the whitespace before the opening quote. On send_email(user.email, "Order shipped", render(... the result was send_email(user.email,, render(.... Check the result, or use di" and handle the punctuation yourself.
ciw on a dotted name changes only one part. With the cursor on example in api.example.com, ciwmonitoring gave api.monitoring.com, because . separates words. To replace the whole host name, use the WORD object: ciWstatus.example.net gave status.example.net.
cw and dw treat the space differently. On deploy in The deploy is ..., cwrollout gave The rollout is ... and kept the space, while dw gave The is ... and removed it. cw behaves like ce on purpose (you usually want to replace the word, not the space), so do not expect c to mirror d exactly with w.
dj deletes two lines. j is a linewise motion covering the current line and the next. To delete only the current line, use dd. To delete from the cursor to the same column on the next line, Vim offers dvj, which forces characterwise, but you rarely want it.
Quote objects pick the quotes around the cursor. As in the HTML table above, ci" on the /orders link changed href, not title. Move into the string you mean (f is good for that) before changing it.
Summary
- An edit is an operator plus a motion or text object:
d,c,y,>,<,=,gU,gu,g~,gqand!all take the same motions and objects. Doubling the operator (dd,gUU,==) acts on the current line. - Counts multiply:
d3w=3dw, and2d2wdeletes four words. - Exclusive motions (
w,t,/) stop before the target; inclusive ones (e,f) include it; linewise ones (j,G) take whole lines, sodjdeletes two. iobjects take the contents,aobjects take delimiters or whitespace too.dawremoves a word cleanly,ciwreplaces one,ciWreplaces a dotted name or path.- Bracket and tag objects (
i(,i[,i{,it) choose the innermost pair around the cursor; a count (2i() reaches outward. In Vim 9.1,ci(,ci"andcitalso search forward along the line. a"takes trailing whitespace, or leading whitespace when there is none after the quote.- An operator plus an object is one change, so
.repeats it on the next target; "search,ciX,n." handles most multi-place edits.
Exercises
On this line, with the cursor in column 1, change Order shipped to Order delivered:
send_email(user.email, "Order shipped", render(event, "plain"), retries=3)Show Solution
2G0ci"Order delivered and Esc (the line is line 2 of notify.py):
send_email(user.email, "Order delivered", render(event, "plain"), retries=3)ci" found the first quoted string on the line.
Remove the duplicated is from The deploy is is blocked on the DNS change for api.example.com today. so that exactly one space remains.
Show Solution
Put the cursor on either is (3w from the start of the line lands on the second one) and type daw:
The deploy is blocked on the DNS change for api.example.com today.diw would leave two spaces.
Turn the three keys of this JavaScript object into quoted keys using one change and the dot command:
const config = {
apiUrl: 'http://localhost:8080',
timeoutMs: 5000,
retries: 3,
};Hint
+ moves to the first non-blank character of the next line, which is where each key starts.
Show Solution
2Gciw", then Ctrl-R -, then " and Esc. Then +.+.:
const config = {
"apiUrl": 'http://localhost:8080',
"timeoutMs": 5000,
"retries": 3,
};j. would not work reliably here: after each change the cursor sits at the end of the inserted text, and j keeps that column, which on the retries line is the comma.
Starting from column 1 of ALLOWED_HOSTS = ["api.example.com", "admin.example.com", "localhost"], replace the list contents with just "localhost".
Show Solution
0ci["localhost" and Esc (on line 2 of hosts.py, 2G0ci[...):
ALLOWED_HOSTS = ["localhost"]Like ci(, ci[ searched forward from column 1 to the first bracket.
In the send_email line, make the template html instead of plain and the retries 5 instead of 3. Use one change for each, no arrow keys, and no counting of characters.
Show Solution
One way, from line 2: /plain and Enter, then ciwhtml and Esc, then f3r5:
send_email(user.email, "Order shipped", render(event, "html"), retries=5)The search lands on plain, ciw swaps the word, and f3 finds the only 3 after the cursor for r to replace. ci" on plain works as well as ciw.
Tested with: Vim 9.1.0016 on Ubuntu 24.04 (WSL2), November 2026, with Vim's stock defaults.vim and no personal vimrc.
Each before/after pair is the saved file after sending the exact keys shown to a real Vim session. Indentation results depend on file type: the C example used Vim's built-in C indenting, and the .js, .py and .html files were edited with their stock file type settings. The forward search of ci(, ci[, di{ and cit from column 1 is what Vim 9.1 did; Vim versions much older than the one tested may not do it.
Written by The Dark Debugger
Engineer documenting the things that took me too long to learn, so they take you less time. Every Vim tutorial here is tested end to end before publishing.