November 21st, 2003, 12:22 AM
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.
November 21st, 2003, 10:36 PM
Handy Tutorial.... Vi is the god of editors.
My Linux course requires students to use Vi, however many students can't grasp it; they are too used to the Windows GUI environment. In an attempt to aid them I created a Vi Quick Reference sheet in PDF format. I've attached the sheet to this post, hopefully people will find some use for it.
Edit: Apparently I've posted this before, I just looked in my profile and noticed it on the list of attached files. However It probably better suited here. People reading this tutorial are the ones the quick reference will help the most, so I'll leave it here.
IT Blog: .:Computer Defense:.
(Pronounced Pinched): Acronym - Point 'n Click Hacked. As in: "That website was pinched" or "The skiddie pinched my computer because I forgot to patch".
November 21st, 2003, 10:49 PM
Great tut Lansing_Banda, thanks for the history of vi which I never knew
Hey HTRegz, something you might consider adding to your quick reference guide.
If you deal with reading logs a lot, or if you are going through a large file you might consider using the following command in vi.
then you can jump to the line you want by typing the number followed by a 'G' to go directly to that line or a 'g' to go to the line after the line number you put in, i.e. 8226G would take you to the front of the line 8226 whereas 8226g would take you to the line directly after 8226. I can't tell you how much time that command has saved me while going through 25,000 line log files
Give a man a match and he will be warm for a while, light him on fire and he will be warm for the rest of his life.
November 22nd, 2003, 10:04 AM
Great cheat sheet HTregz. I agree, I use Vi whenever I am using a terminal. It really is just damn powerful.
You shall no longer take things at second or third hand,
nor look through the eyes of the dead...You shall listen to all
sides and filter them for your self.