Video

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

Notes

Modes

Vim is a "modal" editor meaning it has various modes which change its behavior in response to your key presses. This is in contrast to a more traditional modeless editor where each key combination always triggers the same behavior.

This modal nature is at the core of Vim's power and it is important to understand it in order to most efficiently use Vim.

Insert Mode

Insert mode is the mode most similar to other editors' only "mode". In insert mode, characters appear immediately in the buffer as you type them. While you might expect this to be the default mode in Vim as well, Vim instead chooses to prioritize moving through a file and making targeted edits which are done in "normal mode".

Normal Mode

Normal mode is named as such since it is the default mode Vim starts in, and the mode you are expected to be in the majority of the time.

In normal mode, each key is mapped to an operation or motion. This fits with the idea that as developers and designers, we actually spend the majority of our time moving and editing within a document, rather than simply adding long blocks of text.

You should try very hard to avoid the following:

  • Staying in insert mode for extended periods
  • Moving while in insert mode (you'd have to use the arrow keys, egads!)

Visual Mode

Enter visual mode by pressing v in normal mode. From there, you can move the cursor using all the normal motions and Vim will highlight from where you started to where you move the cursor. You can use a number of keys such as d, x, or c to operate on the visual selection similar to how these keys would operate in normal mode.

Avoiding Visual Mode

Visual mode is a great tool when you are first learning the motions and text objects as it allows you to visually confirm the text you have targeted before operating.

That said, we recommend trying to move away from visual mode over time as comfort with text objects increases. Nearly any text you want to operate on can be targeted directly with a text object more-effeciently than a visual selection.

There are two main reasons to avoid visual mode:

  • More Effort - viwd (visually select inner word, then delete) can be performed directly as diw (delete inner-word).
  • Breaks Repeating - >ip will indent any paragraph, and can be repeated with . from anywhere in any Vim paragraph. vip> will not repeat.

Occasionally visual mode is useful even to master Vimmers to target an odd range of characters that don't map to an existing text-object, but this should be considered a smell and avoided as much as possible.

Visual Line Mode

Visual line mode operates on entire lines at a time. It can be started with V from normal mode. As discussed above, we recommend avoiding standard visual mode, and we recommend that even more strongly for visual line mode. All the caveats above still apply, but to an even greater extent. If you find yourself using visual line mode, consider one of these alternatives instead:

  • Line count motion: d2j (delete down two lines)
  • Line-based operation with count: 4dd (4 times, delete line)
  • Absolute line numbers: d25G (delete to line 25)
  • Paragraph motions: dip or dap (delete inner-paragraph or "a-paragraph")

Visual Block Mode

Visual block mode allows for selecting a column of text. Unlike standard (aka characterwise) and linewise visual mode, visual block mode is likely the best tool for certain jobs and we recommend using it. Some ideal use cases for visual block mode are editing a git rebase summary

Visual block mode can be started with <C-v> from normal mode. Once started, use normal mode motions to move the highlight region.

Visual block mode uses similar mappings to normal mode, but not identical. The following is a list of the common visual block operations and their mapping:

Mapping Operation
d, or x Delete the visual block selection
c Change
r Replace all characters in the block with the next character you type
I Insert text before the block
A Insert text after the block

Note - When you change or add text, Vim will only show the change for the first line of the block, but will then replicate to all lines after you complete the change / insertion and hit <esc>.

Stay in Normal Mode

If you only take one thing from this video, let it be this: Normal mode is your friend!

Try to stay in normal mode as much as possible and you'll be fine.