Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Modes and Navigation

Basic Vim Motions: h j k l, Words and Find

Learn the core Vim motions: h j k l, word jumps w b e W B E, and character finds f t F T with ; and , repeats. Includes a runnable word-motion model.

Motions move the cursor, and they are also the targets of operators, so every motion you learn doubles in value. This lesson covers the small set you will use hundreds of times a day: single-character moves, word moves, and in-line character finds.

Character and line moves

Key Moves
h left
l right
j down one screen line (a logical line)
k up
gj / gk down/up by displayed line when long lines wrap
5j five lines down (counts work everywhere)

The arrow keys work too, but hjkl keep your hands on the home row. Do not hold l to cross a line: that is the slowest way to move. Use words and finds instead. A handy rule: if you press the same motion key more than three times in a row, there is a better motion.

On wrapped text, j moves to the next logical line and skips the wrapped continuation. Many people map j to gj for prose files; chapter 6 shows the mapping.

Word motions

Vim defines a word as a run of letters, digits and underscores, or a run of other non-blank characters (punctuation). A WORD (capitalized key) is any run of non-blank characters.

Key Moves to
w start of the next word
b start of the previous word
e end of the current or next word
ge end of the previous word
W B E the same with WORDs (whitespace-separated)

In foo.bar(baz), w stops at foo, then ., then bar, then (, then baz, then ). Press W and it jumps over the whole foo.bar(baz) in one go. Use capital motions for paths, URLs and long tokens.

Here is a model of those rules. It is a stand-in that finds the start of every word versus WORD, so you can see where w and W would land.

JS Where w and W land (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The w list shows many more stops than W, which is the whole point.

Finding a character on the line

f{char} jumps forward to the next occurrence of a character on the current line. The variants:

Key Behavior
f{c} move onto the next c
F{c} move onto the previous c
t{c} move till (just before) the next c
T{c} move till just after the previous c
; repeat the last f t F T
, repeat it in the opposite direction

Combined with operators these are superb: dt) deletes up to the closing parenthesis, cf; changes through the next semicolon, yt, copies up to the next comma. And ; is why f beats searching for short in-line hops: f, then ;; skips three commas in four keys.

JS f, t, ; and , (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real Vim has a detail this simplified model skips: after t,, pressing ; would be stuck on the same spot, so Vim jumps past it (the cpo option controls that). Check :help ; for the exact rule.

Counts and combining

Every motion takes a count: 3w three words forward, 2f( the second open parenthesis. And every motion works after an operator. Practice these reading exercises on any text:

  • dw: delete a word. d2w: delete two.
  • yt): copy up to the parenthesis.
  • cf.: change through the next period.
  • db: delete the previous word.

Practice routine

Open a code file and navigate it without arrow keys. Aim for few keystrokes: from the start of a line, how few can you use to reach a specific identifier? Typically f plus a unique letter, or w a few times. Resist the urge to hold a key down.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In foo.bar, how many stops does w make going from f to the end?

    w treats keyword runs and punctuation runs as separate words, so foo, the dot and bar are three stops. W would treat the whole thing as one WORD.
  2. 2. What does dt) do?

    t moves till just before the character, so the ) remains.
  3. 3. Which key repeats the last f, t, F or T in the same direction?

    ; repeats the find; the comma repeats in the opposite direction; n repeats a search.
  4. 4. When is gj more useful than j?

    gj moves by displayed lines, so long wrapped lines are navigated naturally.

Summary

  • hjkl move by character and line; gj and gk move by displayed line.
  • w b e move by word, W B E by whitespace-separated WORD.
  • f t F T find a character on the line; ; and , repeat.
  • Every motion takes a count and works after operators.
  • If you press a key many times in a row, look for a better motion.

Next: Line, Paragraph and File Motions