In many of my Tips ‘n Tricks or tutorial posts, I have commands that involve editing/creating files in Vi. For those that have never used Vi, it can be a daunting experience trying Vi for the first time.

There are many holy wars fought over the best editor to use from the *nix command line. I won’t claim that Vi is the best; rather, Vi is simply what I’m used to, it’s powerful, and it is always available on servers that I work on.

In order to best help those that don’t know how to use Vi but are interested in learning, this will serve as a very brief tutorial on how to get started.

Vi and Vim

The original Vi first saw development in 1976. Thus, it’s an editor that is more than thirty years old. It is older than Linux, and it is older than me. The fact that it is still so prevalent, is testament to the power and smart design that can be found in Vi. That said, I don’t technically use Vi; rather, I use Vim: Vi IMproved.

Vim has many features that extend the original functionality of Vi. By far, my two favorite features are unlimited undo (Vi only supports undo last command) and syntax highlighting. When you see images on my site that have syntax highlighted code, those are screenshots of Vim running in Terminal.

Many recent distros have Vim installed by default. In addition, most distros that I’ve worked with also have Vim in place of Vi. You can check this by running “vi” from the command line. If Vim is running in place of Vi, you’ll see a greeting screen that says “VIM – Vi IMproved” at the top.

Installing Vim

If you run “vim” from the command line and receive an error such as “-bash: vim: command not found”, then you don’t have Vim. Since features that Vim has are greatly beneficial, I recommend installing it if you don’t have it. In addition to the advanced features, Vim has enhanced arrow key support which most new users will appreciate as they get accustomed to the unintuitive nature of Vim.

Vim can be installed quickly using your distro’s package manger. If you are using Ubuntu or another APT distro, you can simply run “sudo apt-get install vim”. On CentOS and other RPM-based distros, gain access to a root shell and run “yum install vim”.

Once you have Vim installed, you will probably want to use Vim whenever you type “vi file”. You can do this easily by setting up an alias. Modify the /etc/bashrc file with whatever editor you are currently comfortable with and add the following line:

alias vi=’vim’

Save the file and exit the editor. Once you log into your shell again, this alias will be in place and all commands to vi will be routed to vim.

Getting Started With Vi

As I’m sure you’ve seen many times before, you can open a file for editing in Vi by running “vi filename”. This will indeed load the file for editing, but how do we actually modify the file?

Modifying Text

Vi is a modal editor. This means that the mode you are in when you first open the file is not an editing mode; rather, it’s more of a command mode where you can issue commands to Vi. In order to actually edit the file, you have to enter an editing mode.

Most of the time, the insert edit mode will be used. To enter the insert edit mode, simply press the ‘i’ key. You’ll notice that the last line of the interface will now display “– INSERT –” to indicate that we’ve changed into the insert mode.

Now that you are in the insert mode, you can simply delete and insert text as if you were in a normal editor. Once you are done making changes, hit the ESC key to exit out of the insert mode and return to the command mode, commonly referred to as “normal mode”.

Saving the File

Now that the file has been modified, we need to save our changes.

In order to save the file, you first need to be in normal mode (press ESC). You can then save the file by using the ‘:w’ command. This means that you need to press the ‘:’ key, the ‘w’ key, and Enter.

This command simply tells Vi to write the contents to the file.

If you wish to save the contents as a different file, give the write file command a file name. For example: “:w filename”.

Exiting Vi

Okay, we’ve opened a file, modified it, and saved it. Now we just need to figure out how we get out of Vi.

Similar to the write command, we just need to issue the quit command. This is done with ‘:q’.

Since Vi supports chaining commands, you can save the file and exit Vi with the ‘:wq’ command. Notice that ‘:qw’ will result in “E492: not an editor command: qw”. So, make sure that you put commands together in the correct order. Another command, ‘:x’, does the same thing as ‘:wq’.

More than Basic Editing

Frankly, this is just scratching the surface of what Vi is and how to use it. It will be enough to get most people started however.

I will revist the topic of Vi to give more details of how to do advanced uses and how to configure options to get the most out of it. For now, I have some links if you’d like to delve into the topic deeper.

Did I help you?