Get in, type, save, and get out alive

Chapter 2 — Survival

Get in, type, save, and get out alive. After this chapter, you can use Vim without panicking.


2.1 Opening and Closing Files

To open a file, type nvim followed by a filename at your terminal prompt:

nvim hello.pyOpen hello.py in Neovim

If the file exists, you see its contents. If it doesn't exist, you get an empty buffer — Vim creates the file when you save.

Neovim showing hello.py — file content above, status bar at the bottom
Neovim showing hello.py — file content above, status bar at the bottom

You can also type just nvim with no filename to open an empty scratch buffer. Or use vim or even vi — on most systems, they all launch the same editor.

Saving — :w

Press : to open the command line at the bottom of the screen, type w (for "write"), and press Enter. Your file is saved.

:wEnterSave the current file
After :w — the status bar confirms the file was written
After :w — the status bar confirms the file was written

Quitting — :q

The most famous question in programming: "How do I quit Vim?" Press :, type q (for "quit"), press Enter.

:qEnterQuit (if no unsaved changes)

If you have unsaved changes, Vim won't let you quit with :q. You have three options:

CommandAction
:wqSave and quit
:xSave and quit (same thing, shorter)
ZZSave and quit (no colon needed)
:q!Quit WITHOUT saving (discard changes)
ZQQuit without saving (no colon needed)
Opening a file in Vim
Opening a file in Vim
Saving a file in Vim
Saving a file in Vim

2.2 Insert Mode and Back Again

Right now, you're in normal mode. Every key is a command. You can't type text — pressing j moves the cursor down instead of typing the letter 'j'. This is the part that surprises everyone.

To type text, press i to enter insert mode. The status bar at the bottom will show INSERT. Now keys type text, just like any other editor.

iEnter insert mode
Insert mode — notice INSERT at the bottom. Keys now type text.
Insert mode — notice INSERT at the bottom. Keys now type text.

When you're done typing, press Esc to return to normal mode. The INSERT indicator disappears. You're back to command mode.

EscReturn to normal mode
After pressing Escape — INSERT is gone, we're back in normal mode
After pressing Escape — INSERT is gone, we're back in normal mode

That's the fundamental rhythm of Vim editing: i to insert, type what you need, Esc to return. Get in, type, get out.

Entering and leaving insert mode
Entering and leaving insert mode

2.3 The Escape Habit

This is the single most important habit in Vim: after every edit, press Esc. After typing a line of code, Esc. After fixing a typo, Esc. After pasting text, Esc. Always return to normal mode.

Normal mode is your home base. It's called "normal" for a reason — it's the mode you should normally be in. Insert mode is a brief excursion: get in, type, get out.

New Vim users stay in insert mode too long. They type a paragraph, then try to navigate with arrow keys, then get confused when they can't use dd to delete a line. The fix is simple: build the Esc reflex now. It will save you from every Vim confusion.

Building the Escape reflex
Building the Escape reflex

2.4 Undo and Redo

Vim is safe to experiment in. Made a mistake? Press u to undo. Keep pressing u to undo more. Vim supports multi-level undo — you can undo all the way back to the original file.

uUndo the last change

Went too far? Press Ctrl-R to redo — bring your changes back.

Ctrl-RRedo (undo the undo)
After pressing u — the deleted line is restored
After pressing u — the deleted line is restored

A "change" in Vim is everything between entering and leaving insert mode. If you press i, type three lines, then press Esc, a single u undoes all three lines. This is why the Escape habit matters — it creates natural undo checkpoints.

Undoing changes with u
Undoing changes with u

2.5 Basic Movement: h j k l

In normal mode, move the cursor with four keys right on the home row:

KeyDirectionMnemonic
hLeftLeftmost key
jDownj hangs below the line
kUpk points up
lRightRightmost key
Cursor moved to row 3, column 6 using j and l — hands never left the home row
Cursor moved to row 3, column 6 using j and l — hands never left the home row

Yes, the arrow keys work too. But they defeat the purpose. The whole point of Vim is that your hands never leave the home row. Every time you reach for an arrow key, you lose a fraction of a second. Over thousands of edits per day, that adds up.

Why hjkl? On the ADM-3A terminal where vi was born in 1976, these keys had arrow symbols printed on them. It's a historical accident that turned into the most ergonomic cursor movement in computing.

Navigating with h j k l
Navigating with h j k l

Quick Reference

KeyAction
nvim fileOpen a file in Neovim
iEnter insert mode
EscReturn to normal mode
:wEnterSave (write) the file
:qEnterQuit (if no unsaved changes)
:wqEnterSave and quit
ZZSave and quit (shortcut)
:q!EnterQuit without saving
ZQQuit without saving (shortcut)
uUndo
Ctrl-RRedo
hMove left
jMove down
kMove up
lMove right