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:
| Key | Direction | Mnemonic |
|---|---|---|
h | Left | Leftmost key |
j | Down | βJumpβ down (or: j has a descender that points down) |
k | Up | βKickβ up |
l | Right | Rightmost key |
Why Not Arrow Keys?#
Arrow keys work in Vim, but using hjkl offers advantages:
- Speed: Your hands never leave the home row
- Efficiency: Less hand movement means faster editing
- Composability: Works with counts and operators
- Availability: Works in any terminal, even without arrow key support
Tip: Force yourself to use
hjklfor 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#
| Command | Moves to⦠|
|---|---|
0 | First column (column 0) |
^ | First non-blank character |
$ | End of line |
g_ | Last non-blank character |
Example line: int x = 5;
int x = 5;
^ ^ ^
0 ^ $
0goes to the very first column (the first space)^goes to theiinint(first non-blank)$goes to the;(end of line)
Moving Vertically on Screen#
| Command | Moves to⦠|
|---|---|
+ or Enter | First 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:
- Real lines: Lines in the file (separated by newlines)
- Display lines: Lines as shown on screen (wrapped portions)
| Command | Operates on⦠|
|---|---|
j / k | Real lines |
gj / gk | Display 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#
| Command | Action |
|---|---|
:{n} | Go to line n (Ex command) |
{n}G | Go to line n (Normal mode) |
{n}gg | Go to line n (Normal mode) |
gg | Go to first line |
G | Go 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#
| Command | Action |
|---|---|
50% | Go to 50% through file |
25% | Go to 25% through file |
Column Movement#
Moving to a Specific Column#
| Command | Action |
|---|---|
| `{n} | ` |
Example: 20| moves to column 20.
Finding Characters in a Line#
| Command | Action |
|---|---|
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)
f(β jump to the(fxβ jump tox;β jump to nextx(there isnβt one, stays)2f,β jump to second comma (the, ypart has it)
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:
| Command | Action |
|---|---|
Ctrl-o | Jump to previous position (older) |
Ctrl-i | Jump 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#
- Open a file with multiple lines
- Disable arrow keys mentally (or remap them to nothing)
- Navigate using only
h,j,k,l - Practice until it feels natural
Exercise 2: Counts#
- Enable relative line numbers:
:set relativenumber - Look at a line 7 lines below, press
7j - Look at a line 4 lines above, press
4k - Practice jumping to specific lines by their relative number
Exercise 3: Line Boundaries#
- Go to a line with indentation
- Press
0to go to column 0 - Press
^to go to first non-blank - Press
$to go to end of line - Press
g_to go to last non-blank
Exercise 4: Jump to Lines#
- Press
ggto go to first line - Press
Gto go to last line - Press
50%to go to middle - Type
:42to go to line 42 (or any line)
Exercise 5: Jump History#
- Navigate around the file with various jumps
- Press
Ctrl-oseveral times to go back - Press
Ctrl-ito go forward again - 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#
| Category | Commands |
|---|---|
| Basic Movement | h (left), j (down), k (up), l (right) |
| Line Boundaries | 0 (start), ^ (first char), $ (end) |
| Line Jumps | gg (first), G (last), {n}G (line n) |
| Display Lines | gj, gk, g0, g$ |
| Jump History | Ctrl-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.