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:
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.
November 21st, 2003, 09: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 :)
November 22nd, 2003, 09:04 AM
Great cheat sheet HTregz. I agree, I use Vi whenever I am using a terminal. It really is just damn powerful.