DaleSchool

Moving the Cursor

Beginner15min

Learning Objectives

  • Move the cursor with h, j, k, l
  • Jump by words with w, b, e
  • Jump to the start and end of a line with 0 and $
  • Jump to the start and end of a file with gg and G

Working Code

Create a practice file:

cat << 'EOF' > navigation.txt
Server configuration file.
host: localhost
port: 8080
database: postgres
timeout: 30
max_connections: 100
log_level: debug
EOF
vim navigation.txt

Vim opens in Normal mode. Instead of arrow keys, press h, j, k, l:

KeyDirectionMnemonic
h← LeftLeftmost on the keyboard
j↓ Downj's hook points downward
k↑ Upk points upward
l→ RightRightmost on the keyboard

It feels odd at first, but your right hand never leaves the home row. You save the time it takes to reach for the arrow keys.

Try It Yourself

Word-by-word movement

Moving one character at a time is slow. Jump by words instead:

max_connections: 100

Try this on that line:

KeyActionMeaning
wStart of the next wordword
bStart of the previous wordback
eEnd of the current wordend

Press w several times. The cursor hops max_connections:100.

Line-level movement

host: localhost
KeyAction
0Start of the line (column 0)
^First non-whitespace character on the line
$End of the line

Press 0 to jump to the beginning, $ to jump to the end.

File-level movement

What if the config file is 500 lines long? You can't press j 500 times:

KeyAction
ggTop of the file
GBottom of the file
5G or :5Jump to line 5

Press G to go to the end of the file. Then press gg to snap back to the top!

Number prefixes

Type a number first and the next motion repeats that many times:

5j    → move down 5 lines
3w    → jump forward 3 words
10k   → move up 10 lines

Try pressing 5j once. It's the same as pressing j five times.

"Why?" — Why hjkl instead of arrow keys?

When vi (Vim's ancestor) was born, keyboards didn't have arrow keys. So it used h, j, k, l — the keys already sitting on the home row.

Arrow keys exist today, but hjkl is still faster:

Arrow keys: move right hand → hit arrow → return to home row (~0.5 sec)
hjkl:       hand stays put → move instantly (0 sec)

Multiply that by the thousands of cursor moves you make each day. Suffer through a few days and it becomes second nature.

Combining motion commands

Here's how it looks in a real code file:

# settings.py
DEBUG = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1']
DATABASE_URL = 'postgres://user:pass@localhost/mydb'
SECRET_KEY = 'abc123'
What you want to doKeys
From DEBUG to SECRET_KEY3j
To 'abc123' at the end of line$
Back to the comment on line 1gg
To line 33G or :3
Scan by hopping through wordsRepeat w
Scrolling by screen

In long files, scrolling by screen is handy:

KeyAction
Ctrl+fOne screen down (forward)
Ctrl+bOne screen up (backward)
Ctrl+dHalf screen down (down)
Ctrl+uHalf screen up (up)

Use Ctrl+f to skim through a 500-line log file quickly.

Deep Dive

  1. Open the navigation.txt you just created with vim.
  2. Jump to the top with gg, then the bottom with G.
  3. Jump to line 3 (port: 8080) with 3G.
  4. Press w to move word by word, then b to step back.
  5. Bounce between line start (0) and line end ($).
  6. Move down 5 lines with 5j, then up 3 lines with 3k.
  7. Navigate for 5 minutes using only hjkl — no arrow keys allowed.

Quiz

Which key moves the cursor down by one line?

Which key jumps to the start of the next word?

Which key combination jumps to the top of the file?