Video

Want to see the full-length video right now for free?

Sign In with GitHub for Free Access

Notes

Command Language

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.

Commands

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:

Mapping Operation
d Delete
c Change
y Yank (copy)
v Visually select
>, < indent, dedent
= 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 yy, cc, >>, == behave similarly.

Using Motions

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:

Mapping Operation
de Delete to the end of the current word
d2e Delete to the end of next word
dj Delete down a line (current and one below)
dt) Delete up until next closing parenthesis
d/world Delete up until the first search match for "world"

Further, all of these motion combinations can be used with other operators like c or v to perform those operations on the same text ranges.

See Vim's help page for motions for a full listing: :h motion.

Text Objects

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. dw 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:

Mapping Operation
iw, aw "inner word", "a word" (a word includes the space)
ip, ap "inner paragraph", "a paragraph" ("a" includes the newline)
i), a) "inner parenthesis", "a parenthesis" (includes the parens)
i', a' "inner single quote", "a single quote" (includes the quotes)
i", a" "inner double quote", "a double quote" (includes the quotes)
it, at "inner tag", "a tag" (includes the open and closing tag)

For a full listing, see :h text-objects.

The dot command

The . 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 thank yourself.

The Language of Vim

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 word" becomes 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.