The default editor that comes with the Linux/UNIX operating system is called vi (visual editor). Using vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file. The advanced version of the vi editor is the vim editor.
How to Open VI Editor
To open vi editors, we just need to type the command mentioned below.
vi [file_name]
Here, [file_name] = this is the file name we want to create or to open the pre-existing file.
Example 1: Creating a new file with `file_name` = geeksforgeeks
vi geeksforgeeks

to create file we used vi geeksforgeeks
As we can see, we have created a new file geeksforgeeks in vi editor, which has no content in it.
Example 2: Opening a preexisted file with `file_name` = jayesh
vi jayesh

Opened already existed file with vi jayesh
As we can see, we have opened a file_name = Jayesh, that already existed in vi editor.
Modes of Operation in the vi editor
There are three modes of operation in vi:

Here are three modes of operations on the vi editor
Vi Command Mode :
When vi starts up, it is in Command Mode. This mode is where vi interprets any characters we type as commands and thus does not display them in the window. This mode allows us to move through a file, and delete, copy, or paste a piece of text. Enter into Command Mode from any other mode, requires pressing the [Esc] key. If we press [Esc] when we are already in Command Mode, then vi will beep or flash the screen.
Vi Insert mode:
This mode enables you to insert text into the file. Everything that’s typed in this mode is interpreted as input and finally, it is put in the file. The vi always starts in command mode. To enter text, you must be in insert mode. To come in insert mode, you simply type i. To get out of insert mode, press the Esc key, which will put you back into command mode.
Vi Last Line Mode (Escape Mode):
Line Mode is invoked by typing a colon [:], while vi is in Command Mode. The cursor will jump to the last line of the screen and vi will wait for a command. This mode enables you to perform tasks such as saving files and executing commands.
Linux vi Commands and Examples
NOTE: vi editor in Linux is a case-sensitive.
How to insert in vi editor in Linux :
To enter in insert mode in vi editor in Linux we just need to press `i` on our keyboard and we will be in insert mode. we can just start entering our content. (Refer to screenshot mentioned below).

