$_ CLI In Depth
Learning progress 0%
lesson performance tmux vim git

vim: Motions, Operators & Text Objects

5 min read Module 18: Performance, Debugging & Power Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:15

Modes and the grammar

vim has modes. Normal mode is where keys move the cursor and run commands, insert mode is where typed text goes into the file, and command mode is where you type a line that starts with a colon. Press Escape to return to normal mode from the other two. The editing grammar works in normal mode: a count, then an operator, then a motion or a text object. The count 3 with the operator d and the motion w means delete three words.

The operators act on a region. d deletes, c deletes and enters insert mode, y yanks into a register, and the greater-than and less-than signs indent. The region comes from a motion, which moves the cursor, or from a text object, which names a unit such as a word, a sentence, or the contents of quotes.

Motions

w moves to the start of the next word, b moves back to the start of the previous word, and e moves to the end of the word. A capital W does the same with words separated by spaces only. The zero key moves to column zero, the dollar sign to the end of the line, and f followed by a character moves to the next occurrence of that character on the same line. The t motion stops just before the character.

Because a motion is a region, you can see what an operator will do before you press it. dw deletes from the cursor up to the start of the next word, and dt followed by a comma deletes up to a comma. The motion is the noun, and the operator is the verb.

Text objects

Text objects are regions with a name. iw is the inner word, aw is the word with trailing space, and i followed by a quote is the text inside a pair of quotes. The difference between i and a is whether the delimiters are included. ci followed by a quote changes the text inside the quotes and leaves the quotes in place, which is often exactly what you want when you edit a string.

Text objects work with any operator. di followed by a parenthesis deletes what is inside the parentheses, and yi followed by a brace yanks the inside of a block. They make edits independent of where the cursor is inside the object.

Batch mode for experiments

vim can run without a terminal, which is useful for scripts and for teaching. The -e and -s options put vim in silent Ex mode, where it reads commands from standard input or from -c options. The -N option turns off compatibility with vi, and -u NONE skips your vimrc, so the result does not depend on your setup. Each -c option runs one command, and wq at the end writes the file and quits.

BASH1 lines
vim -Nes -u NONE -c "normal! wdw" -c "wq" words.txt

The normal command runs normal-mode keys from an Ex command, and the exclamation mark stops the keys from using any mapping. In batch mode the cursor starts on the last line of the file, which is a surprise the first time. Use gg to jump to the first line before you rely on the cursor position.

Warning

In batch mode, normal with insert text ends insert mode for you. In an interactive session, you must press Escape. Forgetting that difference is the usual reason a script hangs or a change is lost.

Practice with a purpose

Pick one motion or object per day and use it on real text until it is automatic. Use the dot command to repeat the last change, which makes an operator plus motion very cheap to repeat. The vim documentation at the project site lists every motion, operator, and object, and the vimtutor program that ships with vim teaches the basics interactively on a real terminal.

Registers and repeating changes

Every delete and yank goes into a register. The unnamed register holds the most recent change, and p puts its contents after the cursor. Named registers, written with a quote followed by a letter, keep several values at once. The dot command repeats the last change, so a dw followed by a few dots deletes several words in a row, and the count before the dot changes how many times the change repeats.

Undo and the file on disk

The u key undoes the last change, and Ctrl-r redoes it. Undo history lives in the session, so it is lost when you quit, unless you enable persistent undo in your configuration. Writing the file with a colon followed by w saves the changes to disk, and a colon followed by q quits. A colon followed by q with an exclamation mark quits without saving, which is the way out of an unwanted change.

Searching and substituting

A slash followed by a pattern searches forward through the file, and n repeats the search. The substitute command in command mode changes text across lines. A form such as substitute with the pattern, the replacement, and the g flag changes every match on each line, and a range before the command limits it to certain lines. Batch mode accepts the same substitute command with the -c option, so an edit can be applied by a script and checked afterward.

Visual selection

The v key starts a character selection and V starts a line selection. Motions extend the selection, and an operator then acts on the whole selected region. Visual mode is the easiest way to see the region an operator will use, and it is worth using while you learn the grammar, because it shows the boundaries before anything changes.

Terminal lab · 3 steps · real Linux in your browser

Edit files with vim in batch mode

Use vim motions, operators, and text objects to change files, run non-interactively with -Nes.

01
Combine a motion with an operator
In vim, an operator such as d waits for a motion. dw deletes from the cursor to the start of the next word. Run it in batch mode on a file with three words, moving to the second word first: ``bash printf "alpha beta gamma\n" > words.txt vim -Nes -u NONE -c "normal! wdw" -c "wq" words.txt `` Open words.txt. It should contain alpha gamma.
Show hint for step 1
w moves to the start of the next word. Operator plus motion is the pattern: d, c, y followed by w, e, $ and so on.
02
Change inside quotes with a text object
A text object names a region, such as the inside of a quoted string. ci" means change inside the double quotes. Move onto the first quote and change its contents to new: ``bash printf "say \"old\" now\n" > quote.txt vim -Nes -u NONE -c "normal! f\"ci\"new" -c "wq" quote.txt `` Open quote.txt. It should read say "new" now.
Show hint for step 2
f" moves the cursor to the next double quote. In batch mode :normal ends insert mode for you, so the text after ci" is typed before the end.
03
Move a line with dd and p
dd deletes the current line into the default register and p puts it back below the cursor. Together they move a line. Start with three lines and move the first one below the second. gg jumps to the first line of the file: ``bash printf "three\ntwo\none\n" > lines.txt vim -Nes -u NONE -c "normal! ggddp" -c "wq" lines.txt `` Open lines.txt. The order should be two, three, one.
Show hint for step 3
In batch mode the cursor starts on the last line, so gg moves it to the first line. After dd the cursor is on the line that was second, and p puts the deleted line after it.
Check yourself Question 1 of 4
Concept Check Reading the count, operator, motion recall

In normal mode, what does the command 3dw do?

Recap & Key Takeaways

In normal mode, a command is a count, an operator, and a motion or text object, such as 3dw or ci followed by a quote.
w, b, e, dollar sign, and f followed by a character are the common motions, and each one defines the region an operator acts on.
Text objects such as iw and i followed by a quote select a unit independent of the cursor position inside it.
vim -Nes -u NONE with -c options runs normal-mode keys in batch mode, so an edit can be scripted and checked.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue