The “vim” Command in Linux [8 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The vim command in Linux is a powerful, faster text editor with advanced features. However, it isn’t suitable for novices. Even though, the nano command is easy to use but it isn’t efficient for complicated tasks. That is why professionals or advanced users look for another option and the vim command in Linux is a contender for that. It has special features like GUI mode, autocompletion, color schemes, and auto-completion. Moreover, searching and replacing, undoing, and redoing is comparatively easy in the vim command.

A. Description

The “vim” of the vim command in Linux is short of “Vi Improved”. Vi is a part of the UNIX operating systems. A large number of commands and features make Vi a powerful, versatile and efficient tool. The vim command in Linux is an upgraded form of Vi. The normal mode and insert mode of the vim command comes from the Vi text editor. However, the vim command has other modes as well like visual mode, command-line mode, Ex mode where each mode provides different functionality.

B. Syntax

The vim command in Linux can take multiple options and open multiple files. Now, the syntax of the vim command goes like the following

vim [OPTION]... [FILE]...
Note: In the syntax above, the OPTION and FILE enclosed by square brackets (“[]“), followed by three dots, denotes that multiple options and files can be provided at the same time.

C. Options

The vim command in Linux has a lot of options, as I have mentioned earlier. To check them out, execute the following command in the command prompt:

man vim

Here I have listed some of the useful options for you in the following:

Useful Options

  • -t, positions cursor based on a tag.
  • -b, enables binary mode.
  • -D, goes to debugging mode.
  • -m, makes modifying files disabled.
  • -R, opens a file in read-only mode.
  • -r, enables recovery mode.
  • -v, starts the vim command in Vi mode.
Note: The options in Linux CLI (Command Line Interface) are all case-sensitive, use them as they are listed here.

How to Install the “vim” Command in Linux

Firstly, I will install the vim command in Linux as it doesn’t come by default in most systems. It is a simple four-step process. To install the vim command, follow the step below:

  1. At first, open a Terminal in your Ubuntu.
  2. Copy the following command in the command prompt and hit ENTER:
    sudo apt-get install vim
  3. Provide your password if necessary and press the ENTER button again.
  4. Write the following if you get the message “Do you want to continue” and hit ENTER:
    y

    Note: If it doesn’t work, run the command from below and start from step 2:

    sudo apt-get update

In the image, you can see my system is installing the vim command in Linux. Also, it is asking for my permission whether I really want to install it or not.Installing the vim command in Linux

Practical Examples of the “vim” Command in Linux

In this part, I will show you some practical examples of the vim command in Linux. For demonstration purposes, I will use the same file named “myfile.txt” throughout all the examples. I have also provided the text I have used so that you can follow along with all the steps. Moreover, to learn the basic functionality of the vim command in a very short time, you can use the following command:

vimtutor

Example 1: Open a File in Vim

Now I will open a file in the vim text editor using the vim command in Linux. Remember, you can open both existing and non-existing files. If you open a non-existing file, the vim command will create a file by this name, similar to the nano command. Now do the following to open a file named “myfile.txt”:

  1. Launch an Ubuntu Terminal first.
  2. Insert the following command to open a file:
    vim myfile.txt
  3. Press ENTER from the keyboard.

You will get an output like this. The file is open in normal mode. You cannot write on the file yet. I will show you how to enable insert mode in the next example. In the end, you will see the name of your file. If it is created for the first time,  you will see “New” as well.Opening a file using the vim command in LinuxI executed the following command to see whether the file has been really created or not:

ls

See, there is a file named myfile.txt in the home directory of my computer.Showing myfile.txt file


Similar Readings


Example 2: Enable Insert Mode in the “vim” Command in Linux

To write anything on a file using the vim command in Linux, you need to go into the insert mode first. To enable insert mode, follow the steps below:

  1. Go to your Ubuntu and open a Terminal.
  2. Insert the following command to open a file and press ENTER:
    vim myfile.txt
  3. To enable insert mode, type “I” from your keyboard.

You will notice that now you can now write on the Terminal. Moreover, in the end, you will see “–INSERT–” which indicates that you are in insert mode.Enabling the insert mode in the vim command in Linux

Example 3: Save a File and Exit

If you write something on a file, you would want to save it and exit, right? Don’t worry; now I will show you how to save and exit at the same time in the vim command in Linux. You should remember that in the vim command, the colon (“:”) is used to write any commands and you need to be in the normal mode for that. Now do the following to save and exit:

  1. At the beginning, open an Ubuntu Terminal.
  2. Then copy the following command in the command prompt and press ENTER to open a file:
    vim myfile.txt
  3. Press “I” on your keyboard and write the following:
    Welcome to Linuxsimply!
  4. Press “ESC” on your keyboard to shift into normal mode.
  5. Now insert the following to save and exit at the same time:
    :wq
    Note: If you want to save only, type “:w”, or only to exit type “:q”.

I used the command “:wq” to save and exit simultaneously. The colon is used to tell the vim command in Linux that I am going to enter a command now. The “w” is for saving the current file and the “q” is exiting from the vim editor.Deleting and exiting in the vim command in Linux


Similar Readings


Example 4: Undo and Redo in the “Vim” Command in Linux

It is impossible to imagine a text editor with undo and redo features. All of us use these features on regular basis. The vim command in Linux also comes with those features. In this example, I will show you how to utilize those features. Firstly, I will write a line. Then undo it which will delete the line. Again, I will make the line reappear by redoing it. To undo and redo in the vim text editor, do the following:

  1. Firstly, launch a Terminal in your Ubuntu.
  2. Copy the following command to open a file and press ENTER:
    vim myfile.txt
  3. Press “I” and write the following line on the Terminal:
    Welcome to Linuxsimply!
  4. Press “ESC” on your keyboard to enable normal mode.
  5. To undo, press “U”.
  6. To redo, press CTRL + R.

Firstly, you can see the “Welcome to Linuxsimply!” line.Showing a line before undoingAfter undoing, the line is gone. The “1 change” at the end denotes that one change has been undone.Result of undoingAgain, after redoing, the line has reappeared again. Moreover, the “1 change” at the end denotes that one change has been redone.Result of redoing

Example 5: Search a Word

Searching is an important feature for a text editor. Now you will learn how to search a word in the vim command in Linux. To search for a word, follow the steps below:

  1. Go to Ubuntu and open a Terminal.
  2. Write the following command and press ENTER to open a file:
    vim myfile.txt
  3. Press “I” and copy the line in the Terminal:
    Welcome to Linuxsimply!
  4. Press “ESC” from your keyboard to enter normal mode.
  5. Insert the following line to search word “Welcome”:
    /Welcome
    Note: The search is casesensitive.

Here I searched for the word “Welcome” and see, the word got highlighted. If I press ENTER, the cursor will the at the beginning of that word.Searching in the vim command of Linux

Example 6: Set Numbers and Go to a Particular Number Using the “Vim Command in Linux

In this example, I will set a number for each line and go into a particular line. Now to set numbers and go to a specific number, do the following:

  1. At first, launch an Ubuntu Terminal.
  2. Type the following line in the command prompt to open a file and hit ENTER:
    vim myfile.txt
  3. Press “I” from the keyboard to enable insert mode and then write the following lines:
    Welcome to Linuxsimply!
    At LinuxSimply.com, we are committed to delivering high-quality content that is easy to understand.
    Our articles are carefully researched and written in a clear, concise style.
  4. Press “ESC” to enable normal mode.
  5. Write the following line to set numbers for each line and press ENTER:
    :set number
  6. To go to a particular line (line 2 for instance), insert the following and hit ENTER:
    :2

There are some lines in the first image and I want to number them.Showing lines which will be numberedI followed the first five steps first. As a result, all the lines are numbered now. You can see the numbers on the left side.Numbering using the vim command in LinuxFinally, I followed step 6 to go to the second line. As you can see, the cursor is at the beginning of the second line.Cursor position after numbering


Similar Readings


Example 7: Delete a Line

Now I will delete a line in the vim command in Linux. Deleting a line is quite simple in the vim command. To delete a line, follow the steps below:

  1. Go to your Ubuntu and launch a Terminal for the first time.
  2. Insert the command below to open a file and press ENTER:
    vim myfile.txt
  3. Press “I” and copy the following lines:
    Welcome to Linuxsimply!
    At LinuxSimply.com, we are committed to delivering high-quality content that is easy to understand.
    Our articles are carefully researched and written in a clear, concise style.
  4. Now press “ESC” on your keyboard to enable the normal mode.
  5. Use the arrow keys to move the cursor and place it on a line you want to delete.
  6. Type the following to delete that line:
    dd

As you can see, I placed the cursor on the last line as I wanted to delete it.Positioning the cursorAnd see, that line is deleted.Deleting a line

Example 8: Copy and Paste Using the “Vim Command in Linux

At the end, I will show you how to copy and paste in the vim command in Linux as you will be doing it a lot. In addition to that, you will use the visual mode for the first time. In this example, I will copy a line and paste it into the second line. Now to copy and paste in the vim command, do the following:

  1. Open a Terminal in your Ubuntu at first.
  2. Write the following command in the command prompt to open a file and press ENTER:
    vim myfile.txt
  3. Then type “I” and copy the following line:
    Welcome to Linuxsimply!
  4. Press the “ESC” button and “V” to enter into the visual mode.
  5. Use the arrow keys (or mouse) to highlight what you to copy.
  6. To copy, type “Y”.
  7. Then press the “ESC” and “I” to enter into the insert mode and then place your mouse cursor.
  8. Finally, go into normal mode by pressing “ESC” and type “P” to paste.

In this example I copied “Welcome to Linuxsimply!” and the first image is showing how I highlighted it while I was in visual mode.Enabling the visual mode in the vim command in LinuxAnd in the second image, you can see that I first line is successfully copied to the second line.Displaying copied line

Conclusion

In this article, I have tried to show some of the basics of the vim command in Linux. However, it is a huge topic to cover in a single article. Hopefully, this article will make your journey in the vim command in Linux easier.


Similar Readings

Rate this post
Walid Al Asad

Hello Everyone! I am Walid Al Asad. Currently, I am working at a tech company named Softeko as a Linux Content Developer Executive. I live in Dhaka, Bangladesh. I have completed my BSc. in Mechanical Engineering from Bangladesh University of Engineering and Technology (BUET). You can find me on LinkedIn, and ResearchGate. Read Full Bio

Leave a Comment