opened a file and pressed `i` to write content
Moving within a File (Navigation) in Vi Editor :
To move around within a file without affecting text must be in command mode (press Esc twice). Here are some of the commands that can be used to move around one character at a time.
| Commands |
Description |
| `k` |
Moves the cursor up one line. |
| `j` |
Moves the cursor down one line. |
| `h` |
Moves the cursor to the left one-character position. |
| `l` |
Moves the cursor to the right one-character position. |
| `0` |
Positions cursor at beginning of line. |
| `$` |
Positions cursor at end of line. |
| `W` |
Positions cursor to the next word. |
| `B` |
Positions cursor to previous work. |
| `(` |
Positions cursor to beginning of current sentence. |
| `)` |
Positions cursor to beginning of next sentence. |
| `H` |
Move to top of screen. |
| `nH` |
Moves to nth line from the top of the screen. |
| `M` |
Move to middle of screen. |
| `L` |
Move to bottom of screen. |
| `nL` |
Moves to nth line from the bottom of the screen. |
| Colon followed by a number position |
The cursor on the line number is represented by the number after the colon. For example, “:10” positions the cursor on line 10. |
There are the following useful commands which can be used along with the Control Key. These commands are helpful in saving time by navigating quickly in a file without manually scrolling.
| Command |
Description |
| CTRL+d |
moves the screen down by half a page. |
| CTRL+f |
moves the screen down by a full page. |
| CTRL+u |
moves the screen up by half a page. |
| CTRL+b |
moves the screen up by a full page. |
| CTRL+e |
moves the screen up by one line. |
| CTRL+y |
moves the screen down by one line. |
| CTRL+I |
redraw the screen. |
Inserting and Replacing text in Vi Editor :
To edit the file, we need to be in the insert mode. There are many ways to enter insert mode from the command mode.
| Command |
Description |
| i |
Inserts text before current cursor location |
| a |
Insert text after current cursor location |
| A |
Insert text at the end of current line |
| o |
Creates a new line for text entry below cursor location and switches to insert mode. |
| O |
Creates a new line for text entry above cursor location and switches to insert mode. |
| s |
Replaces single character under the cursor with any number of characters and switches to insert mode. |
| R |
Overwrites text from the cursor to the right, without switching to insert mode. |
Deleting Characters and Lines in Vi Editor :
Here is the list of important commands which can be used to delete characters and lines in an opened file.
| Command |
Description |
| `X` (Uppercase) |
Deletes the character before the cursor location. |
| `x` (Lowercase) |
Deletes the character at the cursor location. |
| `Dw` |
Deletes from the current cursor location to the next word |
| `d^` |
Deletes from current cursor position to the beginning of the line. |
| `d$` |
Deletes from current cursor position to the end of the line. |
| `Dd` |
Deletes the line the cursor is on. |
Copy and Paste in Vi editor in Linux:
Copy lines or words from one place and paste them in another place by using the following commands.
| Commands |
Description |
| Yy |
Copies the current line. |
| 9yy |
Yank current line and 9 lines below. |
| p |
Puts the copied text after the cursor. |
| P |
Put the yanked text before the cursor. |
Save and Exit in Vi Editor in Linux:
Need to press [Esc] key followed by the colon (:) before typing the following commands:
| Commands |
Description |
| q |
Quit |
| q! |
Quit without saving changes i.e. discard changes. |
| r [file_name] |
Read data from file called [file_name] |
| wq |
Write and quit (save and exit). |
| w |
Write to file called [file_name] (save as). |
| w! |
Overwrite to file called [file_name] (save as forcefully). |
| !cmd |
Runs shell commands and returns to Command mode. |
Searching and replacing in (ex-Mode) in Vi Editor (Linux):
vi also has powerful search and replacement capabilities. The formal syntax for searching is:
:s/string
For example, suppose we want to search some text for the string “geeksforgeeks” Type the following and press ENTER:
:s/geeksforgeeks
Input:

:s/string
Output: finding the first match for “geeksforgeeks” in text will then be highlighted.

“geeksforgeeks” in text will then be highlighted.
The syntax for replacing one string with another string in the current line is:
:s/pattern/replace/
Here “pattern” represents the old string and “replace” represents the new string. For example, to replace each occurrence of the word “geeks” in a line with “geeksforgeeks” type:
:s/geeksforgeeks/gfg/
Input:

:s/geeksforgeeks/gfg/
Output:

