The vi Editor
Lesson 6 of 11
6The vi Editor: Thinking in Modes
vi is present on almost every Linux machine, even tiny ones. The key to mastering it is one idea: the editor has modes, and each mode gives the keys a different meaning. Once you grasp that, editing becomes fast and mouse-free.
What the picture shows: You start in Normal mode, i takes you to Insert, Esc brings you back, and : opens the command line where :wq saves and quits.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
vi is like a typewriter with two modes: one where keys put letters on the page, and one where keys control the page itself (move, cut a line, search). Beginners get confused because they type while in control mode. Once you know which mode you are in, the editor becomes fast and clear.
vi is a modal editor: normal mode for moving and editing, insert mode (i or a) for typing, Esc to go back, and the : line mode to save and quit with :wq or :q!. Commands compose like a sentence, verb plus motion: dw deletes a word and d$ deletes to the end of the line.
Before You Start
🐚Shell navigation
You open vi from the shell: vi file.txt.
✍️The idea of a text editor
Having tried Emacs helps you compare the two philosophies.
🧘Patience in the first hour
It feels strange at first because typing does not work immediately, that is normal.
The Core Idea
1Command mode
The starting mode. Keys here are not letters to type but commands: dd deletes a line, p pastes. Press Esc from any mode to return here.
2Insert mode
Only here do keys type text as in any editor. Enter with i (before cursor), a (after it) or o (a new line below).
3Ex (colon) mode
Typing : drops the cursor to the bottom line for a full command: :w save, :q quit, :wq both, :q! quit without saving.
4Commands are a language: verb + motion
d (delete) + w (word) = dw. d + $ = delete to end of line. After learning a few verbs and motions, your power multiplies by combining them.
Figure 1, The three modes: you always return to command mode with Esc, and branch out from it.
i first, and if you get lost press Esc twice then u to undo.
How It Works: Command Table and Editing Cycle
| Task | Keys (command mode) |
|---|---|
| Move the cursor | h j k l (left, down, up, right) or the arrow keys |
| Start / end of line | 0 / $ |
| Start / end of file | gg / G |
| Insert | i a o O |
| Delete char / line | x / dd |
| Copy a line and paste | yy then p |
| Undo / redo | u / CTRL-R |
| Search forward / backward | /text / ?text, then n and N to step through matches |
| Save and exit | :wq or ZZ |
| Quit without saving | :q! |
vi file.txt, you are in command mode./ to search or G for the end).i and type, then Esc.dd, yy and p.:wq.Worked Example: Common Editing Tasks
| You want to… | Press | Why it works |
|---|---|---|
| Add a line at the end of the file | G o … Esc | G jumps to the end and o opens a new line and enters insert |
| Duplicate a line | yy p | Copy then paste below the current line |
| Delete 3 lines | 3dd | A number repeats the command |
| Find a word | /error Enter n | Search, then jump to the next match |
| Mistake! | u | Undo one step |
Esc a habit before any important command; it does no harm if you are already in command mode and saves you if you are not.
Myth vs. Reality
| The myth | The reality |
|---|---|
| vi is old and useless today. | It is the editor present on any server you log into, and often the only one available in emergencies. |
| Command mode is an obstacle. | It is a feature: motions and deletions are done from the keyboard without a mouse, and can be combined and repeated with numbers. |
| vi and vim are exactly the same. | vim is an improved vi-compatible editor, and many systems make vi launch vim or a reduced build of it. |
Where Is This Idea Actually Used?
🚑Emergency fixes
Editing a config file on a remote server over SSH.
🔁Git messages
Git often opens an editor for a commit message, frequently vi.
⚡Fast editing
Power users love combining motions to edit code with no mouse.
🕰️History
Written by Bill Joy in the late 1970s and became a standard across the Unix family.
Try it yourself
You opened a file in vi and started typing, and strange things happened. Why?
vi starts in command mode. Press i to enter insert mode, Esc to return, and u to undo.
Which command saves and quits?
:w saves and :q quits; :wq does both. :q! quits and discards your changes.
What does 3dd do in command mode?
A number repeats the command: dd deletes a line, so 3dd deletes three.
Quick Review Flashcards
Esc.i before the cursor, a after, o new line.:wq or ZZ.:q!dd and p do?dd deletes the line (keeping it), p pastes it.u to undo, CTRL-R to redo.Check Your Understanding
You opened vi, typed, and odd things happened; why?
You were in command mode so letters became commands. Press Esc and u to undo, then i before typing.
What is the difference between :q, :q! and :wq?
:q exits if there are no changes, :q! exits discarding changes, :wq saves then exits.
How do you move a line elsewhere?
dd on the line, move to the new place, then p.
Resources for this part
Sorted by kind: start with the official docs when you want the precise reference, or with an article or video when you want the explanation first.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Video1
Done with this lesson?
Sign in to save your progress.