Q1To be able to type text in vi, what do you press in normal mode?
The Basics of the vi Text Editor
Learn to start vi, switch between normal and insert mode, return with Esc, and save with :wq or discard with :q! — hands-on in a browser terminal.
Starting vi and Its Modes
vi is a text editor that's on almost every Linux server.
In this course you'll learn the minimal vi operations that work on any server.
vi has two modes.
Right after it starts you're in normal mode, where keys act as movement or commands and you can't type text.
Pressing i switches to insert mode, where you can type characters.
Pressing Esc returns you to normal mode.
i enters insert mode, and Esc returns to normal mode.vi memo.txt # open memo.txt (starts in normal mode)
# press i -> insert mode (you can type)
# type text
# press Esc -> back to normal mode
Stuck? Esc then :q! gets you out
If you make a mistake and the screen seems stuck, press Esc a few times and then :q! to exit and start over.
This always gets you safely back to the original state.
Save and Discard — :wq and :q!
Typing : in normal mode opens a command line where you can enter quit-related commands.
:wq means write and quit — that is, save and close.
:w only saves without quitting, and :q quits when there are no changes.
:q! discards changes and force-quits, returning to the state before editing.
| Key / Command | Mode | Action |
|---|---|---|
vi file | — | Open a file (starts in normal) |
i | Normal | Enter insert mode |
Esc | Insert | Return to normal |
:w | Normal | Save (without quitting) |
:q | Normal | Quit (when there are no changes) |
:wq | Normal | Save and quit |
:q! | Normal | Discard changes and quit |
:wq saves changes and quits; :q! throws away changes and quits.:q! keeps no changes
:q! quits throwing away all changes from that editing session.
It's handy when you mistype and want to start over, but when you have content to keep, use :wq.
vi draft.txt # open
# i to insert -> type text -> Esc
# type :q! -> discard changes and quit (draft.txt stays empty)
cat draft.txt # nothing was written
Doing More with vi — a / o and :w
i isn't the only way to enter insert mode.
a starts typing to the right of the cursor, and o opens a new line below the cursor and starts typing.
Uppercase A starts at the end of the line, and O opens a new line above the cursor.
They all return to normal mode with Esc just the same.
You can also use the save commands selectively.
:w only saves, without quitting, so you can keep editing.
For a long edit, save often with :w and close with :wq at the end to stay safe.
| Key | Where typing starts |
|---|---|
i | To the left of the cursor (before it) |
a | To the right of the cursor (after it) |
o | On a new line opened below |
A | At the end of the line |
O | On a new line opened above |
i starts from the left, a from the right, and o from a new line below. All return with Esc.Deleting and Copying Lines — dd / yy / p
In normal mode you can edit by line without typing characters.
dd deletes (cuts) the whole line the cursor is on.
x deletes one character under the cursor, and u undoes the previous operation (undo).
yy copies (yanks) the line the cursor is on, and p pastes the copied — or dd-cut — content onto the line below the cursor.
A line cut with dd can also be pasted with p, so it works for moving lines too.
| Key | Action |
|---|---|
x | Delete one character under the cursor |
dd | Delete one line (cut) |
3dd / d3d | Delete several lines with a count (e.g. 3 lines) |
yy | Copy a line (yank) |
10yy / y10y | Copy several lines with a count (e.g. 10 lines) |
p | Paste the copied / cut line below |
u | Undo the previous operation |
dd cuts, yy copies, and p pastes on the line below. All are typed in normal mode.Adding a count in front lets you operate on several lines at once.
3dd (or d3d) deletes 3 lines, and 10yy (or y10y) copies 10 lines.
If you paste with p after a delete or copy, you can move or duplicate several lines in one operation.
3dd deletes 3 lines, 10yy copies 10 lines. Paste them at once with p.Moving the Cursor and Searching — gg / G and / ?
In a long file, jumping all at once gets you to your destination faster than moving line by line.
gg moves the cursor to the first line of the file, and G to the last line.
Even where gg doesn't work, 1G moves to the first line.
To find text, use search.
Typing /word and Enter searches forward (downward) for word from the cursor.
?word searches upward (backward), n moves to the next match in the same direction, and N to a match in the opposite direction.
These are all typed in normal mode; no Esc needed.
| Key | Action |
|---|---|
gg / 1G | Move to the first line |
G | Move to the last line |
/word | Search downward (forward) for word |
?word | Search upward (backward) for word |
n / N | To the next match / the previous match |
gg/G jump to first/last line; /word searches downward and ?word upward, with n/N moving to the next/previous match.Knowledge Check
Answer each question one by one.
Q2What do you press to return from insert mode to normal mode?
Q3What happens when you run :q!?