πŸ‘ˆ

πŸ“ 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 vi always 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:

ModePurpose
Normal modeNavigation + commands
Insert modeTyping text
Command modeSave, 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 line
  • yy β†’ copy line
  • p β†’ paste
  • gg β†’ 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:

KeyMeaning
iInsert before cursor
aAppend after cursor
oNew line below
ONew 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:

  1. Open file
  2. Press i
  3. Type text
  4. Press ESC
  5. Save and quit

Let’s see commands.


πŸ’Ύ Save and Exit Commands (MUST KNOW)

CommandMeaning
:wSave (write)
:qQuit
:wqSave and quit
:xSave 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:

KeyAction
hLeft
lRight
jDown
kUp

Why?

  • Faster
  • Hands stay on keyboard

Line & file movement

CommandMeaning
0Start of line
$End of line
ggGo to top
GGo to bottom
:10Go to line 10

βœ‚οΈ DELETE, COPY, PASTE (VERY IMPORTANT)

Delete

CommandAction
ddDelete current line
dwDelete word
d$Delete till end of line
xDelete one character

πŸ“Œ Delete = cut (stored in buffer)


Copy (Yank)

CommandAction
yyCopy current line
ywCopy word
y$Copy till end

Paste

CommandAction
pPaste below cursor
PPaste above cursor

πŸ” Undo and Redo

ActionCommand
Undou
RedoCTRL + R

πŸ“Œ Undo is life saver.


πŸ” SEARCHING (Very useful)

Search forward

/word

Search backward

?word

Press:

  • n β†’ next match
  • N β†’ 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 β†’ delete
  • y β†’ 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)

FeatureNanoVi
Beginner friendlyβœ…βŒ
Always installedβŒβœ…
Powerful editingβŒβœ…
Used in productionRareVery 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:

  1. i
  2. ESC
  3. :wq
  4. dd
  5. /search

That’s enough for real-world server work.