Basic motions: hjkl, words, lines

Vim · Essentials · 10 min

Motions are half of every Vim command; fast movement is where Vim speed starts.

In Normal mode you move with motions instead of arrow keys (arrows still work).

  • h j k l move left, down, up, right.
  • w jumps to the start of the next word, b back to the start of the previous word, e to the end of the word.
  • W, B, E do the same for WORDs, which are separated only by whitespace (so foo.bar(x) is one WORD but several words).
  • 0 goes to column 0, ^ to the first non-blank character, $ to the end of the line.
  • gg goes to the first line, G to the last line, 42G (or :42) to line 42.
  • Ctrl-d and Ctrl-u scroll half a page down and up.
  • { and } jump to the previous or next blank line, i.e. by paragraph.

Almost every motion takes a count: 5j moves down five lines, 3w moves three words forward. Turning on :set relativenumber shows line distances so counts are easy to read off.

These motions are not only for moving. In the next lessons you combine them with operators: dw deletes a word, c$ changes to the end of the line, y} copies a paragraph. Learn the motions well and every operator becomes more useful.

Example

w     " next word start         b     " previous word start
e     " end of word             W     " next WORD (whitespace-separated)
0     " start of line           ^     " first non-blank character
$     " end of line             gg    " first line of file
G     " last line               42G   " line 42 (same as :42)
5j    " down 5 lines            3w    " forward 3 words
}     " next blank line         Ctrl-d " half page down

Remember

  • Counts multiply motions: 5j, 3w, 2}
  • w/b/e move by words, W/B/E by whitespace-separated WORDs
  • 0, ^ and $ for line positions; gg and G for file start and end

Watch out for

  • Holding j or l to crawl across the screen; use counts, words and searches instead.

Check yourself

  1. The cursor is at the start of `user.name = x`. Where does `W` move it?
  2. What does `^` do?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: vimhelp.org/motion.txt.html