Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Editing Files  ›  Lesson

Vim — Modes and Moving Around

Vim 15 minVim starts in Normal mode where keys are commands, i or a or o enter Insert mode for typing, and Esc followed by a colon command saves or quits.

Vim — Modes and Moving Around

Sooner or later you'll be logged in to a server with no graphical editor, and a file will need changing. Vim, or its older relative vi, is installed almost everywhere. Newcomers often get stuck in it because they don't know one fact: the keys do different things depending on which mode you're in.

The rule

In Vim, keys are commands until you press i.

Open Asha's notes with:

$ vim ~/garden/notes.txt

Vim starts in Normal mode. Here, letters don't type text. Each one is a command: j moves down a line and x deletes a character. There are three modes to know:

Mode How you get there What keys do
Normal the starting mode; Esc from anywhere move, delete, copy, paste
Insert i, a or o from Normal type text, like any editor
Command-line : from Normal save, quit, search and replace

Three ways into Insert mode:

  • i inserts before the cursor.
  • a appends after the cursor.
  • o opens a new line below and starts typing there.

To get out of a file:

Type (in Normal mode) Result
:w then Enter write (save) the file
:q quit; refuses if you have unsaved changes
:wq save and quit
:q! quit and throw away the changes

Moving in Normal mode:

  • h j k l move left, down, up and right. The arrow keys usually work too.
  • w jumps to the next word and b goes back a word.
  • 0 goes to the start of the line and $ to the end.
  • gg goes to the first line, G to the last, and 5G to line 5.

The shortcut

When in doubt, press Esc. It always brings you back to Normal mode, and pressing it twice does no harm. From Normal mode, every exit is a colon followed by a letter or two.

Asha adds a line to her notesStep 1 of 5
  1. Open

    vim ~/garden/notes.txt. She is in Normal mode, and typing letters now would run commands.

The classic trap is starting to type the moment Vim opens. Letters like d, x and G quietly change the file or move the cursor. If that happens, press Esc and then u to undo, or leave with :q! to discard everything.

Drills

Answer fast — trust the rule.

quick drill

Which mode is Vim in right after it opens a file?

quick drill

You've made changes you don't want. How do you quit without saving?

quick drill

In Normal mode, which key opens a new line below the cursor and starts Insert mode?

quick drill

You're not sure which mode you're in and want to save. What's the reliable key sequence?

quick drill

Which keys take you to the last line of the file, then to the end of that line?

Remember

  • In Vim, keys are commands until you press i (or a, or o).
  • When in doubt, press Esc. Then :w saves, :q quits, :wq does both and :q! discards.
  • Don't type as soon as Vim opens. If something goes wrong, Esc then u undoes it.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainEscape from Vim

A friend opened a file in Vim, typed a few words, and now cannot get out. Explain what went wrong and give the exact keys to either keep or discard their changes.