Skip to content
Home ยป working with vi Editor

working with vi Editor

Working with the vi Editor


1. Introduction

vi (Visual Editor) is one of the most powerful and commonly used text editors in Linux/UNIX systems. It is a screen-based editor that allows users to create, edit, and manage text files efficiently using the keyboard.

๐Ÿ“Œ vi is available on almost every Linux system, especially servers.


2. Why vi Editor is Important

  • Available by default on all Linux systems
  • Very fast and lightweight
  • Works in CLI (no GUI required)
  • Preferred by system administrators
  • Essential for server configuration files

3. Modes of vi Editor

vi works in three main modes (very important for exams):

1. Command Mode (Normal Mode)

  • Default mode when vi starts
  • Used for navigation, deletion, copying, saving
  • Commands do not appear on screen

๐Ÿ“Œ Press Esc to return to command mode anytime.


2. Insert Mode

  • Used to insert or edit text
  • Text appears on screen

Commands to enter Insert Mode:

  • i โ€“ insert before cursor
  • a โ€“ insert after cursor
  • o โ€“ open new line below
  • O โ€“ open new line above

๐Ÿ“Œ Press Esc to exit insert mode.


3. Last Line Mode (Ex Mode)

  • Used for saving, quitting, searching
  • Commands start with :

๐Ÿ“Œ Appears at the bottom of the screen.


4. Starting vi Editor

vi filename

Example:

vi file.txt

5. Basic Navigation Commands (Command Mode)

CommandAction
hMove left
lMove right
jMove down
kMove up
ggGo to first line
GGo to last line
0Start of line
$End of line

6. Editing Commands

Insert Mode Commands

CommandFunction
iInsert before cursor
aInsert after cursor
oNew line below
ONew line above

Delete Commands (Command Mode)

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

Copy & Paste

CommandAction
yyCopy line
pPaste below
PPaste above

7. Saving and Quitting (Last Line Mode)

CommandAction
:wSave
:qQuit
:wqSave & quit
:q!Quit without saving
:w filenameSave as new file

8. Search and Replace

Search

/text
  • Press n for next occurrence
  • Press N for previous occurrence

Replace

:%s/old/new/g
  • Replaces all occurrences

9. Undo and Redo

CommandAction
uUndo
Ctrl + rRedo

10. File Information Commands

:set number     # show line numbers
:set nonumber   # hide line numbers

11. Example: Editing a Configuration File

sudo vi /etc/hosts

Steps:

  1. Press i โ†’ insert text
  2. Make changes
  3. Press Esc
  4. Type :wq
  5. Press Enter

12. Advantages of vi Editor

  • Extremely fast
  • Keyboard-based efficiency
  • Minimal system resources
  • Ideal for servers
  • Powerful editing features

13. Disadvantages

  • Steep learning curve
  • Not beginner-friendly initially

14. Conclusion

The vi editor is a powerful, efficient, and essential tool in Linux. Understanding its modes and commands is crucial for system administration, server management, and exam preparation.