What is Vi?
In the Linux operating system you need a text editor to open and edit a configuration file or any other file.
In Linux - where we mainly use CentOS - you have several well-known text editors, but the most important text editor that is always present by default is Vi . In fact it is always included in the installation of the Linux operating system and therefore you don’t need to install it.
If it is not present you can install it with the following commands vi text editor:
Install CentOS Vi
yum install vim-enhanced -y
Ubuntu Vi install
sudo apt-get install vim
install Debian Vi
sudo apt install vim
The main reasons why it is best to work with the Vi text editor are:
- Vi has many similarities to commands in the operating system
- Very comprehensive yet easy to use
- Is used in every howto on the internet
- Often installed by default with the Linux OS
Basic functions Vi text editor
Although Vi is very user-friendly, you need to know some basic things to be able to use it yourself. Below we explain some of the most common basic functions, and once you have mastered them you will soon be able to use the more advanced functions.
Open file with Vi
First of all, we’ll start by opening a file. This can be a new (empty) file or an existing file you want to view or modify.
Open or create new / empty file with Vi
vi /path/to/journey/new/file
Open existing file with Vi
vi /path/to/existing/file
Once you enter this command, the vi editor will open your chosen file and, in the case of an existing file, display its contents.
Different modes in Vi
So once you have opened the file with Vi you will see the contents of the file you are opening. You can’t edit or modify the file right away, because you’re in the so-called : “Command mode”.
Command mode The Command Mode means that you can enter commands to choose what you want to do. If you don’t enter anything you can see the content and move your cursor through it, but you can’t enter, edit or delete anything.
Insert mode As soon as you want to edit something, you have to choose the so-called : Insert mode. The name says it all, but this is the mode where you can insert / enter and modify or delete things.
Possible commands in Command mode Vi
There are several possible commands or keys in command mode that you can use to get started. The most important ones I will list and explain below:
Key/Command Explanation | |
---|---|
The i ensures that you go to the insert mode. This means that you can add, edit or delete cursors in your file where you have them. | |
This means that the cursor is placed at the beginning of a new line underneath the cursor. | |
This closes vi and the file you opened. **Note that this is without saving the file. | |
The escape button will close vi and the open file, but the file will be saved. | |
The escape button takes you out of insert mode and back into command mode. You can then choose what to do next. |
Navigating through the file in Vi
When you open a file with a lot of content, it is difficult to scroll through easily with your cursor. You will find that it is very slow and not conducive to your productivity.
Fortunately Vi has some nice solutions built in that make it a lot easier and above all faster.
With the commands below you can navigate through the file in Vi much easier. Please note that these commands only work in command mode, so you should not be in insert mode.
-key/command | explanation |
---|---|
This will go directly to the end of the file. | |
This command is used to go directly to the beginning of the file. | |
This command sets the line numbering in the file so that each line number is displayed. | |
This command will jump directly to the line number you entered. Suppose you want to see line 10, then enter this command | |
/This command searches for the first occurrence of the word you wish to find in your file. | |
The first line of the command is the line number of the cursor. | |
The following table lists the different types of data, and their corresponding data. | |
The following table gives an overview of the different types of data in the database. |