Vim is a text editor that runs inside your terminal and works in modes: one mode where keys move around and edit, another where keys type text. With that idea and the handful of keys below, you can edit a file on any server you log in to, and get back out again.
Why a terminal editor is worth knowing
Vim's name stands for 'Vi IMproved'. It builds on vi, an older Unix editor, and some form of vi is installed on most Linux and Unix machines. When you SSH into a server with no graphical desktop, a terminal editor is often the only way to fix a config file. Git also opens a terminal editor for commit messages and interactive rebases, and on many systems that editor is Vim unless you've chosen another.
So even if you write most of your code in a graphical editor, you'll meet Vim sooner or later. Knowing a handful of commands means that moment costs you ten seconds rather than a search for 'how do I quit Vim'.
Modes: the idea everything else hangs on
Most editors have one mode: press a key and that character appears. Vim separates moving and editing from typing. That confuses people at first. It is also why Vim is quick once learned: every key on the keyboard can be a command, with no Ctrl or Alt needed.
The modes you'll use from day one:
| Mode | How you get there | What keys do |
|---|---|---|
| Normal | Vim opens here; Esc returns here | Move, delete, copy, undo |
| Insert | i from normal mode | Type text |
| Command-line | : from normal mode | Run a command such as :w |
There is also visual mode (v), for selecting text before acting on it, but the three above are enough to edit a file and leave.
If you're not sure which mode you're in, press Esc. It always brings you back to normal mode, and pressing it when you're already there does no harm.
Moving around in normal mode
Vim opens a file in normal mode, where keys are commands rather than text. The basic cursor keys sit on the home row, under your right hand:
hmoves leftjmoves downkmoves uplmoves right
The arrow keys work too, but hjkl keeps your fingers where they type. Most motions also take a count in front, so 5j moves down five lines.
Line by line is slow in a long file. To jump to a word, type / followed by what you're looking for, then press Enter:
/salmonThe cursor lands on the next match. Press n to go to the match after that, or N to go back to the previous one. A search is a motion like any other, which matters once you start combining motions with edits.
Deleting and undoing
Editing happens in normal mode as well. Press d twice, dd, and Vim deletes the whole line the cursor is on. It isn't gone for good: Vim keeps the deleted text, so p puts it back below the cursor. That makes dd followed by p a quick way to move a line.
If the delete was a mistake, u undoes it. Vim shows a short message at the bottom, such as '1 more line', to tell you what came back. u keeps working back through your changes one at a time, and Ctrl-r redoes what you undid.
dd is one example of a pattern that runs through Vim. d is an operator, meaning 'delete', and what follows it says how much: dw deletes to the start of the next word, d$ deletes to the end of the line, and 3dd deletes three lines. Learn a few operators and a few motions and you can combine them without memorising every pairing.
Typing in insert mode
To add text, press i. Vim switches to insert mode and shows -- INSERT -- on the bottom line. From now on keys type characters, just as in any other editor. When you've finished, press Esc and you're back in normal mode, where keys are commands again.
Here is one edit from start to finish, adding a line above salmon on a menu:
i inserts before the cursor. A few related keys save cursor movement: a appends after the cursor, A appends at the end of the line, and o opens a new line below and starts insert mode on it.
Keep insert mode short: go in, type, press Esc. Moving and deleting belong to normal mode, so that's where you spend most of your time.
Saving and quitting
The famous problem is leaving Vim. From normal mode, : starts a command, and you see it on the bottom line as you type. w writes the file to disk and q quits, so :wq followed by Enter saves and exits in one go:
The other commands you'll need:
:wsaves and stays in the file.:qquits, but only if there are no unsaved changes. Otherwise Vim refuses and tells you so.:q!quits and throws your changes away.
If typing : puts a colon into your text, you're still in insert mode. Press Esc first.
A worked example
Say you need to add sardines to menu.txt on a server, and your hand slips on the way. In the shell, open the file:
vim menu.txtThen, keystroke by keystroke:
/salmon Enter jump to the salmon line
dd delete it
u undo: the line is back
i insert mode, type the new line
Esc back to normal mode
:wq Enter save and quitEvery step except the typing happens in normal mode. You go into insert mode only to type, then come straight back out.
Common mistakes
- Typing text in normal mode. Each letter runs a command, so a sentence can delete or change lines you didn't mean to touch. Press
Esc, thenuuntil the file looks right again. - Leaving Caps Lock on. Capital letters are different commands from lower-case ones, so
Jjoins lines instead of moving down. - Using
:q!out of habit. It discards everything since the last save. Reach for:wqunless you mean to throw the changes away. - Trying to learn every command at once. The commands in this page cover most quick edits; add more as you need them.
Where to go next
Vim ships with vimtutor, a hands-on lesson you run from the shell that walks through these commands on a practice file. Inside Vim, :help opens the built-in manual, and :help followed by a command, such as :help dd, jumps to that command's page.
Neovim is a fork of Vim with the same modes and keys, so everything here carries over. Most graphical editors also offer a Vim mode as a plugin, so the habits work wherever you edit. If you're making the terminal itself nicer to work in, see supercharging your terminal, and for printing a file without opening an editor at all, there's the cat command.
Key takeaways
- Vim is modal: normal mode moves and edits, insert mode types.
h,j,kandlmove the cursor, and/searches.dddeletes a line,uundoes, andistarts typing.Escalways takes you back to normal mode.:wqsaves and quits;:q!quits without saving.