Good Vim users rarely hold down an arrow key. Normal mode has commands that jump straight to where you want to be. Press Esc before trying any of them.
Single steps
hmoves leftjmoves downkmoves uplmoves right
Arrow keys usually work too, but these four keep your hands in place.
Moving by words
wgoes to the start of the next word.bgoes back to the start of a word.egoes to the end of a word.
With the cursor on the first letter:
restart the web server
^ press w
^ you are on "t" of "the"The capital versions W, B and E treat anything between spaces as one word, which helps with paths such as /etc/ssh/sshd_config.
Within a line
0goes to the first column.^goes to the first character that is not blank.$goes to the end of the line.
Across the file
gggoes to the first line.Ggoes to the last line.42Gor:42goes to line 42.
Line numbers are handy when a program reports an error "on line 42". Show them with :set number.
By screens
Ctrl-fforward one screen,Ctrl-bback one screen.Ctrl-ddown half a screen,Ctrl-uup half a screen.H,M,Lmove to the top, middle and bottom of the visible screen.
Two more jumps are worth knowing. { and } move by paragraphs, which are blocks separated by blank lines. % jumps between matching brackets.
Counts
Put a number before a motion to repeat it. 5j moves down five lines and 3w moves forward three words.
After a big jump, such as G or a search, press Ctrl-o to go back to where you were. This is a Vim feature, so other vi programs may not support it.
Motions also combine with editing commands. For example, d followed by w deletes up to the next word. Learning movement now pays off twice.
Try it
- Make a long file:
seq 1 200 > numbers.txt. - Open it with
vim numbers.txt. - Press
G, thengg. - Type
100G, then10j. You should be on the line that reads110. - Press
Ctrl-dandCtrl-uand watch the screen move. - Quit with
:q.
For a live shell to practise in, there is the shell navigation lab.
Check your understanding
Vim Navigation
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.