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.txtVim 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:
iinserts before the cursor.aappends after the cursor.oopens 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:
hjklmove left, down, up and right. The arrow keys usually work too.wjumps to the next word andbgoes back a word.0goes to the start of the line and$to the end.gggoes to the first line,Gto the last, and5Gto line 5.