Vi Editor in Linux
Whenever you use a Linux system, you will encounter text files and configuration files that you will need to edit. There are a variety of text editors across all Linux distributions to help you for this task. Most, text editor applications come with a GUI. On Linux server machines and other specific distros, only a command-line version of the operating system is available. In that case, a non-GUI based text editor must be used. This blog post introduces you to the command-line based Vi text editor, that is installed on most Linux systems by default.
Available Text Editors
Here is a short list of some commonly used GUI-based text editors:
- Sublime Text
Here is a list of commonly used non-GUI based text editors:
- Vi (currently has an improved version ‘Vim’)
- Emacs (has a command line version too)
- nano editor
In this blog post, we will focus on one non-GUI based text editor Vi that is installed by default on most Linux systems. This is not the case with Emacs or the other available options.
Introducing Vi Text Editor
Nowadays, the improved version of Vi referred to as Vim is installed by default. Vim or Vi, depending on the installation, can be accessed using the command-line tool
vi. Since Linux distributions have Vim installed for use, even when we type the commands using
vi, remember that we are using Vim in the background.
Vi is not a conventional text editor. It has special ‘modes’ to create and edit documents. You can follow along with this blog post on your Linux machine. Soon, you will be able to start using the Vi Editor!
Modes in Vi
The Vi editor operates in two modes:
- Command mode: In this mode, the user instructs Vi about the activity to be performed – inserting text or deleting text or copying text. When you open Vi editor, it opens by default in command mode.
- Input mode: Also referred to as Insert mode. Once the command is received for a specific task, say inserting text, then Vi switches to input mode. Whatever content is typed on the screen, is accepted as input into the file.
To switch between the modes Esc key is used.
Let’s begin exploring these modes by inserting and deleting text from a file sample.txt.
To open a file in Vi, simply type
vi followed by the file name as shown in the following screenshot. If the file does not exist in the current directory, then it is created.
The following screenshot demonstrates how Vi editor appears once it is opened. Whenever Vi opens, the cursor exists at the beginning of the file. Towards the end of the terminal screen, you can find the name of the file.
To begin inserting content into the file, we must first instruct Vi to accept input. This can be done by pressing
i when in the command mode. It indicates Vi to insert text starting at the current position of the cursor. Then Vi switches into input mode and text can be typed. Here the content ‘good’ has been written into the file.
The next task is to save the changes to the file. To do this, Vi must be instructed with a command.
To move into the command mode, press Esc and type the command to save the file – which is
:wq. You can see this command typed at the bottom of the terminal screen. This is represented in the following screenshot. ‘w’ means write to the file and ‘q’ means to quit.
Once the Enter key is pressed, then content is written into the file and we move back into the terminal. To verify that the content has been successfully written into the file,
cat command can be used to view the file’s contents, as shown in the following screenshot. The result is as expected.
Next, let us attempt to add the word ‘morning’ to our file ‘sample.txt’. We want the file to contain the words ‘good morning’. Let us open up the file in Vi again. We know that when Vi opens, the cursor is always present at the beginning of the file. If we use the
i command to insert text, then text would be inserted in the current position of the cursor. This means the content we type will overwrite the existing content. How do we begin appending text to the existing content? Simply by using a different command
A that appends text to the existing content.
Once the command
A is issued, the editor moves into input mode. The remaining word can be typed into the file,
And the work can be saved.
The updated file contents can be viewed using
cat as shown in the following screenshot.
Let’s try editing the file again. This time we have erroneously written some contents to the file. We do not want to save the file’s contents. We simply want to quit the editor. Can you guess which command can be used to quit the editor?
Close! It is
:q. Let’s see what happens when you press Enter.
There is an error message that there is a new character in the buffer that has not been written to the file yet.
To override this error and simply quit the editor, issue the command
:q!. The ! symbol indicates Vi to quit without saving any existing changes in the buffer.
The file’s contents remains the same.
What if we want to delete a file’s contents. There are separate commands in Vi to delete a character, a word and a line. We will discuss how a character can be deleted. Using the
A command, characters G and H have been appended to the file as shown in the following screenshot.
Character H needs to be removed. Since the cursor is under H (note the box around H in the screenshot above), the command to delete a character can be issued. Press ‘Esc’ to ensure you are in command mode and issue
x to delete the character the cursor is under.
From the following screenshot, we can see that H has been deleted successfully.
- We have seen that Vi editor has two modes: command mode and input/insert mode.
- Vi opens by default in insert mode, with the cursor in the beginning of the file
icommand is used to insert content at the position the cursor is in, which by default is the beginning of the file
Acommand is used to append content to the end of the file
xcommand is used to delete the character at the current position of the cursor
Explore the Vi commands for the following tasks:
- Insert five lines into a file
- Copy the 2nd line and paste it after the 5th file
- Delete the 3rd line of the file
- Navigate within the file and insert text at the beginning, middle and end of a line
- Search for a string within a file
Why should cybersecurity professionals know to use Vi Editor?
- During penetration testing/red teaming engagements, you may gain access to a Linux machine without a GUI. Some text/configuration files may have to be created/edited for post-exploitation activities. Knowing how to use the Vi editor comes in handy then.
- During a digital forensics engagement, ability to use the Vi editor helps with the collection and processing of evidence on a Linux machine without a GUI.
- A cloud security professional will not have a GUI when working with Linux instances in the cloud. Knowledge about using Vi editor makes their tasks easier. For example: to configure web server settings.
- A system/network security professional who works with non-GUI based server machines frequently, will benefit from using the Vi editor to configure security settings - for example: to configure firewall rules.
Once you are comfortable with using Vi Editor, you will be better prepared to use Linux systems without a GUI!