The Basics of vim (Vi IMproved)

During my work over the years as a software engineer, I've noticed that many people have trouble learning how to use the vi editor. Although the need to know it has lessened though the years, knowing how to use vi is still a useful skill.

Once you know how to use a few vi commands, using another text editor (or watching someone else use another text editor!) can feel inefficient and cumbersome. So I wanted to make a short video to explain the basics, so people can jump right in and start using it.

Why learn vi?

If you're trying to get comfortable working on the command line, it's often useful to open an editor in the context you're in, without switching to a graphical application or installing software.

If you learn how to use vi, it won't matter what version of a UNIX-like system you're on; all of them will generally include a version of the editor.

It doesn't take much to learn enough about vi to be productive. In this video, I'll start with the basics.

Example: vi on MacOS

As an example, I'll show the version of vi that comes with MacOS 10.14 "Mojave". I'll also use the accessibility keyboard to make it easier to visualize what's going on. First let's take a look at what we're actually executing.

$ which vi
/usr/bin/vi
$ ls -la `which vi`
lrwxr-xr-x  1 root  wheel  3 Nov 29 20:16 /usr/bin/vi -> vim

As you can see, vi on MacOS is a symbolic link to vim (vi improved). This is quite common on most modern UNIX-like operating systems, but if you're working on an older operating system, you might find a less advanced version of vi.

Let's Test it Out

With that, let's get going. If you open a command prompt (on the Mac, I use iTerm2), type vi, and press enter, you'll see a screen with a little bit of help text and information, and tilde characters along the left side of the screen. The tilde characters indicate the end of the file. If you're used to an editor like Notepad, you might think you can now start typing. That isn't true, and that's what irritates a lot of people about vi at first. Most widely-used software today is much more intuitive to use, but vi was created before any of those usability patterns existed.

Command Mode and Insert Mode

The first thing you'll need to know is that vi has several modes. We'll talk about insert mode and command mode first. When you first open vi, it will be in command mode. The simplest command you can give vi is the insert command. Press i to enter insert mode. You'll see the text "-- INSERT --" appear at the bottom of the terminal window, to give the user a clue as to which mode is in use.

When you are in insert mode, vi behaves much like any other editor that you might have used in the past. I'm just going to type some text in:

Hello, world!
Let's see what `vi` can do.

Now I'm going to press escape. In vi, when you're finished typing in insert mode, you must press escape to return to command mode. Notice that after I pressed enter on the second line, the tilde disappeared. So now I've got two lines of text, and a blank line below it - and I'm back in command mode.

Extended Commands

By now, I must admit that I'm by no means an expert on vi. I've learned tips and tricks from others I've worked with over the years, and lots of people do things differently. But I want to show you what I personally find to be the most useful commands.

Earlier I talked about vi's two basic modes. Actually, there is a third mode: ex mode (named after the older ex, or extended editor, which vi evolved from), or colon mode (as it is now sometimes known), which you can use to type in more extensive commands.

For example, if I now want to save my work, I would first press : (colon), and then enter the command w (short for write, which can also be used), followed by a space, followed by my desired filename, to write the file to disk. I'll use the name hello.txt.

:w hello.txt
"hello.txt" [New] 3L, 43C written

Now, as you can see, vi reports that I've written out a new file called hello.txt, consisting of three lines, and 43 total characters.

You can use other variations in ex mode, such as using w! (often pronounced "w-bang") to force a file to be written (useful if a file with the given name already exists).

The other ex command that you'll need right away is q, short for "quit", which exits the editor. If you have unsaved changes, vim will prompt you to type q! (or "q-bang") to force-quit.

Another ex command I find useful is :1, which returns you to the first line of the file. Actually, I use this one quite often, because you can type in any line number. It's useful when you're trying to locate a particular line of source code.

More Useful Commands

Let's imagine we want to add some text onto the second line we've written. So now we'll want to move around a little bit in our document. In ages past, computer keyboards didn't have arrow keys on them. But many people rested their fingers on the "home row" of the keyboard. So vi was designed to use the h, j, k, and l keys for movement. The h key, being the leftmost of the four, is used for leftward movement. Likewise, the l key is used to move to the right. The j key (which happens to dip just below the line it's written on, if that helps you remember) is for downward movement. Finally, the k key is used for upward movement.

In a modern version of vi, you may not need to use these commands for movement at all, since vim supports the use of arrow keys. When you use vim, you can use the arrow keys to move around in either insert or command mode. But if you ever encounter an older version of vi, you have to be in command mode to move the cursor. So knowing the "old way" to move around may come in handy. Also, it will help you to remember the next two command I'm going to show you.

Commands in vi are case sensitive. For example if you press H (capital h), the cursor will be placed at the top of the current window. If you press L (capital l), the cursor will be placed at the bottom. I don't usually use those commands, but I wanted to mention them because they're quite different from the actions their lowercase versions perform. But the commands diverge more from here, so I'll touch on the ones I most commonly use.

Append to end-of-line

When I'm looking around in a file, I'll often find that I want to append something to the end of a line. To do so, press A (capital A). This will move the cursor just beyond the end of the current line, and enable insert mode.

Hello, world!
Let's see what `vi` can do. Well, it's easy to append to the end of a line. That's a good start.

Just as I did before, I'll press escape to close insert mode. But the line of text I've written is pretty long. I might be writing a technical document, where I may be expected to limit my line length to a certain number of characters. Let's see how vi can help with that.

The Ruler

The vim editor has the concept of a ruler, which, when enabled, will let you know the current position of the cursor. To enable it, use a colon command:

:set ruler

Now, in the lower-right corner of the terminal, you should see the line number and column number of the cursor. (For example, mine says 2,97, since I'm at the end of the long line of text.)

Moving Around Faster

Now, I'm not happy with what I wrote, because it's too verbose. So the next thing I want to do is go back a few words to remove it. You can move forward and backward one word at a time by using the B (capital B) and W (capital W) commands. (Often you can hold down the control or command key and use the left and right arrows to accomplish the same thing, but it depends on your terminal.) Pressing B will go back by one word, and W will go forward one word. (The lowercase versions of the same command will do the same thing, but also stop at punctuation.)

So I'll press B until I'm at the point where I want to edit the file. Then I'll use the i command to return to insert mode and make my changes. I'm going to remove the whitespace at the end of the line, and then add an additional line break.

Deleting Lines

Now, I'm going to remove the last line in the file using the d command. The d command is different from the others, in that you must press d twice for it to take effect. If you simply type dd, you will remove the line the cursor is currently on. If you wanted to delete more than one line, you can add a number in between the ds. For example, if I press k to move back to the first line, I can then type d2d to delete both of the remaining lines. But I didn't really want to do that, so I'll press u (lowercase u) to undo that action.

Joining Lines

If you have two lines that you want to concatenate onto a single line, you can use the J (capital J) command to join them. For example, given our text:

Hello, world!
Let's see what `vi` can do.

By positioning the cursor on the first line and then pressing J, you get:

Hello, world!  Let's see what `vi` can do.

... and the cursor is positioned where the line break used to be, so that you can make further adjustments if you want.

Deleting a Single Character

For whatever reason, the lines were joined together, we got two spaces instead of one. Since I only wanted a single space, I pressed x (lowercase X) to delete the character directly under my cursor. If you use X (capital X), it behaves like a backspace command and deletes the character to the left of the cursor.

Conclusion

With that, I think I've given you enough information to start using vi on a day-to-day basis. There are many more commands you can learn and many more ways to edit more efficiently, but I'll save those for another video - or you can do your own research to find out what works best for you.