An Introduction to vi

Vi is a text editor, designed to be used as the main editor in the UNIX operating system. As such, vi is available in a large number of systems, from large workstations using UNIX, to small computers and even mobile equipment. It is an editors that has been used and approved by thousands of programmers and other computer users.

Vi is very powerful, but a little difficult to understand initially, due to its philosophy of work. In vi, there is the concept of editing modes, so you cannot just run the program and start typing as you do in a common text editor. To enter modes you usually need to type a letter, such as "i", "s", ":", etc. The main reason for this is that vi was created with the philosophy of concentrating most commands on the normal keys, that can be accessed without moving your fingers.

The advantage of this method of editing is that you can do most operations on a file, such as moving around, deleting, changing characters, and running macros without having to move your hands from the main keyboard. This philosophy is one of the secrets of the speed with which one can edit text in vi.

The basic commands

The basic commands in vi are used to enter text. There are two main ways to enter what is called the "insertion mode". You can either type "i", to insert directly before the current character, or you can type "a" to insert directly after the current character. Once you have entered your text, you can leave insertion mode with "ESC" (the escape key). In fact, escape is the key you can use to escape from any mode and go back to the "original" mode that you have when the program is started.

Another important command, especially for the ones who "don't want" to use vi, is the exit command, ":q", which means quit. A lot of commands in vi are started with ":", since ":" starts the command mode, where you can compose a line of commands to be executed by vi.

Once you learn the basics of vi, you will notice that the time it takes to edit a file will decrease, especially if you have to work with structured text such as source code. For example, to move around around in vi you just need to use the four keys labeled "h", "j", "k", and "l". They will move around in the file one position to the left, down, up, and right, respectively.

More moving commands

There are also commands to move whole words at a time. The key "w" will move you one word to the forward direction. The key "b" will move one word in the backward direction. The key "G" will move you to the end of a file, and the combination "1G" will move you to the beginning of a file.

In fact "1G" is an example of a command with a numeric prefix. You can type any number before G, and this will take you to that line. For example, "22G" will take you to line 22. Another way of doing the same is typing ":22". You can go to a specific column with the command "|". For example "10|" you take you to column 10. As another example, "(" will take you to the beginning of the next sentence.

Everything starts to get even more interesting once you can combine these moving commands with editing commands. For example, the key "d" means that you are deleting whatever characters we are moving over in the next commands. Thus, if you type "dl" this means "delete until you find the next character". Similarly, "dj" means delete until the next line, and "dG" means delete until the end of the file.

Copying text also can be done in a similar way. You use the key "y" followed by a moving command (in UNIX parlance, copying is refereed to as "yanking"). Thus, "yG" means copy from here to the end of the file. Also, "y)" means copy until the beginning of the next sentence.

You can paste whatever text you deleted using the "p" key. By default, it pastes after the current character, but you can past before the current character with "P". There is also a version of "y" for whole lines: just type "Y" once.

As you see, that are a lot of commands that you can run just typing single keys. This is the whole idea of vi: simplifying the amount of editing you have to do (even if it is a little difficult to learn this at the beginning). As another example, if you want to repeat the previous command you typed, just hit the key "." and if will be repeated (even if it means to type a whole paragraph that you entered with an "i" command).

I hope this was enough to give a quick introduction to vi. There is a lot more to be learn about this text editor, and you are not supposed to learn everything in just one session. Just remember that learning vi is a nice way of becoming more productive as you work with your computer. Invest a few minutes every time to learn a different command, and you will see that you productivity at text editing will increase.

Further resources

There are a lot of resources out there that can help in learning vi. Here are some that I use frequently:

Article created on 2008-08-19 22:52:35

Post a comment