vi Editor – A Beginner’s Guide

 If you work with Linux or Unix systems, you’ve probably heard of the vi editor. Love it or fear it, vi is one of the most powerful and widely available text editors in the Unix world. This blog will help you understand what vi is, why it matters, and how to use it confidently.


What is vi?

vi (short for visual editor) is a command-line text editor available on almost every Unix/Linux system by default. It is lightweight, fast, and does not require a graphical interface—making it perfect for servers and remote systems.

Most modern systems actually use vim (Vi IMproved), which is backward compatible with vi and adds extra features.


Why Use vi?

Here’s why vi is still relevant today:

  • ✅ Available on almost every Linux/Unix system

  • ✅ Works in terminal-only environments

  • ✅ Extremely fast and efficient

  • ✅ Ideal for editing config files on servers

  • ✅ Powerful keyboard-based controls

Once you get used to it, vi can be much faster than GUI editors.


Starting vi

To open a file using vi:

vi filename.txt

If the file doesn’t exist, vi will create it.


Understanding vi Modes

This is the most important concept in vi.

1. Command Mode (Default Mode)

  • Used to navigate, delete, copy, paste, save, and quit

  • You cannot type text in this mode

2. Insert Mode

  • Used to insert or edit text

  • Press i to enter Insert Mode

3. Last Line Mode (Ex Mode)

  • Used for saving, quitting, searching, and replacing

  • Press : to enter this mode

👉 Press Esc anytime to return to Command Mode


Common vi Commands

Switching Modes

ActionCommand
Insert at cursori
Insert at line startI
Insert at line endA
Exit insert modeEsc

Saving and Quitting

ActionCommand
Save file:w
Quit:q
Save and quit:wq
Quit without saving:q!

Navigation Commands

ActionCommand
Move lefth
Move rightl
Move upk
Move downj
Beginning of line0
End of line$
Go to line number:10

Editing Text

ActionCommand
Delete a characterx
Delete a linedd
Copy a lineyy
Pastep
Undou
RedoCtrl + r

Searching and Replacing

Search

/pattern
  • Press n for next match

  • Press N for previous match

Replace

:%s/old/new/g
  • Replaces all occurrences in the file


Useful Tips for Beginners

  • 🔹 Always remember Esc is your safety key

  • 🔹 If stuck, press Esc twice

  • 🔹 Use :set number to show line numbers

  • 🔹 Practice with small files first


When Should You Use vi?

  • Editing config files like /etc/passwd, /etc/ssh/sshd_config

  • Working on remote servers via SSH

  • Quick file edits without installing software

  • Emergency troubleshooting in minimal environments


No comments:

Post a Comment