November 20th, 2003, 11:22 PM
A Vi Tutorial
Hey guys, I wrote a Vi tutorial for MsMitten's newsletter so I thought I would post it here for everyone to see:
~A Vi Tutorial~
Vi (pronounced "vee eye") is a *nix text editor that was originally developed by Bill Joy in 1976. Bill more or less hacked up two very bad editors of his day called "ed" and "em," put them together, and Vi was born. A more detailed history is located here: http://www.cs.pdx.edu/~kirkenda/joy84.html
Over the years there have been many differant releases of Vi. These include but are not limited to VIM, Elvis, Vile, Lemmy, NVi, Stevie, WinVi, XVi, Pvic, etc... But the basic concepts of Vi have stayed in tact in all of them.
The difference between Vi and any other text editor is that Vi has two modes. A "command mode", and a "insert mode." The command mode lets you quickly move around the document, delete lines, insert lines, and whatever else you feel like doing. The insert mode is used to insert new text. Now you may be saying to yourself, "Well why don't I just use my mouse and the toolbar commands in my notepad?" Well because in the before time when the terminal was king, mouses were not really used all that often <gasp>! Vi almost simulates free formating in a terminal (I know that doesn't make much sense; let me explain): instead of having to punch the up arrow 100 times and then go to the end of the line and then press ctrl + H 20 times as you would have to do in pico; in Vi you just type 1G, and dd (see?).
Okay so on to the commands:
When you first enter a terminal, you are going to be presented with your shell prompt:
To enter into the Vi text editor, simply type Vi or Vim (depending on what version you have). This will present you with a empty page full of '~'. These represent empty lines. Right now there is little that you can do except start entering text, so lets do that. Right now you are in the "command mode." In the command mode you can enter various commands, which you will learn later. With these commands you can specify also how many times to do that activity by putting a number in front of it.
Example: dd deletes one line, but 4dd deletes four. Get it?
note: From now on, when I type ^ and then a letter (^P) I mean <Ctrl> letter.
to enter the insert mode you must type either
note: 'a' = puts you in insert mode after the character your cursor is on
'i' = puts you in insert mode before the character your cursor is on
To exit insert mode simply press:
Now you are back in insert mode. To quit Vi, in command mode type:
More than likely it is going to say "No write since last change" so you either have to save your document with:
or use the command:
which exits regardless of the document state.
note: if you are editing a file that already has a saved filename, you can exit and save at the same time with:
Now go through your files and find a text full file that we can play around with. To open that file type:
Okay, now lets learn how to move around in Vi. The most basic movements in command mode are the arrow keys:
up, down, left, right or h, j, k, and l (which do the same movement)
If you feel like getting more complicated then use these keys which also do the same thing:
+, -, <backspace>, <space>
To Scroll you can use either:
^D (scroll down)
^U (scroll up)
^F (page down)
^B (page up)
There are many more ways to move around (like next word, next white space, etc...), but I will let you look at those later at the end of the file.
Next we will learn how to search. The two handiest commands are
for search and
for goto. To use the first one, simply enter command mode by hitting
then whatever you are trying to find. Say I wanted to find the symbol @ in my file:
would take me right to it. Then you can hit
to go to the next occurrence of that string or
to go to a previous. Next, the 'G' command lets you go to any line you want to. Simply type the line number and then
If I wanted to go to line 128:
would take me there.
note: If you don't specify a number and just hit 'G', you will be taken to the end of the file. To go to the beginning of the file, use
A final note on moving. If you move somewhere you didn't want to go or would just like to move back to your original position, just type
' ' (that is two apostrophes without spaces)
Now we are going to learn how to delete text, copy text, and paste text. The easiest way to delete text is with the 'x' command. Just put the cursor over a letter and hit:
You can also use a number in front to say how many letters to delete:
deletes four letters. The next delete command is:
which deletes one word forward and:
which deletes one word backward. Once again these can both be used with the numbers. Finally, there is the:
command which will delete a single line unless you add a number before it. If you make a mistake and delete something that you really didn't want to delete, then the:
command will fix that right up. It will restore the last mistake you made. Try and use it twice; it will restore the mistake you just made.
Next we have the yank command which is achieved with:
It will copy text. You can also use the number commands in front of it.
note: yank starts at position 0 so:
will yank 4 lines.
Possibly one of the greatest thing about Vi is its buffers. It has 26 different buffers that you can store text in (a-z). You can call the buffers with the
command where n is the name of the buffer. For example:
will store 5 lines of text into the buffer named 'a.' You can then print that buffer back out using:
The great thing about buffers is that they can be used to store anything in. Say you were writing some code and wanted to delete something for the time being but you might want to use it later:
would delete 5 lines and store it in buffer q so you could use it later.
That is all for now, go to the link below for more information on Vi and for some more commands.
All my information came from prior knowledge and:
which is a document written by Bill Joy.