Chapter 10 – Vim

After having spent time learning the most important commands that we need to perform essential tasks on Linux, you should realize by now that using the command line interface on its own has its limitations.

We cannot write scripts, change configurations, automate tasks, and more, from a prompt. For this, we have to venture outside the CLI, and onto a text editor, and more precisely, onto Vim.

Vim isn’t your typical Notepad, so don’t take this chapter as a slight.

First Steps With Vim

Vim is available in almost all Linux distributions. So, you don’t have to install it on your machine.

However, in some cases, especially when you have a lightweight distro, Vim may not be readily available to you by default. In that case, you can still install it using your distribution’s package manager.

For instance, if you are running a Debian-based distribution, then you can simply run: apt-get install vim and that should do it.

Starting vim

To open Vim, simply type the vim command, followed by the file that you want to edit.

$ vim myfile.txt

If the provided filename does not exist, vim will create an empty file with that name.

This is what you should see when you run the above command:

Vim

Quitting Vim

Now, before we start typing and editing text, the first and most important thing you need to know about Vim is how to exit from it.

And no, we are not going to close it using the X button at the top right of the window. This isn’t the right way to do it. Besides, this option is only possible in graphical desktop environments. And in many cases, you might find yourself using Vim in a non-graphical terminal, in which there are no windows, and therefore no X buttons.

So, to gracefully exit from Vim in the correct way, you should press the colon key, followed by q (as in quit) :q. You should view the keys that you’re typing at the bottom of your screen as shown below.

After that, simply press enter, and you should be out of Vim and back to your command line prompt.

By typing the above command, the changes you made to the file will not be saved. If you wish to save and exit, then instead of :q, you should type :wq, which is a combination of :w (as in write), and :q.

Vim modes

Note that when you typed :q in the previous example, it did not get inserted in the content of the file as you would expect from a text editor.

This is because you can only type directly in the content of a file in Vim when you are in the insert mode. But Vim opens by default in the normal mode.

So, when Vim is in the normal mode, you can not type directly into the file, but you can do a lot of other things, like navigating within its content using certain key presses.

To move from the normal mode to the insert mode, you can do that by pressing the i key. And, whenever you want to go back to the normal mode (not just from the insert mode, but from any other mode), then simply press the escape key esc.

Now, when you are in the normal mode, and you press the colon key :, you switch to the command mode. This is what we did before in our previous example. When we pressed :q (or :wq), what we did was, switching first to the command mode by pressing the colon, and then, we ran the command q to exit from Vim.

To summarize this section, we have learned about three modes in Vim: The Normal, Insert, and Command Modes. There are other modes of course, but for now, let’s focus on these, and we’ll get the chance to cover the others later in this chapter.

Insert Mode

As mentioned earlier, you can access the insert mode by pressing the i key. Once you’re there, you can edit your text directly as you would do with a normal text editor.

Insert Mode

You can go back anytime to the normal mode by pressing the escape key Esc.

Normal Mode

Move the Cursor

Now, say we have a file with a lot of text. In a graphical environment, we can easily navigate within this file using the mouse. However, this option is not a possibility with Vim. So, how do we go about navigating and moving the cursor in this case?

Thankfully, the normal mode allows us to navigate easily by pressing certain keys:

  • j : Move the cursor down one line.
  • h : Move the cursor left one character.
  • k : Move the cursor up one line.
  • l : Move the cursor right one character.
  • w : Move the cursor to the next word.
  • b : Move the cursor to the previous word.
  • 0 : Move the cursor to the beginning of the current line.
  • $ : Move the cursor to the end of the current line.

For most of these keys, you can press a numeric key before them to have them applied several times. For instance :

  • 5j : Move the cursor down 5 lines.
  • 4h : Move the cursor left 4 characters.
  • 3w : Move the cursor to the next third word.
  • 3b : Move the cursor to the previous third word.

Undo/Redo

You can undo a previous change by pressing the u key. To do the opposite, and redo what you’ve undone, then you can simply press ctrl+r.

Changing text in normal mode

Although the insert mode is where you can edit the text of a file by typing directly into it, you can still apply some changes to the text while being in the normal mode.

You can use x to delete the character at the current position of the cursor, or you can replace it by pressing r followed by the character you wish to replace it with.

You can use the delete command d, followed by another character to delete a single word, line, or more:

  • dd : Delete the current line.
  • 5dd : Delete the next 5 lines.
  • dw : Delete the current word.
  • 4dw : Delete the next 4 words.
  • d$ : Delete all text from the current position of the cursor to the end of line.
  • d0 : Delete all text from the beginning of line to the current position of the cursor.

Note that the text that you remove using the delete command gets stored in Vim’s clipboard. In other words, this is similar to cut. So, when you delete text using one of the above commands, you can paste it after by pressing p.

Speaking of copy and paste, you sure would want to know how to copy text in Vim without removing it. For this, you would have to first select the portion of text that you want to copy, and then press y.

Well, that would be simple if only we had a mouse to select text with. In our case, we have something else: The Visual Mode.

Visual Mode

If you want to select text in order to copy it or delete it, then you can do so by pressing v first, this should put you in the visual mode, and then you can change your selection by pressing the same keys used for navigation in the normal mode.

Once you are satisfied with your selection, you can then either delete it by pressing d, or just copy it using y.

Visual Mode

Replace Mode

The replace mode allows you to replace text by typing directly over it. You can access the replace mode by pressing the R key. Once again, when you’re done replacing the text that you want, you can go back to the normal mode by pressing the escape key.


If you’ve reached this far, then you should now have the basic knowledge to start using Vim. Now, your next step should be to practice.

You should keep in mind that only after you have spent hours on this text editor that you can truly grasp its utility. At first, you might find yourself editing text at a slower pace than what you’re used to. But, once you get over the learning curve, you will be surprised at how fast you can edit text.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *