An Introduction to Vi

Vi is probably the most widely used editor out there. The vi version that comes with most modern operating systems is nvi, which I will discuss here. I hear vim is much preferred by many people because of it's tremendous extensions to the original vi.

Vi is a screen based editor, i.e. when you start it the whole screen will be filled with your text file. Its core, however, is nex. A line oriented editor.

Vi may seem confusing to new users, because it differentiates between a "command" and an "edit" mode. But believe me, it's worth learning how to use it. Once you know how to use vi, you practically edit files on any system, because vi is so widely used (yes, there is even a version for MS-DOS available).

You can start vi by typing "vi <file>". I recommend you type: "vi ~/.exrc" so we can make vi look more friendly. You will now see a screen filled with tildes (~), and maybe some lines of text at the top.

To describe vi commands we will use the following conventions:
^] means "press the ESC key"
^M means "press the RETURN key"
^.. means hold CTRL while pressing .., where .. is a letter.

This annotation is used in many vi related documents, so you might as well learn about them now. Now type in the following:

iset showmode^M
set ruler^M
set showmatch^M
^]:wq^M

Good. Now when you start vi again, you'll notice that at the bottom of your screen a line and column number will have appeared. In the bottom right corner of the screen there will be "Append", "Insert", "Replace", or "Command". When you start vi it will say "Command".

Open a non-existant file now. In the bottom left corner you will see something like: "myfile: new file: line 1". Press SPACE. Your screen will "blink", or you will hear a bell. Don't worry about that. The aforementioned line has now disappeared and will have been replaced by "1,1 *Command". This means you are on the first line, in the first column, you have changed the file (*) (not really, but you pressed space, so vi will think you have), and that you are in "Command" mode.

To start typing press: "i". Notice how "Command" turns into "Insert". Now go ahead and type something. Notice how the line and column numbers change as you type. Now press "^]". You are back in command-mode and you can move around the file now, using the following commands:

k - up
j - down
h - left
l - right

Practice moving around for a while. Okay. You may have noticed that you can use the arrow keys too. Forget that. The arrow keys are "weird" keys. If you are working on an old terminal, or via ssh, or whatever, you may find that the arrow keys won't work. So it's good practice to use only the hjkl-keys. And it helps prevent RSI, because you won't have to move your arm.


Back to Linux For Monkeys, or back to Editing text on UNIX.