Modes and Navigation
Summary: in this tutorial, you will learn master vim's modal editing system and learn to navigate files with motions, jumps, and text objects at lightning speed.
Modes and Navigation
Now that Vim is installed and configured, it's time to understand its core concept in depth: modes. Once you understand modes and learn how to move around a file efficiently, everything else in Vim clicks into place.
What you'll learn in this tutorial:
- What each of Vim's four main modes does and when to use it
- Multiple ways to enter and exit each mode
- How to move around a file without using arrow keys or a mouse
- How to move by words, lines, and pages (much faster than one character at a time)
- How to search for text and jump to specific locations
- How to set bookmarks and navigate back to them
Understanding Vim's Modes
Remember from the introduction: Vim has different "modes" for different tasks. Here's a complete reference:
| Mode | What It's For | How to Enter | How to Leave |
|---|---|---|---|
| Normal | Navigate and run commands | Default when you open Vim | (this is home base) |
| Insert | Type text | Press i, a, o, or others | Press Escape |
| Visual | Select text | Press v, V, or Ctrl-v | Press Escape |
| Command-line | Run Vim commands | Press : | Press Enter or Escape |
Normal Mode — Your Home Base
Normal mode is the default mode — it's where you start when you open Vim, and where you return to between tasks. In Normal mode, every key is a command:
hjklmove your cursor (left, down, up, right)dddeletes an entire lineyycopies ("yanks") a lineppastes what you've copied or deleteduundoes your last action
You'll spend most of your time here. Think of Normal mode as the "steering wheel" — you're navigating and controlling the editor.
Common beginner mistake: Trying to type text while in Normal mode. If you press keys and see weird things happening instead of text appearing, you're in Normal mode! Press i to switch to Insert mode first. If things get really confusing, press Escape to make sure you're in Normal mode, then try again.
Insert Mode — For Typing Text
Insert mode is where you actually type text, just like in any regular editor. There are several ways to enter Insert mode, and each one positions your cursor differently:
| Key | What It Does | When to Use It |
|---|---|---|
i | Start typing before the cursor | Most common — general text insertion |
I | Start typing at the beginning of the line | Adding something to the start of a line |
a | Start typing after the cursor | Appending text after a character |
A | Start typing at the end of the line | Adding text to the end of a line |
o | Create a new line below and start typing | Starting a new line of text |
O | Create a new line above and start typing | Inserting a line above your current position |
s | Delete the current character and start typing | Replacing a single character with different text |
S | Delete the entire line and start typing | Rewriting a whole line |
You'll know you're in Insert mode because you'll see -- INSERT -- at the bottom of the screen. Press Escape (or Ctrl-[, which does the same thing) to return to Normal mode.
Quick tips for beginners:
- Use
a(append) instead ofi(insert) when you want to start typing after the current character, not before it. - Use
Ato quickly jump to the end of a line and start typing — it's much faster than manually navigating to the end first. - Use
oto create a new line below — it's faster than going to the end of the line, pressing Enter, and then typing.
Visual Mode — For Selecting Text
Visual mode lets you highlight (select) text, then do something with the selection. It comes in three flavors:
| Key | Mode Name | What It Selects |
|---|---|---|
v | Character-wise | Individual characters (like click-and-drag in other editors) |
V | Line-wise | Entire lines at a time |
Ctrl-v | Block-wise | A rectangular column of text (very unique to Vim!) |
Once you've selected text, press a command key to act on it:
d— delete the selected texty— copy ("yank") the selected text>— indent the selected text to the right<— unindent the selected text to the left~— toggle uppercase/lowercase
Press Escape to cancel the selection and return to Normal mode.
Command-Line Mode — For Vim Commands
Press : (colon) from Normal mode to enter Command-line mode. A cursor appears at the bottom of the screen where you type commands:
:w " Save the file
:q " Quit Vim
:wq " Save and quit
:e filename " Open a different file
:!ls " Run a terminal command (ls lists files in a directory)
:%s/old/new/g " Find and replace all occurrences in the entire filePress Enter to run the command, or Escape to cancel and go back to Normal mode.
Basic Movement — h, j, k, l
In Normal mode, you use these four keys instead of arrow keys to move your cursor:
k (up)
↑
h ← → l (left / right)
↓
j (down)
Why these keys instead of arrow keys? When the original vi editor was created in 1976, the computer keyboard had arrows printed on these exact keys. But the real benefit is ergonomic: these keys are on the "home row" (the middle row where your fingers naturally rest), so your hands never need to move away from the typing position.
# Practice: open any file and move around
vim ~/.vimrc
# Press j to move down one line
# Press k to move up one line
# Press h to move left one character
# Press l to move right one characterArrow keys still work! You can absolutely use the regular arrow keys, especially while you're learning. But training yourself to use hjkl pays off over time — your hands stay in position, and these keys combine with editing commands in powerful ways you'll learn later.
Moving by Words (Much Faster!)
Moving one character at a time with h and l is slow. Instead, you can jump by whole words:
| Key | Where It Moves You | Memory Trick |
|---|---|---|
w | Forward to the start of the next word | "word forward" |
b | Backward to the start of the previous word | "back a word" |
e | Forward to the end of the current/next word | "end of word" |
ge | Backward to the end of the previous word | "go to end, backwards" |
W | Forward to start of next WORD (spaces only) | Same as w but bigger jumps |
B | Backward to previous WORD (spaces only) | Same as b but bigger jumps |
What's the difference between w and W? Lowercase w treats punctuation as word boundaries — in the text hello-world, pressing w would stop at hello, then -, then world (three "words"). Uppercase W only uses spaces as separators — hello-world is treated as one single "WORD" and W jumps over the whole thing.
Try it yourself: Open any file and press w repeatedly to watch your cursor jump word by word. Then try b to go backward. You'll immediately see how much faster this is than pressing l over and over.
Moving Within a Line
These commands let you jump to specific positions on the current line:
| Key | Where It Moves You | Plain English |
|---|---|---|
0 | The very beginning of the line | Jump to column 1 |
^ | The first non-space character | Jump to where the actual text starts (skips indentation) |
$ | The very end of the line | Jump to the last character |
f{char} | Forward to a specific character | f( jumps to the next ( on this line |
F{char} | Backward to a specific character | F( jumps back to the previous ( |
t{char} | Forward to just before a character | t) stops one character before the next ) |
T{char} | Backward to just after a character | Like F but stops one character short |
; | Repeat the last f/F/t/T jump | Jump to the next match in the same direction |
, | Repeat in the opposite direction | Go back if you overshot |
Here's a practical example. Imagine your cursor is at the start of this line:
" Say this is your line: const result = calculateTotal(items);
0 " Cursor jumps to 'c' (very start of the line)
$ " Cursor jumps to ';' (very end)
f( " Cursor jumps to the '(' character
; " If there's another '(', cursor jumps to it
^ " Cursor jumps to 'c' (first non-space character)Moving Through the Entire File
These commands let you jump around the whole file quickly — essential for large files:
| Key | Where It Moves You | Plain English |
|---|---|---|
gg | The very first line of the file | Jump to the top |
G | The very last line of the file | Jump to the bottom |
42G or 42gg | Line number 42 | Jump to a specific line |
Ctrl-d | Down half a page | Scroll down quickly |
Ctrl-u | Up half a page | Scroll up quickly |
Ctrl-f | Down a full page | Scroll down faster |
Ctrl-b | Up a full page | Scroll up faster |
H | Top of the visible screen | Move to the High position |
M | Middle of the visible screen | Move to the Middle |
L | Bottom of the visible screen | Move to the Low position |
% | The matching bracket | If cursor is on (, jumps to matching ) |
gg " Jump to the very first line
G " Jump to the very last line
42G " Jump to line 42
50% " Jump to 50% through the file (the middle)gg and G are your best friends when working with files. Need to check something at the top? Press gg. Need to see the end? Press G. Need to go back? Use Ctrl-o (jump back) — we'll cover that below.
Using Counts (Multiplying Any Motion)
Here's a powerful trick: you can put a number before almost any movement command to repeat it that many times:
5j " Move down 5 lines (instead of pressing j five times)
3w " Move forward 3 words
10l " Move right 10 characters
2} " Move forward past 2 blank lines (2 paragraphs)The formula is: number + motion = repeated motion. This works with almost every movement command and is one of the things that makes Vim so fast.
Paragraph and Sentence Movement
These commands jump by larger chunks of text — useful for scrolling through code and documents:
| Key | Where It Moves You |
|---|---|
{ | Up to the previous blank line (beginning of a paragraph or code block) |
} | Down to the next blank line (end of a paragraph or code block) |
( | Beginning of the previous sentence |
) | Beginning of the next sentence |
In code files, { and } are especially handy because functions and blocks are usually separated by blank lines. Pressing } hops from one block to the next.
Searching for Text
Searching is one of the fastest ways to navigate. It lets you jump directly to any word in the file:
| What to Type | What It Does |
|---|---|
/word then Enter | Search forward (downward) for "word" |
?word then Enter | Search backward (upward) for "word" |
n | Jump to the next match (in the search direction) |
N | Jump to the previous match (opposite direction) |
* | Instantly search for the exact word your cursor is on (forward) |
# | Instantly search for the word under your cursor (backward) |
/function " Search forward for the text "function"
n " Jump to the next occurrence
N " Jump back to the previous occurrence
* " Instantly search for whatever word your cursor is onThe * key is incredibly useful! Place your cursor on any word and press * — Vim instantly highlights every occurrence of that word in the file and jumps to the next one. Press n to cycle through all the matches. This is one of the quickest ways to find all uses of a variable or function name.
Marks (Bookmarks in Your File)
You can set marks — like bookmarks — at positions in your file and jump back to them later:
ma " Set a mark named 'a' at your current cursor position
'a " Jump back to the LINE where mark 'a' is located
`a " Jump back to the EXACT position (line and column) of mark 'a'Think of marks like sticking a Post-it note on a specific line. You can move away, do other things, and then flip right back to that exact spot.
You can use any lowercase letter for marks (ma, mb, mc, ... mz), giving you up to 26 bookmarks.
Some special marks that Vim creates automatically:
| Mark | What It Remembers |
|---|---|
'' (two single quotes) | Where you were before your last big jump |
'. | Where you made your last edit |
'^ | Where you last left Insert mode |
The Jump List (Browser-Like Back/Forward)
As you navigate around — searching, jumping to marks, going to specific lines — Vim remembers everywhere you've been, like a browser's history. You can go back and forward through this history:
| Key | What It Does |
|---|---|
Ctrl-o | Jump back to where you were before (like the browser Back button) |
Ctrl-i | Jump forward again (like the browser Forward button) |
:jumps | Show your entire jump history as a list |
This is extremely useful! You can jump to a function definition with /functionName, read it, then press Ctrl-o to go right back to where you were before.
Summary
Here's what you've learned:
- Vim has four main modes: Normal (commands), Insert (typing), Visual (selecting), Command-line (
:commands) - Normal mode is the default — every key is a command, not text
- Enter Insert mode with
i,a,o(and others) and leave with Escape hjklreplace arrow keys: left, down, up, right (keeping your hands on the home row)- Word motions:
w(next word),b(back a word),e(end of word) - Line motions:
0(start of line),$(end of line),f{char}(find character) - File motions:
gg(top of file),G(bottom),Ctrl-d/Ctrl-u(scroll half-page) - Searching:
/pattern(search forward),n/N(next/previous match),*(search word under cursor) - Put a number before any motion to repeat it:
5j= move down 5 lines - Marks (
mato set,'ato jump back) let you bookmark positions - Jump list (
Ctrl-oback,Ctrl-iforward) works like browser history for your cursor
In the next tutorial, you'll learn to combine these movements with editing commands — this is where Vim's real power shines.
Practice: Navigation Drill
Open any file with some content (like your ~/.vimrc) and practice these tasks:
- Jump to the top of the file with
gg, then to the bottom withG - Go to line 10 with
10G, then line 5 with5G - Search for a word by typing
/setand pressing Enter, then usenandNto cycle through matches - Move across a line using
w,b, ande - Jump to a specific character with
f"(finds the next quotation mark on the line) - Set a mark with
ma, move somewhere else, then jump back with'a - Use
Ctrl-oto go back through your jump history
Show Solution
# Open your vimrc (which should have plenty of content to navigate)
vim ~/.vimrc
# 1. Jump to top and bottom
# Press gg — cursor jumps to line 1 (the very top)
# Press G — cursor jumps to the last line (the very bottom)
# 2. Go to specific lines
# Type 10G — cursor jumps to line 10
# Type 5G — cursor jumps to line 5
# 3. Search for text
# Type /set and press Enter — cursor jumps to the first "set"
# Press n — jumps to the next "set"
# Press n again — keeps going to the next one
# Press N — jumps BACK to the previous "set"
# 4. Move by words
# Press w — cursor jumps to the start of the next word
# Press w again — keeps jumping word by word
# Press b — cursor jumps back to the previous word
# Press e — cursor jumps to the end of the current word
# 5. Find a character on the line
# Press f" — cursor jumps to the next " on this line
# Press ; — jumps to the next " after that
# Press , — jumps back to the previous "
# 6. Set and use a mark
# Press ma — sets a bookmark called 'a' at your current position
# Move around the file (try gg, G, /word, etc.)
# Press 'a — cursor jumps right back to where you set the mark!
# 7. Jump history
# Navigate around the file (search, gg, G, etc.)
# Press Ctrl-o — cursor jumps back to where you were before
# Press Ctrl-o again — goes back another step
# Press Ctrl-i — jumps forward again
# When done, press :q to quit (or :q! if you accidentally edited something)Written by the ShellRAG Team
The ShellRAG editorial team writes practical, beginner-friendly Vim tutorials with tested code examples and real-world use cases. Every article is technically reviewed for accuracy and updated regularly.
Learn more about us →