Learn by reading through in order

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.

The two modes of vi
normal modemove / command (:wq etc.)insert modeyou can type textswitch with iback with Esc
Right after starting you're in 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.

① Open note.txt with vi note.txt (it starts in normal mode).

② Press i to switch to insert mode and type hello vi.

③ Press Esc to return to normal mode, then type :wq and press Enter to save and quit.

④ Back at the terminal, run cat note.txt and confirm what you typed was saved. (Run it correctly and an explanation will appear.)

Linux console
0 / 2 completed
Loading Linux Terminal...

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 / CommandModeAction
vi fileOpen a file (starts in normal)
iNormalEnter insert mode
EscInsertReturn to normal
:wNormalSave (without quitting)
:qNormalQuit (when there are no changes)
:wqNormalSave and quit
:q!NormalDiscard changes and quit
:wq saves, :q! discards
:wqsave and quit (contents kept):q!discard and quit (back to before editing)
: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

① Open scratch.txt with vi scratch.txt.

② Switch to insert mode with i, type temporary, then press Esc.

③ Type :q! and press Enter to discard the changes and quit.

④ Back at the terminal, run cat scratch.txt and confirm nothing was saved because you discarded it.

Linux console
0 / 2 completed
Loading Linux Terminal...

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.

KeyWhere typing starts
iTo the left of the cursor (before it)
aTo the right of the cursor (after it)
oOn a new line opened below
AAt the end of the line
OOn a new line opened above
Different ways to enter insert mode
itype from the left of the cursoratype from the right of the cursoroopen a new line below and type
i starts from the left, a from the right, and o from a new line below. All return with Esc.

① Open practice.txt with vi practice.txt.

② Press o to open a new line below and enter insert mode, then type first line.

③ Press Esc, press o again to open another new line, type second line, and press Esc.

④ Type :w and press Enter to save (not quitting yet), then type :q to quit.

⑤ At the terminal, run cat practice.txt and confirm both lines were saved.

Linux console
0 / 2 completed
Loading Linux Terminal...

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.

KeyAction
xDelete one character under the cursor
ddDelete one line (cut)
3dd / d3dDelete several lines with a count (e.g. 3 lines)
yyCopy a line (yank)
10yy / y10yCopy several lines with a count (e.g. 10 lines)
pPaste the copied / cut line below
uUndo the previous operation
Editing lines with dd / yy / p
ddcut the current lineyycopy the current lineppaste on the line below
dd cuts, yy copies, and p pastes on the line below. All are typed in normal mode.

① Open edit.txt with vi edit.txt.

② Enter insert mode with i, type one and Enter, then two, and press Esc to prepare 2 lines.

③ Copy the current line with yy and paste it below with p, confirming the same line is added.

④ Then delete the line the cursor is on with dd.

⑤ Save and quit with :wq, and check the result with cat edit.txt.

Linux console
0 / 2 completed
Loading Linux Terminal...

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.

Operating on several lines with a count
3dd / d3ddelete 3 lines at once10yy / y10ycopy 10 lines at onceppaste the copiedlines at once
Adding a count in front: 3dd deletes 3 lines, 10yy copies 10 lines. Paste them at once with p.

① Open block.txt with vi block.txt.

② Enter insert mode with i, type one and Enter, and likewise type two through five one per line for 5 lines total, then press Esc.

③ Type :1 and press Enter to move to line 1, cut the top 3 lines with 3dd (or d3d), then paste those 3 lines below the cursor with p and confirm you can move lines.

④ Copy 2 lines with 2yy (or y2y) and paste them below with p.

⑤ Save and quit with :wq, and check the result with cat block.txt.

Linux console
0 / 2 completed
Loading Linux Terminal...

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.

KeyAction
gg / 1GMove to the first line
GMove to the last line
/wordSearch downward (forward) for word
?wordSearch upward (backward) for word
n / NTo the next match / the previous match
Moving with gg / G and searching with / ?
gg / 1Gmove to the first lineGmove to the last line/word nsearch downward, next?word Nsearch upward, opposite
gg/G jump to first/last line; /word searches downward and ?word upward, with n/N moving to the next/previous match.

① Prepare a file with many lines using ls -l /bin > nav.txt.

② Open it with vi nav.txt, move to the last line with G, and to the first line with gg (or 1G).

③ Type /sh and press Enter to search downward for lines containing sh, then move to the next match with n and the previous with N.

④ Type ?ls and press Enter to search upward for ls this time.

⑤ Since you made no changes, quit with :q and return to the terminal.

Linux console
0 / 2 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1To be able to type text in vi, what do you press in normal mode?

Q2What do you press to return from insert mode to normal mode?

Q3What happens when you run :q!?