Introduction
This lesson is about editing text with vi.
The main question is:
How do you open, move, insert, delete, copy, search, save, and quit in vi without panic?
What you should be able to do after this lesson:
- Understand
vimodes. - Move through a file.
- Insert and edit text.
- Delete and copy text.
- Save and quit.
- Recognize
nano,vim, andemacs. - Understand the
EDITORvariable.
Before you start:
- on many systems, typing
vireally opensvim - if you feel lost, press
Esca few times to get back to normal mode - if you just want to leave safely,
:q!is the emergency exit
Big Idea: vi Is a Modal Editor
This is the most important beginner fact.
vi does not treat all keys the same way all the time.
The meaning of a key depends on the current mode.
Simple picture:
- normal mode = move around and give commands
- insert mode = type text
If vi feels strange, it is usually because you forgot which mode you are in.
First Safe Session
If you remember only one workflow at first, remember this:
- open the file
- press
i - type text
- press
Esc - type
:wq
That is enough for a beginner to survive first contact with vi.
Open a File
vi notes.txt
If the file exists, it opens.
If it does not exist, vi lets you create it when you save.
Move Around
Classic movement keys:
h j k l
Meaning:
h= leftj= downk= upl= right
Useful extra movement keys:
0
$
gg
G
Meaning:
0= start of current line$= end of current linegg= top of fileG= bottom of file
Arrows may work on many systems, but h j k l are the classic vi skill LPIC expects you to recognize.
Search Inside a File
Search commands:
/pattern
?pattern
n
Meaning:
/pattern= search forward?pattern= search backwardn= repeat the search in the same direction
Enter Insert Mode
Common keys:
i
a
o
Meaning:
i= insert before cursora= append after cursoro= open a new line below
To leave insert mode:
Esc
That key matters so much that many beginners press it more than once. That is fine.
Delete, Copy, Paste, Undo
Common commands:
x
d
dd
y
yy
p
u
Meaning:
x= delete one characterd= delete with a motiondd= delete current liney= yank with a motionyy= copy current linep= pasteu= undo last change
For a beginner, dd, yy, p, and u give you a lot of power very quickly.
Save and Quit
Important commands:
:w
:q
:wq
:q!
ZZ
Simple meanings:
:w= save:q= quit:wq= save and quit:q!= quit without savingZZ= save and quit
If You Panic in vi
This is the recovery block many beginners need most.
- Press
Esc. - Decide whether you want to save.
- If yes, type:
:wq - If no, type:
:q!
That is enough to recover from most beginner confusion.
Awareness of Other Editors
LPIC expects awareness of:
vimnanoemacs
vim is an extended vi.
nano is usually easier for beginners.
emacs is another major editor family.
EDITOR
Many programs look at the EDITOR environment variable to decide which editor to launch.
Example:
export EDITOR=vi
Example output from echo "$EDITOR" after export:
vi
What this shows:
- the shell variable is now set
- programs that respect
EDITORmay useviby default
Common Beginner Mistakes
- trying to type text while still in normal mode
- trying to run commands while still in insert mode
- forgetting to press
Escbefore:wqor:q! - deleting text and not knowing that
ucan undo it - thinking
viis random when the real issue is mode confusion
Practice Step by Step
1) First safe edit
- Open:
vi notes.txt - Press
i. - Type some text.
- Press
Esc. - Save and quit with:
:wq
2) Delete, undo, and leave without saving
- Open a file in
vi. - In normal mode, press
ddto delete a line. - Press
uto undo it. - If you want to leave without saving:
:q!
3) Search for text
- Open a file in
vi. - Type:
/pattern - Press
Enter. - Press
nto jump to the next match.
Cheat Sheet
- normal mode = move and command mode
- insert mode = typing mode
h j k l= basic movement0and$= start and end of lineggandG= top and bottom of filei,a,o= enter insert mode in different waysEsc= leave insert modedd= delete lineyy= copy linep= pasteu= undo/patternand?pattern= search:w= save:wq= save and quit:q!= quit without savingEDITOR= preferred editor variable
Test Your Knowledge
Complete the quiz to assess your understanding of this course's concepts.
