Linux provides powerful command-line tools for creating and managing files. Among these tools, vi is one of the most widely known text editors.
The vi editor allows users to create and modify text files directly from the Linux terminal. It is especially useful when working with remote servers where a graphical text editor may not be available.
Developers, system administrators, DevOps engineers, and Linux users commonly use vi to edit configuration files, scripts, source code, logs, and other text-based files.
Solace Infotech's sitemap also lists vi among its Linux command resources.
What Is the Vi Command in Linux?
vi is a command-line text editor available on Unix and Linux systems.
To open an existing file, use:
vi filename.txt
If the file does not exist, vi can create it when you save the content.
For example:
vi example.txt
opens example.txt for editing.
Why Use Vi?
Vi is useful because it works directly inside the terminal.
Some of its advantages include:
Available on many Linux systems
Works without a graphical interface
Useful for remote server administration
Supports efficient keyboard-based editing
Can edit configuration files quickly
Suitable for shell scripts and source code
Requires very little system resources
When managing remote Linux servers through SSH, a terminal editor such as vi can be extremely convenient.
Basic Vi Syntax
The basic syntax is:
vi filename
For example:
vi config.txt
You can also open a file using:
vi /etc/hosts
Editing system files may require administrative privileges. If appropriate:
sudo vi /etc/hosts
Use sudo carefully and make sure you understand the changes before saving system configuration files.
Understanding Vi Modes
One of the most important things to understand about vi is that it works with different modes.
The most important modes are:
Normal Mode
Normal mode is used for navigation, deletion, copying, pasting, and executing commands.
When you first open a file in vi, you are normally in Normal mode.
Press:
Esc
to return to Normal mode.
Insert Mode
Insert mode is used to type and edit text.
You can enter Insert mode by pressing:
i
After pressing i, you can start typing.
Press:
Esc
to return to Normal mode.
Command-Line Mode
Command-line mode allows you to enter commands for operations such as saving and quitting.
From Normal mode, press:
:
For example:
:w
saves the file.
Open a File With Vi
To open a file:
vi file.txt
Once the editor opens, you can navigate through the file.
If you want to start editing, press:
i
Then type your content.
When you finish editing, press:
Esc
to return to Normal mode.
Create a New File Using Vi
You can create a new file by opening a filename that does not already exist:
vi newfile.txt
Press:
i
and enter your content.
Then press:
Esc
and save the file with:
:w
The file will be created when you save it.
Save a File in Vi
To save the current file:
:w
Press Esc first if you are in Insert mode, then type:
:w
and press Enter.
Quit Vi
To exit vi:
:q
If the file has not been modified, vi will close.
Save and Quit
To save your changes and exit:
:wq
Another commonly used command is:
ZZ
in Normal mode, which saves changes and exits.
Quit Without Saving
If you have made changes but do not want to save them:
:q!
The ! tells vi to force the operation.
This is useful when you realize that you have made unwanted changes and want to leave the editor without saving them.
Save Without Quitting
Use:
:w
This saves your changes while keeping the editor open.
You can continue editing afterward.
Enter Insert Mode
There are several commands for entering Insert mode.
i
Insert before the current character:
i
a
Insert after the current character:
a
I
Move to the beginning of the line and enter Insert mode:
I
A
Move to the end of the line and enter Insert mode:
A
o
Create a new line below the current line:
o
O
Create a new line above the current line:
O
These commands make vi editing much faster once you become familiar with them.
Navigate Through a File
In Normal mode, you can use the arrow keys on many systems.
Vi also provides traditional keyboard navigation.
Move Left
h
Move Down
j
Move Up
k
Move Right
l
These four keys are fundamental to traditional vi navigation.
Move to the Beginning of a Line
Use:
0
This moves the cursor to the beginning of the current line.
You can also use:
^
to move to the first non-whitespace character on the line.
Move to the End of a Line
Use:
$
This moves the cursor to the end of the current line.
Move to the Beginning of a File
Use:
gg
This moves the cursor to the beginning of the file.
Move to the End of a File
Use:
G
This moves the cursor to the last line.
Go to a Specific Line
You can move directly to a particular line.
For example:
:25
moves to line 25.
You can also use:
25G
from Normal mode.
This is useful when working with large configuration files or source-code files.
Search for Text
Vi provides a useful search function.
To search forward, type:
/search-term
and press Enter.
For example:
/database
Vi searches for the next occurrence of database.
Press:
n
to move to the next match.
Use:
N
to move to the previous match.
Search and Replace
You can replace text throughout a file using a substitution command.
For example:
:%s/old/new/g
This replaces all occurrences of old with new in the file.
For example:
:%s/http/https/g
This can replace occurrences of http with https.
Always review replacement commands carefully, particularly when editing production configuration files.
Delete a Character
In Normal mode:
x
deletes the character under the cursor.
Delete a Line
Use:
dd
This deletes the current line.
Delete Multiple Lines
You can specify a number before dd.
For example:
3dd
deletes three lines starting from the current line.
Copy a Line
In vi, copying is commonly called yanking.
Use:
yy
to copy the current line.
Copy Multiple Lines
For example:
3yy
copies three lines starting from the current line.
Paste Text
Use:
p
to paste after the current cursor position or line.
Use:
P
to paste before the current position.
Undo Changes
To undo the most recent change:
u
This is useful if you accidentally delete or modify content.
Redo Changes
Depending on the vi implementation, redo can be performed with:
Ctrl + r
This is particularly useful after undoing a change and deciding that you want to restore it.
Repeat the Last Command
The . command repeats the most recent change.
For example, if you delete a word and then press:
.
vi repeats the deletion.
This can significantly speed up repetitive editing tasks.
Delete a Word
Use:
dw
to delete from the cursor through the end of the current word.
You can also use:
daw
to delete the word including surrounding spacing where applicable.
Change a Word
Use:
cw
to change the current word.
Vi removes the word and enters Insert mode so you can type the replacement.
Copy and Paste in Practical Editing
Suppose a configuration file contains:
server_name example.com;
You can move to the line and use:
yy
to copy it.
Then use:
p
to paste a duplicate line.
You can edit the duplicated line using commands such as:
cw
This can be useful when creating similar configuration entries.
Display Line Numbers
To display line numbers:
:set number
This can make navigating large files easier.
To turn them off:
:set nonumber
Line numbers are particularly useful when troubleshooting errors that refer to specific lines.
Enable Syntax Highlighting
On vi implementations that support syntax highlighting, you may use:
:syntax on
This can make source code and configuration files easier to read.
Support and appearance can vary depending on the installed editor implementation.
Work With System Configuration Files
Linux administrators often need to modify configuration files.
For example:
sudo vi /etc/hosts
or:
sudo vi /etc/ssh/sshd_config
Before modifying important system files:
Understand the configuration.
Create a backup when appropriate.
Make only necessary changes.
Check syntax if the application provides a validation command.
Test the service after making changes.
A small configuration mistake can prevent a service from starting correctly.
Vi and Remote Server Administration
Vi is particularly useful when managing remote Linux servers.
For example, after connecting through SSH:
ssh user@example.com
you can edit a server configuration:
vi application.conf
This avoids transferring the file to your local computer simply to make a small change.
Vi vs Vim
Vim stands for Vi Improved and extends the original vi editor with many additional features.
Depending on the Linux distribution, the vi command may invoke a traditional vi implementation, Vim, or another compatible editor.
Vim commonly provides additional functionality such as:
Syntax highlighting
Plugins
Multiple windows
Advanced search
More powerful customization
Extended scripting capabilities
The basic vi commands remain useful even when working with Vim.
Common Vi Mistakes
Forgetting the Current Mode
New users often start typing commands while in Normal mode or attempt to navigate while in Insert mode.
Remember:
Esc
returns you to Normal mode.
Not Knowing How to Exit
The most important commands to remember are:
:q
Quit.
:wq
Save and quit.
:q!
Quit without saving.
Accidentally Editing a System File
Be careful when using:
sudo vi
A small mistake in a system configuration file can affect the operation of the server.
Replacing Text Without Reviewing It
Commands such as:
:%s/old/new/g
can modify many occurrences at once.
Review your search and replacement expressions before executing them.
Useful Vi Command Sequence for Beginners
A simple workflow looks like this:
Step 1: Open a file
vi example.txt
Step 2: Start editing
Press:
i
Step 3: Type your content
Enter the required text.
Step 4: Return to Normal mode
Press:
Esc
Step 5: Save and exit
Type:
:wq
and press Enter.
This basic workflow is enough to get started with vi.
Best Practices for Using Vi
Follow these practices when working with vi:
Learn the difference between Normal and Insert modes.
Remember Esc as your way back to Normal mode.
Learn :w, :q, :wq, and :q! first.
Create backups before editing critical configuration files.
Use search instead of manually scanning large files.
Use line numbers when troubleshooting configuration problems.
Review global replacement commands carefully.
Avoid unnecessary use of sudo.
Test configuration changes before restarting production services.
Practice common navigation and editing commands regularly.
Frequently Asked Questions
What is the vi command in Linux?
vi is a command-line text editor used to create and modify text files directly from the Linux terminal.
How do I open a file using vi?
Use:
vi filename.txt
How do I start typing in vi?
Press:
i
to enter Insert mode.
How do I save a file in vi?
Press Esc, then type:
:w
and press Enter.
How do I exit vi?
Use:
:q
if there are no unsaved changes.
How do I save and exit vi?
Use:
:wq
How do I exit vi without saving?
Use:
:q!
Is vi the same as Vim?
They are closely related. Vim is an enhanced implementation inspired by vi and provides many additional features.
Conclusion
The vi command in Linux is an essential terminal-based text editor that allows users to create and modify files without relying on a graphical interface.
Although vi can feel unfamiliar at first, learning its modes and a small set of essential commands makes it highly efficient. Commands such as i, Esc, :w, :q, :wq, dd, yy, p, and /search provide a strong foundation for everyday editing.
For Linux administrators and developers, knowing how to use vi is especially valuable when working with remote servers, configuration files, shell scripts, and production environments.
Once the basics become familiar, you can gradually learn more advanced navigation, search, replacement, macros, and customization features to make terminal-based editing much faster.