Vi Editor Basics
So far, we've worked with files using commands such as cat, head, and tail. These commands are useful for viewing content, but when you need to edit a file directly from the Linux terminal, a text editor is more practical.
vi is one of the most widely available terminal-based editors on Linux systems. Learning its basic commands is useful when working with remote servers where a graphical editor may not be available.
What is Vi?
Vi stands for Visual Editor. It is a terminal-based text editor used to create and modify files.
To open a file:
vi devops.txtIf the file doesn't exist, vi can create it when you save the file.
You can work with files containing any kind of text-based content, for example:
vi notes.txt
vi config.yml
vi application.json
vi main.tfThe file extension doesn't determine whether vi can edit the file. If the content is text, vi can generally be used to edit it.
Understanding Vi Modes
The most important concept to understand in vi is that it works through different modes.
For beginners, the workflow can be understood using these three states:
- Command Mode
- Insert Mode
- Escape / Return to Command Mode
Technically, Esc is a key, not a separate editing mode. Pressing Esc takes you out of Insert Mode and returns you to Command Mode. Commands such as :wq and :q! are then entered through Command-Line Mode.
Command Mode
When you open a file using vi, it starts in Command Mode.
In Command Mode, keyboard keys are interpreted as editor commands rather than normal text.
You can use Command Mode to:
- Navigate through the file
- Delete lines
- Copy lines
- Paste lines
- Move the cursor
- Perform other editing operations
You do not normally type regular text directly in Command Mode.
For example:
vi devops.txtWhen the file opens, you are initially in Command Mode.
Insert Mode
Insert Mode is used to enter and modify text inside the file.
From Command Mode, press:
iYou can now type normally.
For example:
Linux is widely used for servers and cloud infrastructure.Depending on the vi implementation, you may see:
-- INSERT --at the bottom of the terminal.
Entering Insert Mode
Command Mode
↓
i
↓
Insert ModeYou can now add or modify the file content.
Escape: Returning to Command Mode
After entering or modifying content, press:
EscThis leaves Insert Mode and returns to Command Mode.
Insert Mode
↓
Esc
↓
Command ModeThis is an important step because commands such as dd, yy, p, and :wq are not entered as normal text.
Saving and Exiting Vi
After editing the file:
- Press
Esc - Type
:wq - Press Enter
Esc
:wq
EnterHere:
w→ Write (save)q→ Quit
Therefore:
:wqmeans save the changes and exit vi.
Exit Without Saving
If you have made changes but do not want to save them:
- Press
Esc - Type:
:q!- Press Enter
Here:
q→ Quit!→ Force the operation
So:
:q!exits vi without saving the unsaved changes.
Use
:q!when you want to discard the changes made since the last save.
Basic Editing Commands
Once you understand Command Mode and Insert Mode, a few basic commands are enough to perform common editing tasks.
| Command / Key | Purpose |
|---|---|
i | Enter Insert Mode at the cursor |
Esc | Return to Command Mode |
dd | Delete the current line |
yy | Yank (copy) the current line |
p | Paste after the current line |
Delete a Line
Place the cursor on the line you want to remove and press:
ddThe entire line is deleted.
For example:
Line 1
Line 2
Line 3Place the cursor on Line 2 and press:
ddThe result becomes:
Line 1
Line 3Copy a Line
To copy a line, place the cursor on that line and press:
yyyy means yank the current line.
The copied line is stored in vi's internal buffer.
For example:
Line 1
Line 2
Line 3Place the cursor on Line 2:
yyThe line is now available to paste.
Paste a Line
After copying a line using:
yypress:
pto paste it after the current line.
For example:
Line 1
Line 2
Line 3After copying Line 2 and pressing p:
Line 1
Line 2
Line 2
Line 3Quick Reference
- Vi is a terminal-based text editor commonly available on Linux systems.
- Use
vi <filename>to open or create a file. viinitially opens in Command Mode.- Press
ito enter Insert Mode and type or modify content. - Press
Escto return to Command Mode. - Use
:wqto save and exit. - Use
:q!to exit without saving. - Use
ddto delete a line. - Use
yyto copy a line. - Use
pto paste a copied line. - Understanding the current mode is the most important part of using
vi.
Written By: Muskan Garg
How is this guide?
Last updated on
