How to use vi editor in Linux?

Posted in

How to use vi editor in Linux?

Maaz Bin Asad
Last updated on June 8, 2022

    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, a convenient terminal text editor, does not hold your hand and displays a list of keyboard shortcuts. We can consider it as a modern modular text editor with three modes: insert, escape, and command. Since Vi editor runs as a terminal program, you must launch it from a terminal pane. 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. 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. Here are some of the reasons why it is such a famous editor. It's 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.

    How to use vi editor in Linux?

    In this guide, we'll teach you how to use the Vi editor, as well as the most critical shortcuts for editing every file efficiently. The Command mode, Escape mode, and the Insert mode, which we will explore today, are the three primary modes available in Vi.

    1. Command Mode

    When you first open the vi editor, the command mode is the very first mode you can use. 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 Currently, the file is empty. If no such file exists, one will be created automatically. We can use these shortcuts 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. Welcome

    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