How to use vi editor in Linux?

Posted in

How to use vi editor in Linux?

Maaz Bin Asad
Last updated on July 20, 2024

    Vi is a versatile text editor that comes pre-installed on most Linux systems, even embedded ones. Knowing Vi is necessary if you ever need to edit a text file on a device without a more user-friendly text editor. Vi, unlike Nano, is a convenient terminal text editor.

    How to use vi editor in Linux?

    Since Vi editor runs as a terminal program, you must launch it from a terminal pane. There are different purpose for which vi editor can be used:

    1) To create a file


    vi myFirstFile.txt

    It is going to open the vi editor as below:

    You can make edits into the file only after you type "I" key, which acts as a command to enter into insert mode. After you have entered Insert mode, you should be able to see INSERT at the bottom left corner as below:

    After you have written few lines, you need to save your file by first using ESC key to exit the insert mode and then by typing ":wq" which means i want to save the file.

    2) To edit a file

    If you want to open an already created file using vi, you can use the “vi /file/path” instruction. In case the file doesn't already exist, the vi instruction will generate a file with the same name and save it to the designated location. If you want to change a system config file, you need to use sudo to provide administrative permissions to edit the file. For instance, suppose you wanted to update your fstab file, you can use the command - sudo vi /etc/fstab. In case you're using a non-Ubuntu build of Linux which has no support for sudo, use the su command instead. After making the required edits you should save the file.

    3) To view a file

    To view a file, you can use vi <file_path> command which will open the file in the vi editor. You can successfully exit the file after reading using keys : ESC + ":q!". This will exit the editor without making any changes to the file.

    Why to use vi editor?

    Vi is one of the famous editor which is used in nearly every Linux distribution. It runs on all platforms and distributions in the same way. It is easy to use. As a result, millions of Linux users adore it and use it to complete their editing tasks. Nowadays, specialized vi editors are available, the most common of which is VIM, which stands for Vi Improved. Nano, Nvi, Vile, etc. are among the others. It's a good idea to study vi because it has a lot of features and allows you to edit files in a variety of ways.

    After going through the beginner guide to use vi editor, we'll now learn the different modes of vi editor along with the most critical shortcuts for editing every file efficiently.

    Three modes of Vi Editor

    Vi is a modern modular text editor with three modes:

    1. Command Mode

    Vi editor always starts with command mode whenever any file is opened. Any typed character is treated as a command in this mode. As a result, none of those commands will be shown or seen in the vi editor pane. Furthermore, when in this mode, you can copy, paste, navigate through the text, and even erase some characters. Furthermore, in case you are in either of the remaining modes and want to return back to the command mode, simply click the Escape key. Let’s try to open a file using vi editor.

    $ vi file1.txt

    Command Mode

    Below is a list of shortcut which can be used in command mode:

    • dd can be used to delete one line.
    • dw can be used to delete one word.
    • x can be used to delete a character.
    • G can be used to move towards the end.
    • Cursors to navigate inside the file.

    If you are in the command mode, you will be able to copy, paste, cut, select text. Click the v key when positioning the mouse cursor on either side of the string that you would like to copy. Hover your cursor over the text to select it, then click y to copy it or x to cut it. If you want to paste the copied or cutted text, you need to position your mouse cursor to the location where you want the text and click the p key. Let’s try to delete the “Welcome” word by moving the cursor to the starting character on the word and entering the dw command.


    2. Escape Mode

    You must first be in command mode before pressing the colon ":" character to use this mode. In this mode, you can usually type any command after the colon character. If you need to scan the text in the file, you can always use the forward-slash in place of “:”. We can use these commands in escape mode -

    • :wq - This is used to save the changes written on the file and quit it. It’s case-insensitive.

    WQ Command

    • :q! - You can use this to quit without saving the changes.

    Q! Command

    • :q - This is used to simply quit the file.

    Q Command

    • :w - This is used to just save the changes.

    W Command

    • /string - You can use it to find any string in the file. Just hit enter and the cursor will point to the first occurrence of the string in the text file.


    3. Insert Mode

    Each keystroke will be treated as text in the insert mode, and the vi editor will display it on the screen as inserted from the keyboard. This is the mode in which you can begin inserting and concatenating text into the document. The commands for insert mode are -

    • [Escape Key] + i can be used to enter text.
    • [Escape Key] + a can be used to append strings.
    • [Escape key] + A can be used to append words at the end of a line.
    • [Escape] + r can be used to replace characters inside the text file.

    Wrapping Up!

    The vi editor is by far the most widely used Unix text editor, and it is generally included with all Linux distributions. It has three modes: Command, Escape and Insert. The Command mode accepts user instructions, while using the Insert mode, you can modify or edit text. You should be familiar with the commands so that you can focus on the file quickly. You will use this editor to create scripts and edit files if you learn how to use it. We absolutely believe that this article will encourage you to get hands-on with vi editors in Linux. People are also reading:

    Leave a Comment on this Post