Work with the vi editor in Linux – Best Guide Steps

In this article, we want to teach you how to work with the vi editor in Linux.

List of contents that you read in this article:
1. vi editor tutorial in Linux
2. Advanced vi editor commands in Linux
3. Change commands in the vi editor

There are different tools and methods for editing text files on the Linux server. In this article, from the Linux Tutorials series, we are going to get acquainted with the vi editor and teach the vi editor in Linux.
The most widely used of these tools are nano, vi, and VIM, which is essentially an advanced VIM editor of the same type as vi.

How To Work with the vi editor in Linux with Examples

You can use vi to create new files and edit and read existing files.

Now follow the steps below to Work with the vi editor in Linux.

Using Vi Editor in Linux

By entering the following command, you can create a text file and go to its editing environment.

vi orcacore.txt

Note: If the name of the imported file exists, it will open the same file for editing, and if it does not exist, it will create a new file for you.
The result of the above command will be as follows:

work with the vi editor in Linux - simple view


Below the cursor, you will see the “~” Character at the beginning of each line. This character represents the unused line. If a line does not start with the character “~” and there is no writing and is empty, it indicates that the line uses space, tab, or other invisible characters.

So far you have been able to open a file with this editor. But before taking any action, we will explain the following minor but important concepts.

Executable modes of the vi editor in Linux

When working with this editor, you will encounter the following two modes:

Command mode: This mode will enable you to perform administrative tasks such as saving files, executing commands, moving markers, cutting and pasting lines and words, and searching and pasting.
In Command mode, whatever you type will be interpreted as a command.

Insert mode: This mode will enable you to insert text in a file. Everything that is typed in this mode is considered input and is inserted into the file.
vi opens in command mode by default, and you must enter Insert mode to enter text in the file.
You must press the “I” key to enter Insert mode. When in Insert mode, the phrase Insert is displayed at the bottom of the screen as shown below. You must press the ESC key to exit the Insert mode and enter the command mode.

work with the vi editor insert mode


In the image above, you are in the insert mode.

Note: If you do not know which mode you are in, press the ESC key twice and you will be in Command mode. To understand the difference between the two modes or the above mode, it is better to open a file with vi, start typing a few characters, and enter the Command mode.

Exit from the vi editor

The “:q” command is used to exit vi.
First press the ESC key and type “:q” after entering the Command mode.
If you have made changes to the file but want to exit the file without making any changes, enter the command “:q!“.
Exit the editor without saving changes:

exit vi without saving


With the “:w” command you can save the applied changes.
You can combine the save command with the exit command and use the “:wq” command to save and exit the editor.

Note: After hitting the exit commands and saving, press the Enter key.
Keep in mind you can replace the “:wq” command for storage and exit; Use the ZZ command. (Capital Z must be used.)

Tip: To save the file with another name, you can enter the name of the new file after entering the command “:w” as follows:

:w orca2.txt

At this step of How to work with the vi editor in Linux, you learn to navigate inside the editor.

In order to be able to move inside your file in different sections, you must be in Command mode, that is, press the ESC key twice.

Below are some commands to move inside a file:

k command moves the pointer one line up from the current line where the pointer is.

j command moves the cursor one line down from the current line where the cursor is located.

h command moves the cursor to the left by the current position of one character.

l command moves the cursor to the left of the current position by one character.

Consider the following two important points:

1. Vi is case-sensitive. Make sure the letters are uppercase when entering commands.
2. Most commands in vi can specify how many times the command is executed.
For example, the 2j command moves the pointer lower than the current location of the two lines.

Other ways to move inside the file in vi

Remember that you must be in Command or Execution mode to use the commands.
You can also use the following commands to move inside the file.

| Or 0: moves the cursor to the beginning of the line.

$: moves the cursor to the end of the line.

w: moves the cursor to the next word.

b: takes the cursor to the previous word.

(: moves the cursor to the beginning of the current sentence. Note that sentences are separated by a dot character.

): moves the cursor to the beginning of the next sentence.

}: takes the cursor to a previous paragraph.

