Your First Edit
By the end of this chapter you will have opened a file, changed it in Normal mode, saved it, and quit — the four acts every session is made of. The edit is a real one: the lab performed the same keystrokes headlessly, and the changed file was read back from disk.
The one page of modal editing
Neovim starts in Normal mode. In Normal mode the letter keys do not type; they command. A few commands are the whole of daily life at first:
| Key | What it does |
|---|---|
i |
Enter Insert mode at the cursor — now keys type text |
Esc |
Return to Normal mode (the home key of the whole editor) |
h j k l |
Move left, down, up, right |
x |
Delete the character under the cursor |
dd |
Delete the whole line |
u |
Undo |
Ctrl-R |
Redo |
:w then Enter |
Save (an Ex command — chapter 1 met one already) |
:q then Enter |
Quit; :wq saves and quits, :q! quits and throws changes away |
Commands compose. ciw — change inner
word — deletes the word under the
cursor and drops you into Insert mode
to type its replacement: an operator
(c, change) applied to a text
object (iw, inner word). A moves
to the end of the line and enters
Insert mode there. /quick Enter
searches for the word quick. That
composition — operators × motions and
text objects — is Vim's grammar, and
it is a language of its own: the
series' Vim guide teaches it in full,
and this guide deliberately does not
repeat the course. The table above is
honestly enough to follow every chapter
that follows.
The lab's edit, keystroke by keystroke
The specimen is one line (transcript
neovim-lab/transcripts/t02-keystrokes.txt):
The quick brown fox jumps over the lazy dog
The lab's driver script opened the file and fed Neovim a single sequence of real keystrokes — the same ones your fingers would send:
gg go to the first line
/quick⏎ search for "quick", land on it
ciw change the word under the cursor
swift type the replacement
Esc back to Normal mode
A append at the end of the line
! type the exclamation mark
Esc back to Normal mode
then wrote the file with :write. Read
back from disk, the line is:
The swift brown fox jumps over the lazy dog!
At your own keyboard the same sequence
feels like this: open the file with
nvim fox.txt, and nothing you type
appears — you are in Normal mode, and
your first i (or the ciw above) is
what starts typing. The modal surprise
— the editor ate my letters — is
every beginner's first minute, and
Esc, then looking at the mode shown
at the bottom of the screen, is the
way out of all of it.
Buffers, windows, tabs
Three words organise everything bigger than one file, and they are worth getting right early because they do not mean what other programs mean by them:
- A buffer is a file opened in
memory. Open three files and you hold
three buffers, whether or not any of
them is on screen.
:lslists them;:bnextand:bprevwalk them. - A window is a view onto a buffer.
Split the screen (
:split,:vsplit) and two windows can show two buffers — or two places in the same buffer. - A tab page is a row of windows grouped together, a workspace rather than a file. (Editors that call their per-file tabs "tabs" have trained you to expect the other meaning; in Neovim, per-file means buffer.)
You can work for months with buffers alone, and most people do.
Getting out alive
The classic joke — how do you quit
Vim? — has a serious core: a modal
editor with unsaved changes protects
them. :q refuses to close a changed
buffer and tells you so; :q! closes
it and discards the changes; :wq
saves first. Neovim adds one kindness:
ZZ in Normal mode saves and closes
in two keystrokes. Everything in that
paragraph is the same in Vim — it is
inherited, like the grammar, and the
lab's headless runs leaned on it
constantly: nearly every transcript
ends -c "qa!", the scripted way of
saying quit everything, discard
anything unsaved, because a script
must never stop to ask a question.