Output:
The syntax for replacing every occurrence of a string in the entire text is similar. The only difference is the addition of a “%” in front of the “s”:
:%s/pattern/replace/
Thus repeating the previous example for the entire text instead of just for a single line would be:
:%s/gfg/geeksforgeeks/
Block delete commands in (x mode) in Vi Editor :
need to press ESC and then commands will be followed by colon (:).
| Command |
Description |
| :1d |
delete the line 1. |
| :1,5d |
deletes the lines from 1 to 5. |
| :10,$d |
deletes lines from 10th line to the last of the file.
($ means last line of file).
|
| :.,$d |
deletes lines from present line to that last line. (. means the present line). |
| :.-3,.d |
deletes the lines from present line and above 2 lines
(Deletes 3 lines including the cursor line).
|
| :.,.+4d |
deletes the lines from present cursor line followed 3 lines (total 3 lines). |
| :16 |
deletes the 16 line of the file. |
Block copy command in (x mode) in Vi Editor :
need to press ESC and then commands will be followed by colon (:).
| Command |
Description |
| :1,5 co 10 |
copies the lines from 1 to 5 after the 10th line. |
| :1,$ co $ |
copies the lines from 1 to last line after last line. |
| :.,.+5 co 8 |
copies lines from present to 5 lines after 8th line. |
| :-3,. co 10 |
copies the lines from present cursor line above 3 lines after 10th line. |
Block moving commands in (x mode) in Vi Editor :
need to press ESC and then commands will be followed by colon (:).
| Command |
Description |
| :1,5 mo 9 |
moves line from 1 to 5 after 9th line. |
| :1,$ mo $ |
moves lines from 1 to $ after last line. |
| :.,.+5 mo 10 |
moves line from present line ans next 5 lines after 10th line onwards. |
| :.-3,. mo 10 |
moves present line and above 3 lines after 10th line. |
Basic vi Command and Working – FAQs
What is the difference between vi and vim editor?
Vi is the original text editor that was created in 1976, while Vim (Vi Improved) is an improved version of Vi that was released in 1991. Vim offers additional features and functionality compared to Vi, such as syntax highlighting and mouse support.
How do you search for a word in vi editor?
To search for a word in vi editor, you need to first ensure that you are in command mode. Then
- type the command ‘/searchterm’ and press enter.
- This will search for the first occurrence of ‘searchterm’ in the document.
- To find the next occurrence of the word, type the command ‘n’.
How can you delete a line in vi editor?
Go to command mode (press ESC) then press `dd`. This will delete the complete line in which our cursor is there.
What is the purpose of the command mode in vi editor?
The command mode in vi editor performs commands and navigate through our document or content in file. In this mode, you can move the cursor, delete text, search for text, and save the document.
Conclusion
Vi editor is a powerful and widely used text editor in UNIX and Linux operating system. It allows us to create, edit and manage text files. Vim is the advanced version of vi editor. There are three modes in vi: Command mode, Last Line Mode and Insert Mode. We have also discussed many options in the above context. Overall, we can say that it is a powerful tool and it is useful for both beginners and experienced users.
Master DevOps and also get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.
The most sought-after Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!
Similar Reads
unexpand command in Linux with Examples
To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default converts each space into tabs writing the produced output to the standard output. Syntax $unexpand [OPTION]... [FILE]...where, OPTION: Represents the various
3 min read
uniq Command in Linux with Examples
The uniq command in Linux is a command-line utility that reports or filters out the repeated lines in a file. In simple words, uniq is the tool that helps to detect the adjacent duplicate lines and also deletes the duplicate lines. uniq filters out the adjacent matching lines from the input file(tha
7 min read
until command in Linux with Examples
until command in Linux is used to execute a set of commands as long as the final command in the 'until' Commands has an exit status that is not zero. It is mostly used where the user needs to execute a set of commands until a condition is true. Syntaxuntil COMMANDS; do COMMANDS; donewhere, CONDITION
2 min read
Linux | Uptime command with examples
It is used to find out how long the system is active (running). This command returns set of values that involve, the current time, and the amount of time system is in running state, number of users currently logged into, and the load time for the past 1, 5 and 15 minutes respectively. Linux uptime S
5 min read
How to add User in Linux | useradd Command
useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a native binary compiled with the system whereas adduser is a Perl script that uses useradd binary in the
5 min read
usermod command in Linux with Examples
usermod command or modify user is a command in Linux that is used to change the properties of a user in Linux through the command line. After creating a user we have to sometimes change their attributes like password or login directory etc. so in order to do that we use the Usermod command. The info
4 min read
username Command in Linux With Examples
Linux as an operating system holds the capabilities of handling multiple users. So it is important to keep a check on the users and their related information in order to maintain the integrity and security of the system. Whenever a user is added its information is stored in the "/etc/passwd" file. I
3 min read
users command in Linux with Examples
users command in Linux system is used to show the user names of users currently logged in to the current host. It will display who is currently logged in according to FILE. If the FILE is not specified, use "/var/run/utmp". "/var/log/wtmp" as FILE is common. Syntaxusers [OPTION]... [FILE]where, OPT
2 min read
How to Delete User in Linux | userdel Command
Managing user accounts is an essential aspect of Linux system administration. Understanding how to delete a user in Linux is crucial, whether you need to remove an unused account, revoke access for a departing employee, or clean up your system for security reasons. Here, we will explore the 'userdel
6 min read
vi Editor in Linux
The default editor that comes with the Linux/UNIX operating system is called vi (visual editor). Using vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor to just read a text file. The advanced version of the vi editor is the vim editor. Table of C
9 min read
vmstat command in Linux with Examples
vmstat command in Linux/Unix is a performance monitoring command of the system as it gives the information about processes, memory, paging, block IO, disk, and CPU scheduling. All these functionalities makes the command vmstat also known as virtual memory statistic reporter. The very first report pr
3 min read
vnstat command in Linux with Examples
vnstat is a command-line tool in Linux that is generally used by system administrators in order to monitor network parameters such as bandwidth consumption or maybe some traffic flowing in or out. It monitors the traffic on the system's network interfaces. Installing vnstat on LinuxIn case of RedHa
4 min read
w command in Linux with Examples
The 'w' command in Linux gives us important information about who is currently using the computer, how much the computer is being used, and what programs are running. It's a handy tool for people who take care of computer systems, as it helps them keep an eye on what users are doing, how much of the
3 min read
wall command in Linux with Examples
wall command in Linux system is used to write a message to all users. This command displays a message, or the contents of a file, or otherwise its standard input, on the terminals of all currently logged in users. The lines which will be longer than 79 characters, wrapped by this command. Short line
3 min read
watch command in Linux with Examples
The 'watch' command in Linux is a powerful utility that allows you to execute a command periodically, displaying its output in fullscreen mode. It is particularly useful for monitoring the output of commands that change over time, such as system resource usage or server status. By default, 'watch' r
4 min read
wc command in Linux with examples
wc stands for word count. As the name implies, it is mainly used for counting purpose. It is used to find out number of lines, word count, byte and characters count in the files specified in the file arguments.By default it displays four-columnar output.First column shows number of lines present in
6 min read
Wget Command in Linux/Unix
Wget is the non-interactive network downloader which is used to download files from the server even when the user has not logged on to the system and it can work in the background without hindering the current process. GNU wget is a free utility for non-interactive download of files from the Web. It
6 min read
whatis Command in Linux with Examples
whatis command in Linux is used to get a one-line manual page description. In Linux, each manual page has some sort of description within it. So, this command search for the manual pages names and show the manual page description of the specified filename or argument. Syntax of the `whatis` command
5 min read
How to Display Path of an Executable File in Linux | Which Command
In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In th
6 min read
while Command in Linux with Example
The "while" command is a powerful tool in the Linux shell that allows you to execute a series of commands repeatedly based on a given condition. It is commonly used in shell scripts to create loops, where a block of code is executed as long as a particular condition remains true. Basic Syntax of Whi
6 min read
How to List Current Logged-In Users in Linux | who Command
Linux provides several command-line tools to manage and monitor user activity. One common administrative task is identifying and listing the currently logged-in users on a Linux system. In this article, we will delve into multiple ways to achieve this, exploring commands such as who, user, w, last,
6 min read
How to Display the current Username in Linux | whoami Command
Imagine you're working on your computer and forget who you're logged in as. In Linux, there's a special trick called "whoami" that's like asking "Hey computer, who am I right now?" This article explains how this simple command works and helps you remember who's in charge! Don't worry, it won't be fu
4 min read
write command in Linux with Examples
The `write` command in Linux facilitates the users in sending the messages directly to other logged-in users' terminals. For using this command, try on to type `write username` and then type your message. After typing your message, press `Ctrl+D` to end the session. This command is useful for quick
6 min read
xargs command in Linux with examples
xargs is a Unix command which can be used to build and execute commands from standard input. Importance: Some commands like grep can accept input as parameters, but some commands accept arguments, this is a place where xargs came into the picture. Syntax of `xargs` command in Linuxxargs [options] [c
5 min read
xdg-open command in Linux with Examples
xdg-open command in the Linux system is used to open a file or URL in the user’s preferred application. The URL will be opened in the user’s preferred web browser if a URL is provided. The file will be opened in the preferred application for files of that type if a file is provided. xdg-open support
2 min read