{: takes the cursor to the next paragraph.

n|: moves the cursor to the nth character from the beginning of the line, in the current line.

1G: Move the cursor to the first line of the file.

G: Move the cursor to the last line of the file.

nG: moves the cursor to the nth line of the file.

n: the cursor to the nth line of the file. After writing this command, you must press Enter.

H: Moves the cursor to the first line of the current page.

nH: moves the cursor to the nth line from the top of the screen.

M: Move to the middle of the page.

L: Scroll to the bottom of the page.

nL: moves the cursor to the nth line at the bottom of the page.

E: moves the cursor to the character before the next space character.

fc: This command moves the cursor to the next c character in the current line. You can write any character instead of character c.

Fc: This command moves the cursor to the previous c character in the current line. You can write any character instead of the c character.

Edit the file in the vi editor

You must be in Insert mode to edit the file. There are several ways to go in Insert mode.

i: Inserts text before the current location of the cursor.

I: Insert the text at the beginning of the current line.

a: Enters text after the current location of the cursor.

A: Inserts text at the end of the current line.

o: The following indicator creates a new line for inserting text.

O: Creates a new line at the top of the cursor to insert text.

Vi editor control commands in Linux

You can use the following useful commands with the control key:

Ctrl+d: lowers the cursor by half the screen

Ctrl+f: lowers the cursor to the size of a full-screen

Ctrl+u: raises the cursor halfway up the page

Ctrl+b: raises the cursor to the size of a full-screen

Ctrl+e: raises the page by one line

Ctrl+y: takes the page one line down

Ctrl+u: Moves the page halfway up

Ctrl+d: moves the page down half a page

Deleted characters in vi editor

The following commands are used to delete lines and words in the opened file.

x: Removes the character below the cursor.

X: Deletes the character before the cursor.

dw: Removes the cursor from the current location to the next word.

d^: Removes the cursor from the current location to the beginning of the line.

d$: Removes the cursor from the current location to the end of the line.

D: Removes the current cursor from the current location to the end of the current line.

dd: deletes the line where the cursor is located.

As mentioned above, you can also specify the number of times to execute commands.
For example, the 2x command removes two characters from under the cursor. The 2dd command removes two lines from the current cursor location.

Copy and paste commands with VI Editor

You can copy words or lines from one place to another. Examples of these commands:

yy command copies the current line.

p command pastes the copied text to the location after the cursor.

P command pastes the copied text before the cursor.

Advanced vi editor commands for Linux

There are advanced commands in vi that make editing easier and allow you to use vi effectively.

J: Join the current line with the next line. If a number is added to the command, it connects the number of lines to that number.

u: command plays the role of undoing.

G^: command If you press the control key at the same time as the G key, it displays the current file name and its status.

~: Change large or small characters under the cursor.

<<: shifts the current line one unit to the right.

>>: shifts the current line by one unit to the left.

:f: Show your position in the file in% and display the file name.

The filename statement after the “:f” command changes the file name to the filename.
Phrase filename after the “:w” command writes the file to a file called filename.
Filename statement after the “:e” command Opens another file named filename.
dire-name statement after the “:cd” command changes the current directory to the written directory.

:e#: Opened to move between two files.

:n: If you have opened several files with vi, each time you hit “:n” you will be redirected to the next file.

:p: If you have opened several files with vi, each time you press “:p” you will be redirected to the previous file.

:N: does the same thing as the “:p” command.

:r: If after writing this command you write the name of another file that you opened with vi, it will insert the contents of that file after the cursor.

:nr: If you give a file name after this command, it will read that file and insert it in the file after the nth line.

Vi editor changes commands

In this part of How to Work with the vi editor in Linux, we show you the change commands.

cc: clears the contents of the current line and puts you in insert mode.

r: replaces the character below the cursor with the character entered after the r command and puts you in command mode.

R: By hitting this command and the desired character after it, you can start with the character below the cursor and replace any character you want with the entered character. You must press the ESC key to stop the placement.

S: clears the current line, replaces it with typed text, and puts you in insertion mode.

Word and character search commands in the vi editor

In the vi editor, you can search in 2 ways. Search a string, search a character
To search for strings of two commands “/” and “?” Is used. When you type the above commands will appear at the bottom of the page and then you must enter the desired string.

The difference between the two commands is in the direction of search. Executes the command “/” search from the cursor forward or down the file. And the command “?” Searches the back or top of the file.

The n and N commands repeat the previous searches. Suppose you searched with a command “/” word. After the first result, each time you press the n key, the search will continue in the direction of the “/” command, and if you enter the N command, it will continue the search in the opposite direction of the “/” command.

Some characters have a special meaning when using search commands. And are written after the desired phrase and are considered part of the searched phrase.

^ Character: Search at the beginning of the line. This character must be searched before the phrase.

character . : If the phrase is followed by a character. Write will show you any word that has that phrase.

* Character: After the search command, type the phrase you are looking for, and then type the * character. Any word that has at least one character of the phrase written before * will be presented as a search result.

The $ character searches for the phrase written at the bottom of the line. This character is written at the end of the search term.

Set commands in the vi editor in Linux

With these commands, you can change the appearance of the vi page.

To use this command, you must be in command mode, and after typing the set: command, type one of the following options:

nu writes the line number on the left at the beginning of each line.

ic, It does not matter if the letters are uppercase or lowercase.

ro converts the file to a read-only file.

term Prints the terminal type.

bf ignores control characters from the input.

Execute Linux commands inside vi

At this step of How to work with the vi editor in Linux, you learn to execute Linux commands inside the editor.

The vi editor can run Linux commands from within it. To do this, you have to write the phrase “:!” in grammatical mode and then write the desired command.

For example, when you want to save a file with your desired name, by typing the ls command after the phrase “:!” You can see if there is a file with the desired name or not.

The result of the ls command will be displayed on the page. To exit the ordered result and return to the vi environments, you can press the desired key on the keyboard.

For example, to exit the top command, you must press the q key. You will not be able to exit by pressing the other keys.

Insert text in Vi Editor

The “:s/” command can be used for placement. How to use this command is as follows.

insert text


In this command, write the word you want to replace with the word new_word instead of old_word.
The g character also causes the line on which the cursor is located to make a position-matching match anywhere.

Important Notes

To avoid problems when working with the vi editor in Linux, pay attention to the following points:

You have to be in command mode to work with commands. Press the ESC key twice to make sure you are in command mode.

When working with commands, you must pay attention to the uppercase and lowercase letters.

To insert text in the file, you must be in insert mode.

Conclusion

You can use Vi Editor in most Linux distributions like Fedora, Centos 7, Ubuntu 20.04, Debian 10, etc.

We hope you enjoy this article about how to work with the vi editor in Linux.

You may like these articles:

Cron in Linux

How to Kill a Process in Linux

Environment Variables in Linux

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!