03 - vi Quick Guide for Beginners
#03 - vi Quick Guide for Beginners
##Why Do You Need to Know vi?
When you run git commit without the -m flag, Git opens your default terminal editor — and on most Linux and macOS servers, that editor is vi(or its improved version, vim).
You will also use vi when:
- Connecting to a remote server over SSH (no GUI available)
- Running
git rebase -i(interactive rebase opens an editor) - Editing config files on a server
The goal here is not to become a vi expert. You just need to know enough to open, edit, and savea file without getting stuck.
##The Most Important Thing: vi Has Modes
This is what confuses everyone who opens vi for the first time. Pressing a key does not always type that character. vi has three modes, and which mode you are in determines what a keypress does:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
NORMAL MODE (default when you open vi)
Keys are COMMANDS, not text input.
Press 'i' to enter Insert mode.
Press ':' to enter Command-line mode.
INSERT MODE (for typing text)
Now keys type text normally.
Press ESC to return to Normal mode.
COMMAND-LINE MODE (for save, quit, search)
Commands appear at the bottom of the screen.
Press ENTER to execute, ESC to cancel.
The golden rule: If you are ever confused or something is not working, press ESCuntil you are back in Normal mode. Then start again.
##Opening vi
1
vi filename.txt
If filename.txt does not exist, vi creates it. You open in Normal mode.
You will see a mostly empty screen with ~ on empty lines and the filename at the bottom.
##Step 1: Enter Insert Mode to Start Typing
From Normal mode, press:
| Key | What it does |
|---|---|
i |
Insert BEFORE the cursor |
a |
Insert AFTER the cursor (append) |
o |
Open a NEW LINE below and insert |
O |
Open a NEW LINE above and insert |
You will see -- INSERT -- at the bottom of the screen. Now type normally.
Practice:
1
2
3
4
5
1. Open vi: vi practice.txt
2. Press i
3. Type: Hello from vi!
4. Press Enter, type: This is line 2.
5. Press Escape (returns to Normal mode)
##Step 2: Save and Quit
Press ESC first to make sure you are in Normal mode. Then type : (colon) — you are now in Command-line mode. You will see the cursor jump to the bottom of the screen.
| Command | What it does |
|---|---|
:w |
Write (save) the file |
:q |
Quit vi |
:wq |
Write and quit (save and exit) |
:x |
Same as :wq |
:q! |
Quit WITHOUT saving (force quit) |
The most common sequence you will use:
- Press
ESC - Type
:wq - Press
Enter
Done. File saved, vi closed.
If you are STUCK and just want out without saving:
- Press
ESC(maybe a few times) - Type
:q! - Press
Enter
##Navigating (Normal Mode)
Once you have content, you need to move around to edit it.
###Arrow keys work, but vi pros use these:
| Key | Movement |
|---|---|
h |
Left |
j |
Down |
k |
Up |
l |
Right |
w |
Jump forward one word |
b |
Jump back one word |
0 |
Go to start of line |
$ |
Go to end of line |
gg |
Go to top of file |
G |
Go to bottom of file |
5G |
Go to line 5 |
You do not have to memorize all of these right now. Arrow keys work fine — learn the shortcuts gradually as you use vi more.
##Editing Commands (Normal Mode)
These commands work without entering Insert mode:
| Key | What it does |
|---|---|
x |
Delete the character under the cursor |
dd |
Delete (cut) the entire current line |
yy |
Copy (yank) the current line |
p |
Paste below the cursor |
P |
Paste above the cursor |
u |
Undo last action |
Ctrl+r |
Redo (undo the undo) |
r<char> |
Replace the character under cursor with <char> |
Example — fix a typo on line 3:
- Navigate to line 3 (use arrow keys or
3G) - Move to the typo
- Press
rthen type the correct character :wqto save
##Searching
In Normal mode:
1
2
3
4
/word Search forward for "word", press Enter
?word Search backward for "word", press Enter
n Jump to next match
N Jump to previous match
Example:
- Press
/ - Type
defand press Enter - Press
nto jump to the next function definition
##The Most Common vi Scenario: Git Commit Messages
When you run git commit (without -m), Git opens vi with a template:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#Please enter the commit message for your changes. Lines starting
#with '#' will be ignored, and an empty message aborts the commit.
##On branch main
#Changes to be committed:
# modified: app.py
#```
**What to do**:
1. Press `i` (enter Insert mode)
2. Type your commit message on the FIRST line (above the `#` lines)
3. Press `ESC`
4. Type `:wq` and press `Enter`
Git reads your message and completes the commit.
**To abort the commit**(decide not to commit after all):
1. Press `ESC`
2. Delete all text on the first line (navigate there, press `dd`)
3. Type `:wq` — Git sees an empty message and cancels
Or more simply:
1. Press `ESC`
2. Type `:q!` — force quit without saving, Git cancels the commit
---
##10-Minute Hands-On Practice
Work through these steps one at a time:
```bash
vi training_notes.txt
Inside vi:
- Press
i, type three lines:1 2 3
Git is a version control system. It tracks changes over time. Branching allows parallel work.
-
Press
ESC -
Press
:w+ Enter — file saved, you stay in vi -
Press
gg— go to top of file -
Press
G— go to bottom -
Press
yy— copy last line -
Press
p— paste a copy below -
Press
u— undo the paste -
Type
/time+ Enter — search for “time” - Press
ESC, then:wq+ Enter — save and quit
Confirm it worked:
1
cat training_notes.txt
##Quick Survival Cheatsheet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
GETTING IN:
vi filename open file
i start typing
GETTING OUT:
ESC back to Normal mode (do this if confused)
:wq save and quit
:q! quit WITHOUT saving
EVERYDAY EDITING:
i insert before cursor
o new line below
ESC back to Normal mode
dd delete line
u undo
/text search for text
NAVIGATION:
Arrow keys move around (always works)
gg / G top / bottom of file
Print this out and keep it next to your screen until vi feels natural.
Next: 04-fixing-mistakes.md — How to undo mistakes safely