2.1 β€” Character and Line Movement

2.1 β€” Character and Line Movement#

Efficient navigation is the foundation of Vim productivity. In this lesson, we’ll master the basic building blocks: character and line movement.

The Home Row Movement Keys#

Vim uses h, j, k, l for basic movement instead of arrow keys:

KeyDirectionMnemonic
hLeftLeftmost key
jDown”Jump” down (or: j has a descender that points down)
kUp”Kick” up
lRightRightmost key

Why Not Arrow Keys?#

Arrow keys work in Vim, but using hjkl offers advantages:

  1. Speed: Your hands never leave the home row
  2. Efficiency: Less hand movement means faster editing
  3. Composability: Works with counts and operators
  4. Availability: Works in any terminal, even without arrow key support

Tip: Force yourself to use hjkl for a week. It feels awkward at first, but becomes natural and faster.

Using Counts with Movement#

Every movement command accepts a count:

5j     β†’  Move down 5 lines
10k    β†’  Move up 10 lines
20l    β†’  Move right 20 characters
3h     β†’  Move left 3 characters

Counts transform simple movements into precise jumps.

Relative Line Numbers#

Enable relative line numbers to easily see jump distances:

:set relativenumber

Your screen shows:

  3β”‚ three lines up
  2β”‚ two lines up
  1β”‚ one line up
  0β”‚ current line (cursor here)
  1β”‚ one line down
  2β”‚ two lines down
  3β”‚ three lines down

Now you can see β€œI need to go to the line marked 7” and type 7j.

To show both absolute and relative:

:set number relativenumber

Line-Based Movement#

Moving Within a Line#

CommandMoves to…
0First column (column 0)
^First non-blank character
$End of line
g_Last non-blank character

Example line: int x = 5;

    int x = 5;
^   ^        ^
0   ^        $

Moving Vertically on Screen#

CommandMoves to…
+ or EnterFirst non-blank of next line
-First non-blank of previous line
_First non-blank of current line (like ^)

The g Prefix for Display Lines#

When lines wrap on screen, Vim distinguishes between:

CommandOperates on…
j / kReal lines
gj / gkDisplay lines
0 / $Real line boundaries
g0 / g$Display line boundaries

If a long line wraps, gj moves down to the wrapped portion, while j skips to the next real line.

Jumping to Specific Lines#

By Line Number#

CommandAction
:{n}Go to line n (Ex command)
{n}GGo to line n (Normal mode)
{n}ggGo to line n (Normal mode)
ggGo to first line
GGo to last line

Examples:

:42     β†’  Go to line 42
42G     β†’  Go to line 42
42gg    β†’  Go to line 42
gg      β†’  Go to line 1
G       β†’  Go to last line

By Percentage#

CommandAction
50%Go to 50% through file
25%Go to 25% through file

Column Movement#

Moving to a Specific Column#

CommandAction
`{n}`

Example: 20| moves to column 20.

Finding Characters in a Line#

CommandAction
f{char}Find char forward (cursor on char)
F{char}Find char backward (cursor on char)
t{char}Find char forward (cursor before char)
T{char}Find char backward (cursor after char)
;Repeat last f/F/t/T
,Repeat last f/F/t/T in opposite direction

Example: In function calculate(x, y)

These are covered in depth in the next lesson, but they’re essential for line movement.

Returning to Previous Positions#

Vim remembers where you’ve been:

CommandAction
Ctrl-oJump to previous position (older)
Ctrl-iJump to next position (newer)
``Jump to position before last jump
''Jump to line of position before last jump

The jump list stores significant movements. Use Ctrl-o repeatedly to retrace your steps.

Practice Exercises#

Exercise 1: Basic hjkl#

  1. Open a file with multiple lines
  2. Disable arrow keys mentally (or remap them to nothing)
  3. Navigate using only h, j, k, l
  4. Practice until it feels natural

Exercise 2: Counts#

  1. Enable relative line numbers: :set relativenumber
  2. Look at a line 7 lines below, press 7j
  3. Look at a line 4 lines above, press 4k
  4. Practice jumping to specific lines by their relative number

Exercise 3: Line Boundaries#

  1. Go to a line with indentation
  2. Press 0 to go to column 0
  3. Press ^ to go to first non-blank
  4. Press $ to go to end of line
  5. Press g_ to go to last non-blank

Exercise 4: Jump to Lines#

  1. Press gg to go to first line
  2. Press G to go to last line
  3. Press 50% to go to middle
  4. Type :42 to go to line 42 (or any line)

Exercise 5: Jump History#

  1. Navigate around the file with various jumps
  2. Press Ctrl-o several times to go back
  3. Press Ctrl-i to go forward again
  4. Press `` to toggle between last two positions

Common Patterns#

Editing the End of a Line#

Instead of: $ then i then arrow right
Use: A (append at end of line)

Going to a Specific Line to Edit#

Instead of: Scrolling with j repeatedly
Use: 42G to jump directly to line 42

Moving Relative to Current Line#

Instead of: Counting lines manually
Use: :set relativenumber and use the displayed numbers

Summary#

CategoryCommands
Basic Movementh (left), j (down), k (up), l (right)
Line Boundaries0 (start), ^ (first char), $ (end)
Line Jumpsgg (first), G (last), {n}G (line n)
Display Linesgj, gk, g0, g$
Jump HistoryCtrl-o (back), Ctrl-i (forward)
Counts{n}j, {n}k, etc.

Master these fundamentals, and you’ll have a solid foundation for all Vim navigation.