Prerequisites#
- Comfort with paths,
ls, and creating files (touch,mkdir) from earlier modules - A terminal on any Linux distro (or WSL / cloud VM)
viminstalled (sudo apt install vimorsudo dnf install vimifvimis missing)
Learning objectives#
By the end of this module you will be able to:
- Open or create a file in vim (and open read-only when you only want to look)
- Move between normal, insert, and command-line modes without panic
- Move the cursor, insert and delete text, yank and put (copy/paste)
- Search, do a simple substitute, save, and quit — including quitting without saving
- Turn on a few useful session options such as line numbers
Why editors matter on the server#
On a laptop you might prefer a graphical IDE. On a remote server, a broken config, or a recovery shell, you often have only a terminal editor. vim (and its older cousin vi) is available almost everywhere. Learning a small, reliable subset of keys pays off for years.
Many distros make vi open vim. Both names are fine for this course; the muscle memory is the same.
Opening files#
mkdir -p /tmp/linux-lab-pushpjeet/vim-lab
cd /tmp/linux-lab-pushpjeet/vim-lab
vim notes.md # create or edit
vim -R notes.md # read-only (also: view notes.md)
If the file does not exist, vim still opens a buffer; writing later creates the file.
Check your vim build (example from a lab session):
VIM - Vi IMproved 9.1 (2024 Jan 02, compiled May 23 2025 00:48:59)
Your patch level may differ. That is normal.
The three modes you actually need#
vim is modal. That is the whole game.
| Mode | How you get there | What you do |
|---|---|---|
| Normal | Start here; press Esc anytime |
Move, delete, yank, start commands |
| Insert | From normal: i, a, o, O, … |
Type text like a notepad |
| Command-line | From normal: : / ? |
Save, quit, search, substitute, :set |
Golden rule: when something feels wrong, press Esc until you are back in normal mode, then think again.
A tiny mental picture:
Esc
┌──────────────────┐
│ NORMAL │◄──── Esc from anywhere
└───┬──────────┬───┘
i/a/o│ │ : / ?
▼ ▼
INSERT COMMAND-LINE
Moving around (normal mode)#
Stay in normal mode for movement. Arrow keys often work, but these letter keys are faster and work over slow SSH:
| Key | Move |
|---|---|
h j k l |
left / down / up / right |
w / b |
next word / previous word |
0 / $ |
start / end of line |
gg / G |
top / bottom of file |
Ctrl+d / Ctrl+u |
half-page down / up (when available) |
Numbers multiply motions: 3j moves down three lines; 5w jumps five words.
Inserting text#
From normal mode:
| Key | Action |
|---|---|
i |
insert before the cursor |
a |
append after the cursor |
o |
open a new line below and insert |
O |
open a new line above and insert |
I |
insert at start of line |
A |
append at end of line |
Type your text, then Esc to return to normal mode.
Deleting, yanking, and putting#
Still in normal mode:
| Key | Action |
|---|---|
x |
delete character under cursor |
dd |
delete (cut) the whole line |
dw |
delete from cursor to end of word |
d$ |
delete to end of line |
yy |
yank (copy) the whole line |
yw |
yank a word |
p |
put (paste) after cursor / below |
P |
put before cursor / above |
u |
undo |
Ctrl+r |
redo |
Deleted text sits in a register, so dd then p is move-line; yy then p is duplicate-line.
Search and substitute#
Search (normal mode):
/patternthen Enter — search forward?patternthen Enter — search backwardn/N— next / previous match
Substitute (command-line mode):
:s/old/new/ " first match on this line
:s/old/new/g " all matches on this line
:%s/old/new/g " all matches in the file
:%s/old/new/gc " all matches, confirm each
Batch demo (safe, non-interactive) on our lab file. Starting content:
Hello Linux
This is a notes file
Practice makes progress
Run a whole-file substitute of notes → lab, then save:
cd /tmp/linux-lab-pushpjeet/vim-lab
vim -n -es -c '%s/notes/lab/g' -c 'wq' notes.md
cat -n notes.md
Real output:
1 Hello Linux
2 This is a lab file
3 Practice makes progress
You will usually do this interactively with :%s/.../.../g inside vim; the batch form is just how we captured output for the course page.
Save and quit#
From normal mode, type : then one of:
| Command | Meaning |
|---|---|
:w |
write (save) |
:q |
quit (fails if unsaved changes) |
:wq or :x |
write and quit |
:q! |
quit discarding changes |
:w filename |
write to a different name |
Read-only tip: if you opened with vim -R and then decide to save, vim may warn you; use :w! only when you are sure you have permission and intent.
A few :set options#
Useful while learning:
:set number " show line numbers
:set nonumber " hide them
:set ignorecase " search ignores case
:set hlsearch " highlight matches
These last for the current session unless you put them in a vim config later (out of scope here).
A calm first editing session (walkthrough)#
cd /tmp/linux-lab-pushpjeet/vim-labvim draft.conf- Press
i, type two lines, pressEsc - Fix a typo with
xorcw(change word:cthenw, type replacement,Esc) - Search with
/portthen Enter - Save and quit with
:wq
Example file you might create:
Draft config
port=8080
Common mistakes#
- Typing in normal mode — letters become commands (
dddeletes a line!). Pressibefore typing prose;Escwhen done. - Stuck and scared — mash
Esc, then:q!if you want out without saving. - Forgetting which mode you are in — look at the bottom:
-- INSERT --means insert; empty status usually means normal. - Using the mouse only — fine at first, but force yourself onto
hjklfor a week. :qwhen the buffer changed — vim refuses; use:wqor:q!on purpose.
Hands-on lab#
Setup
mkdir -p /tmp/linux-lab-pushpjeet/vim-lab
cd /tmp/linux-lab-pushpjeet/vim-lab
Tasks
- Create
practice.mdin vim with three short lines about what you learned today. - Move to the top with
gg, to the bottom withG. - Yank the first line (
yy), put it at the bottom (Gthenp). - Search for a word you typed; jump with
n. - Replace one word across the file with
:%s/old/new/g. - Save with
:w. Then make a silly change and exit without saving using:q!. Re-open and confirm the silly change is gone. - Open the same file read-only:
vim -R practice.md, then quit with:q.
Verify: cat practice.md shows your saved content, not the discarded edit.
Practice: check your understanding#
Multiple choice#
1. You open vim and want to type text. Which key starts insert mode before the cursor?
- A.
x - B.
i - C.
:w - D.
dd
Answer
B. i enters insert mode before the cursor. a would append after the cursor.
2. How do you return to normal mode from insert mode?
- A.
:q - B.
Ctrl+conly - C.
Esc - D.
ZZalways
Answer
C. Esc is the reliable way back to normal mode. (Some terminals also map Ctrl+c, but learn Esc.)
3. Which command writes the file and quits?
- A.
:q! - B.
:wq - C.
:set number - D.
/wq
Answer
B. :wq (or :x) saves and exits. :q! discards changes.
4. What does dd do in normal mode?
- A. Open a new line below
- B. Delete the current line
- C. Save the file
- D. Search forward
Answer
B. dd deletes (cuts) the current line.
5. Which substitute changes every foo to bar in the whole file?
- A.
:s/foo/bar/ - B.
:%s/foo/bar/g - C.
/foo/bar/g - D.
:set foo=bar
Answer
B. % means the whole file; g means every match on a line.
6. You want to leave without saving. Which is correct?
- A.
:w - B.
:q - C.
:q! - D.
:s/q!/
Answer
C. :q! quits and discards unsaved changes.
Flashcards#
| Front | Back |
|---|---|
i |
Insert before cursor |
a |
Append after cursor |
o / O |
New line below / above, insert |
Esc |
Back to normal mode |
dd |
Delete current line |
yy then p |
Yank line, put below |
/pattern |
Search forward |
:%s/a/b/g |
Replace all a with b in file |
:wq |
Write and quit |
:q! |
Quit, discard changes |
:set number |
Show line numbers |
gg / G |
Top / bottom of file |
Match the columns#
Match each item on the left with the best description on the right.
| Mode or key | Description | ||
|---|---|---|---|
| 1 | Normal mode | A | Type new text |
| 2 | Insert mode | B | Move, delete, yank; press : for commands |
| 3 | Command-line mode | C | :w, /search, :%s |
| 4 | x |
D | Delete one character |
| 5 | p |
E | Put yanked/deleted text |
| 6 | u |
F | Undo |
Answer key
1→B, 2→A, 3→C, 4→D, 5→E, 6→F
Order the steps#
Put these in the right order to create a file, edit it, and save:
- Press
Esc - Run
vim notes.md - Type your text
- Press
i - Type
:wqand press Enter
Answer key
Correct order: 2 → 4 → 3 → 1 → 5
(vim → i → type → Esc → :wq)
Mini terminal challenge#
Goal: Build confidence with one complete vim session.
Setup
mkdir -p /tmp/linux-lab-pushpjeet/vim-lab
cd /tmp/linux-lab-pushpjeet/vim-lab
Tasks
- Open
challenge.mdin vim. - Insert a title line and two bullet-like lines of text.
- Duplicate the title line at the bottom using
yy/p. - Replace the word
challenge(if you used it) or another word withpracticeusing:%s/.../.../g. - Save and quit.
- Re-open read-only and quit.
Verify: wc -l challenge.md shows at least 3 lines; grep -n practice challenge.md (or your chosen word) finds the substitute.
Stretch: Use :set number, note the line numbers, then :set nonumber before quitting.
Next up: M8 — Bash expansions and globs — let the shell generate names and expand patterns for you.