Want to see the full-length video right now for free?Sign In with GitHub for Free Access
Vim allows us to describe the edits we want to make using a concise and expressive language of key mappings that together we refer to as Vim's command language. This language is one of the most powerful and unique aspects of Vim and is worth investing the time needed to master it.
Each command is made up of two parts: an operation and a section of text; much like a sentence in prose is made of a verb and a noun. The following is a list of some of the operator mappings:
||reformat (reindent, break long lines, etc)|
After pressing the key-mapping for a given command Vim will wait for you to
identify the text you want the command to operate on. The simplest commands
are made by simply repeating the operator a second time to act on the current
line. For example, where
d is the operator for "delete",
dd will delete
the whole line. Each of
== behave similarly.
Obviously it would be somewhat limiting if we could only operate on lines, so
luckily we can also identify text by using any motion. Just like you can use
w to move to the next word, you can use
dw to delete to the next word.
This also includes more complex motions such as
t which will further wait
for you to specify a charter to go up "un*T*il". Thus,
dt, would delete
up until the next comma on the current line.
The following table shows some of the many variations of the delete operation
you can build by combining the
d operator mapping with a motion:
||Delete to the end of the current word|
||Delete to the end of next word|
||Delete down a line (current and one below)|
||Delete up until next closing parenthesis|
||Delete up until the first search match for "world"|
Further, all of these motion combinations can be used with other operators
v to perform those operations on the same text ranges.
See Vim's help page for motions for a full listing:
Combining motions with operations to form commands gives us a huge array of edits we can perform just by remembering a few operators and the motion mappings we are already using to navigate. However, this approach has the limitation that in order to use the motions, you need already have your cursor at one end of the text range you want to edit.
Text objects are another "noun" that can be used in place of motions that can define a range of text from anywhere within it. For instance, given the following text with the cursor on the first "e" in greet:
def greet puts "hello" end
We can use
diw to delete the "inner word", specifically "greet" in this
case. Note that this would not be possible with a motion operation, e.g.
since we are starting in the middle of the word.
Text objects, like motions, can be used with commands to define a single "change". Unlike motions, text objects allow you to run the command from anywhere inside the text object, rather than just at the end. The following is a partial list of the text objects available in Vim:
||"inner word", "a word" (a word includes the space)|
||"inner paragraph", "a paragraph" ("a" includes the newline)|
||"inner parenthesis", "a parenthesis" (includes the parens)|
||"inner single quote", "a single quote" (includes the quotes)|
||"inner double quote", "a double quote" (includes the quotes)|
||"inner tag", "a tag" (includes the open and closing tag)|
For a full listing, see
. command in Vim repeats the last "change" command. This may sound
limited, but since "changes" in Vim are compound expressions that combine a
command and a motion or text object ("verb" and "noun"), this repeatability is
actually quite powerful. For instance, if I were to change a word by running
ciw ("change inner word"), then type "hello", then hit escape, I've now
composed an edit that can change any word to "hello", simply by pressing
Best of all, since I used the text object
iw I can repeat this change with my
cursor anywhere on any other word.
As a rule of thumb, try to think about changes in terms of repeatability. At a
minimum this means trying to use text-objects rather than motions as much as
possible. Every time you press
. and Vim just does the right thing, you'll
The beauty and power of Vim comes from this command language. Now that you have a sense of how to move in Vim, what commands are available, and what text-objects you can use, you can combine these in countless ways. You're not limited to a handful of predefined combinations, but instead you're free to combine the Verbs (commands) and Nouns (motions & text objects) in any way you need.
One benefit is not just in the efficiency and speed of these operations, but
also the surprisingly-limited memorization or thinking needed. Vim's language
is designed to closely map the way we naturally think of the changes we want to
make, and as such there is almost no need to "translate" for Vim. "change a
caw, "delete inside the single quotes" becomes
di', and so
on. The language is intuitive and efficient, leaving you free to focus on the
actual work you want to to get done.