Vim rewards the hands more than the eyes. The built-in tutor is built around that idea: you learn a command by using it immediately, on text that is meant to be changed. Set aside roughly 25 to 30 minutes, make a practice copy of the file, and keep your fingers on the keyboard. Reading alone will not make the commands stick. Check that Shift-Lock is off, then press j enough times to bring the first lesson fully onto the screen.
Movement before memory
Start with the four movement keys: h left, j down, k up, and l right. The arrow keys usually work too, but hjkl keeps your hands in place. Once the habit forms, you can move around the screen without looking down. If you are ever unsure which mode you are in, press <ESC> to return to Normal mode and start the command again.
Move through the lesson text until you are comfortable, then use j to reach the next section. The point is not to memorize a table of keys. The point is to make each motion automatic through repetition.
Enter, leave, and fix small mistakes
From a shell prompt, open a file with:
vim filename
To leave without saving anything, press <ESC> and type:
:q!
To save your changes and quit, use:
:wq
If you started the tutorial with vimtutor, the practice file is already a copy. You can also open it with vim tutor.
In Normal mode, x deletes the character under the cursor. Use it to clean up a typo one character at a time. For example, work on this line until it reads correctly:
---\> The ccow jumpedd ovverr thhe mooon.
To insert text, move the cursor where the missing text belongs, press i, type the text, and press <ESC> to return to Normal mode. Practice on the pair below:
---\> There is text misng this .
---\> There is some text missing from this line.
The essential pattern is simple: x removes a character, i enters Insert mode, and <ESC> takes you back to Normal mode.
Deleting with objects and undoing
A single x is useful, but Vim's real strength is combining an operator with an object. dw deletes from the cursor to the end of the next word. Use it to clean up this sentence:
---\> There are a some words fun that don't belong paper in this sentence.
d$ deletes from the cursor to the end of the line. That makes it useful for trimming duplicated text:
---\> Somebody typed the end of this line twice. end of this line twice.
The general form is:
[number] d object
or:
d [number] object
Here, the number is an optional repeat count, d is the delete operator, and the object is what the command acts on. Common objects include w for a word, e for the end of a word, and $ for the end of the line. The count defaults to 1 if you leave it out.
Because deleting whole lines is so common, Vim gives it a short form: dd. You can also use a count, so 2dd deletes two lines. Try it on this list:
1) Roses are red,
2) Mud is fun,
3) Violets are blue,
4) I have a car,
5) Clocks tell time,
6) Sugar is sweet
7) And so are you.
When something goes wrong, u undoes the last change. U restores the current line to its original state. If you undo too far, CTRL-R redoes the change you just undid. Practice all three on this line:
---\> Fiix the errors oon thhis line and reeplace them witth undo.
Putting, replacing, changing
p puts back the last deleted text after the cursor. If you deleted a whole line with dd, p places that line below the current line. This is how you move lines around without retyping them. Try reordering:
d) Can you learn too?
b) Violets are blue,
c) Intelligence is learned,
a) Roses are red,
r replaces one character under the cursor and then returns you to Normal mode. It is a quick fix when a single letter is wrong:
---\> Whan this lime was tuoed in, someone presswd some wrojg keys!
---\> When this line was typed in, someone pressed some wrong keys!
To change part or all of a word, use cw. Put the cursor on the word, type cw, enter the replacement text, and press <ESC>. Unlike r, cw enters Insert mode because you are changing more than one character. Practice here:
---\> This lubw has a few wptfd that mrrf changing usf the change command.
---\> This line has a few words that need changing using the change command.
The c operator accepts the same objects as d. c$, for example, changes from the cursor to the end of the line:
---\> The end of this line needs some help to make it like the second.
---\> The end of this line needs to be corrected using the c$ command.
The pattern is worth remembering: [number] c object or c [number] object.
Finding your place
CTRL-G shows the current file name, total line count, and cursor position. SHIFT-G jumps to the last line. To jump to a specific line, type the line number and then SHIFT-G. The line number will not appear on screen while you type it, but Vim will use it when you press SHIFT-G.
To search forward, type / followed by a pattern. To search backward, use ? instead. After a search, n repeats it in the same direction, while SHIFT-N repeats it in the opposite direction. Searches wrap around the end of the file:
---\> When the search reaches the end of the file it will continue at the start.
"errroor" is not the way to spell error; errroor is an error.
When the cursor is on a bracket, % jumps to its match. It understands (), [], and {}. This is handy when you are debugging code with unbalanced brackets:
---\> This ( is a test line with ('s, ['s ] and {'s } in it. ))
To replace text, use :s. On the current line, :s/thee/the changes the first match, while :s/thee/the/g changes every match on that line. For a range of lines, use :#,#s/old/new/g. For the entire file, use :%s/old/new/g. Add c to confirm each replacement:
:%s/old/new/gc
Practice on:
---\> the best time to see thee flowers is in thee spring.
Shell commands, saving pieces, reading files
You can run a shell command without leaving Vim by typing :! followed by the command. For example, :!ls lists the current directory on Unix-like systems, and :!dir does the same on Windows. Every : command still ends with <ENTER>.
To save the current file under another name, use :w FILENAME. To save only a range of lines, use :#,# w FILENAME, where the two numbers are the first and last lines of the range. To insert another file's contents below the cursor, use :r FILENAME.
A practical sequence is to save a section to TEST with :#,# w TEST, check that it exists with :!ls or :!dir, then read it back into the current buffer with :r TEST. You can remove the test file afterward with :!rm TEST.
Opening lines, appending, and replacing continuously
o opens a new line below the cursor and enters Insert mode. O opens a new line above the cursor. Practice with:
---\> After typing o the cursor is placed on the open line in Insert mode.
Open up a line above this by typing Shift-O while the cursor is on this line.
a lets you append text after the cursor. A jumps to the end of the line and appends there, which saves you from moving the cursor manually before typing:
---\> This line will allow you to practice
---\> This line will allow you to practice appending text to the end of a line.
R enters Replace mode and keeps replacing characters until you press <ESC>. This is different from r, which replaces only one character. Use R when a long run of characters needs to be overwritten:
---\> To make the first line the same as the last on this page use the keys.
---\> To make the first line the same as the second, type R and the new text.
Vim options can change how searches behave. :set ic makes searches ignore case. :set hls is enables highlighting for all matches and incremental search as you type. Try searching for ignore with /ignore, then turn on the options and search again. n repeats the search.
Help and a starter vimrc
Vim ships with a detailed help system. Open it with <HELP> if your keyboard has that key, with <F1>, or by typing :help. Close the help window with :q. You can go straight to a topic:
:help w
:help c_<T
:help insert-index
:help user-manual
Vim has many features that are not enabled by default. A vimrc file lets you turn them on. On Unix-like systems, start editing it with:
:edit ~/.vimrc
On Windows, use:
:edit $VIM/_vimrc
Then read the example configuration:
:read $VIMRUNTIME/vimrc_example.vim
Save it with:
:write
The next time you start Vim, syntax highlighting should be active. You can keep adding your own settings to the same file.
There is much more to Vim than this first pass covers. The commands here are enough to edit text, move through files, search, replace, and run external commands without leaving the editor. When you want to go deeper, the user manual is already inside Vim, waiting behind :help user-manual.