π
π VI Editor (Powerful but Confusing at First)
What is vi?
vi is a terminal-based text editor in Linux/Unix systems.
- Old
- Extremely powerful
- Available on almost every Linux server
- Default editor on many servers (EC2, production machines)
π Important:
Even if Nano is easier, you MUST know vi Because sometimes Nano is not installed, but
vialways is.
Why vi feels difficult for beginners π΅
Because vi has MODES.
Nano β type directly Vi β depends on which mode you are in
π This is the most important concept.
π VI MODES (MOST IMPORTANT PART)
vi works in three main modes:
| Mode | Purpose |
|---|---|
| Normal mode | Navigation + commands |
| Insert mode | Typing text |
| Command mode | Save, quit, search |
When you open vi, you are NOT in typing mode.
You start in Normal mode β
1οΈβ£ Normal Mode (Default mode)
This is the command mode for movement and actions.
- You cannot type text
- Every key is treated as a command
Examples:
ddβ delete lineyyβ copy linepβ pasteggβ go to top
π If something weird happens β You are probably in Normal mode π
2οΈβ£ Insert Mode (Typing mode)
This is where you can type text normally.
To enter Insert mode, press:
| Key | Meaning |
|---|---|
i | Insert before cursor |
a | Append after cursor |
o | New line below |
O | New line above |
When Insert mode is active:
- Bottom shows:
-- INSERT --
To exit Insert mode:
ESC
π ESC is your best friend in vi.
3οΈβ£ Command Mode (Colon : mode)
Used for:
- Save
- Quit
- Search
- Replace
- Line numbers
You enter command mode by typing:
:
(while in Normal mode)
Example:
:w
:q
:wq
π Opening a file in vi
vi file.txt
If file doesnβt exist:
- It will be created when you save
βοΈ BASIC WORKFLOW (VERY IMPORTANT)
This is how 90% of people use vi:
- Open file
- Press
i - Type text
- Press
ESC - Save and quit
Letβs see commands.
πΎ Save and Exit Commands (MUST KNOW)
| Command | Meaning |
|---|---|
:w | Save (write) |
:q | Quit |
:wq | Save and quit |
:x | Save and quit |
:q! | Quit without saving (force) |
:w! | Force save |
π Examples:
Save only:
:w
Save and exit:
:wq
Exit without saving:
:q!
π§ MOVEMENT (Navigation) β Core vi skill
You can use arrow keys, BUT real vi users use:
| Key | Action |
|---|---|
h | Left |
l | Right |
j | Down |
k | Up |
Why?
- Faster
- Hands stay on keyboard
Line & file movement
| Command | Meaning |
|---|---|
0 | Start of line |
$ | End of line |
gg | Go to top |
G | Go to bottom |
:10 | Go to line 10 |
βοΈ DELETE, COPY, PASTE (VERY IMPORTANT)
Delete
| Command | Action |
|---|---|
dd | Delete current line |
dw | Delete word |
d$ | Delete till end of line |
x | Delete one character |
π Delete = cut (stored in buffer)
Copy (Yank)
| Command | Action |
|---|---|
yy | Copy current line |
yw | Copy word |
y$ | Copy till end |
Paste
| Command | Action |
|---|---|
p | Paste below cursor |
P | Paste above cursor |
π Undo and Redo
| Action | Command |
|---|---|
| Undo | u |
| Redo | CTRL + R |
π Undo is life saver.
π SEARCHING (Very useful)
Search forward
/word
Search backward
?word
Press:
nβ next matchNβ previous match
π SEARCH & REPLACE (POWER FEATURE)
Format:
:s/old/new/
Examples:
Replace first match in line:
:s/java/spring/
Replace all in line:
:s/java/spring/g
Replace in entire file:
:%s/java/spring/g
Replace with confirmation:
:%s/java/spring/gc
π’ Line Numbers (VERY IMPORTANT IN DEBUGGING)
Show line numbers:
:set number
Hide line numbers:
:set nonumber
Shortcut:
:set nu
π VISUAL MODE (Text selection)
Enter Visual mode:
v
- Move cursor to select text
Commands after selection:
dβ deleteyβ copy
Visual line mode:
V
βοΈ REAL PRODUCTION USAGE EXAMPLES
Edit config file on EC2
vi /etc/nginx/nginx.conf
Edit environment variables
vi ~/.bashrc
Edit application properties
vi application.properties
β οΈ COMMON BEGINNER MISTAKES
β Typing without entering Insert mode β Forgetting to press ESC β Not knowing how to quit β Panic when screen freezes
β Solution:
ESC
:q!
Always works π
π§ MENTAL MODEL (IMPORTANT)
Think like this:
- Normal mode β THINK
- Insert mode β TYPE
- Command mode β CONTROL
π VI vs NANO (Quick Comparison)
| Feature | Nano | Vi |
|---|---|---|
| Beginner friendly | β | β |
| Always installed | β | β |
| Powerful editing | β | β |
| Used in production | Rare | Very common |
π§Ύ MUST-MEMORIZE COMMANDS (Cheat Sheet)
i β insert
ESC β normal mode
:w β save
:q β quit
:wq β save & quit
:q! β quit force
dd β delete line
yy β copy line
p β paste
u β undo
/word β search
:set nu β line numbers
Final Advice β€οΈ
Donβt try to memorize everything at once.
Start with:
iESC:wqdd/search
Thatβs enough for real-world server work.