The vi editor is a powerful and widely used text editor in Unix-like operating systems. Here’s a basic cheat sheet for using vi editor:
Modes
- Normal Mode:
- Default mode for navigation and manipulation.
- Insert Mode:
- Allows text insertion.
- Enter Insert Mode:
i
(before cursor).
- Visual Mode:
- Used for text selection.
- Enter Visual Mode:
v
.
- Command-Line Mode:
- For executing commands.
- Enter Command-Line Mode:
:
(from Normal Mode).
Basic Navigation
- Move Cursor Left/Right/Up/Down:
h
,l
,k
,j
(in Normal Mode).
- Go to Beginning/End of Line:
0
(zero),$
(in Normal Mode).
- Go to Beginning/End of File:
gg
(start),G
(end) (in Normal Mode).
Editing
- Insert Before/After Cursor:
i
(before cursor),a
(after cursor) (in Normal Mode).
- Insert at Beginning/End of Line:
I
(beginning),A
(end) (in Normal Mode).
- Copy (Yank) and Paste (Put):
yy
(copy line),p
(paste) (in Normal Mode).
- Delete:
dd
(delete line),x
(delete character) (in Normal Mode).
- Undo/Redo:
u
(undo),Ctrl + r
(redo) (in Normal Mode).
Searching
- Search Forward/Backward:
/search_term
,?search_term
(in Normal Mode).
- Find Next/Previous Occurrence:
n
(next),N
(previous) (in Normal Mode).
Saving and Exiting
- Save:
:w
(in Command-Line Mode).
- Save and Quit:
:wq
(in Command-Line Mode).
- Quit without Saving:
:q!
(in Command-Line Mode).
Miscellaneous
- Replace Character:
r
followed by the replacement character (in Normal Mode).
- Repeat Last Command:
.
(period) (in Normal Mode).
- Cut (Delete) and Paste (Put):
dd
(cut line),p
(paste) (in Normal Mode).
- Set Line Numbers:
:set number
(in Command-Line Mode).
- Remove Line Numbers:
:set nonumber
(in Command-Line Mode).
Exiting VI
- Save and Exit:
ZZ
(in Normal Mode).
- Quit without Saving:
:q!
(in Command-Line Mode).
Note
- To switch from Insert Mode to Normal Mode, press
Esc
. - The
:
(colon) key is used to enter Command-Line Mode.
This cheat sheet covers some fundamental vi commands. Vi has a steep learning curve, but mastering it can provide efficient text editing capabilities. Explore more advanced features and commands for a comprehensive understanding of vi.