Friday, August 31, 2007

The 7 Command VI Tutorial

Summary:
Seven commands is not enough to be productive in VI/VIM. However, seven commands will allow you to create and edit text files quickly. Learning too many commands early on can make it difficult to learn. This tutorial will get you started using VI or VIM and allow you to improve your skills moving forward. Check back because this tutorial will be complimented with more advanced tutorials.

Commands:
OK, lets begin. I know you don't believe that seven commands will allow you to use VI but you will be surprised. Without further ado, here are the commands:
i - Insert
x - Delete
:w - Write to file (or save if you will)
:q - Quit
/ - search
n - next
esc - Change mode

Modes:
The first thing you need to know is that VI has two modes, insert and command. Insert mode allows you to enter text as you are typing. Command mode allows you to run commands. At first it may be difficult to grasp this concept but it is incredibly efficient once you get used to it. To enter insert mode you press "i" to insert text before the cursor. To get back to command mode you press the "esc" key. When in doubt press the "esc" key and start everything from command mode.

1) Let's try it.
At a command prompt type in "vi test.txt". This will start vi with a blank file named test.txt.

2) Now type in "i". This will allow you to type. Type the following text exactly as it is spelled here. "Taking Cassandra to the End Of the World Party by Feer Before th march of Flames". If it doesn't seem to be working press esc and start over.


3) This text doesn't look too good does it? Press "esc" to get out of text mode. You'll notice that you can move around within the text using your arrow keys. Move your arrow key until your cursor is on the second e in the word Feer. If you are pressing arrow keys but the cursor isn't moving or you are seeing funny characters press "esc". Once you are on the second e of "Feer" press "x". This will delete the "e". Now press "i" to insert and type "a". The word should now spell "Fear" instead of "Feer". Press escape to get back to command mode and use the arrows, "x", and "i" to fix the rest of the sentence. It should now read "Taking Cassandra to the End Of the World Party by Fear Before the March of Flames"

4) We are almost finished. We have an incomplete word. The band name is "Fear Before the March of Flames. Not "Fear before th March of Flames". Use your arrow keys to place the cursor just after "th" and press i to insert. Now type "e" and press "esc".

5) Now that our text is the way we want it, let's save the file. From command mode type ":w". This will save the file.

6) OK, so we've created a text file, entered text, fixed text, and saved the file. Not bad for our first 10 minutes. What if we want to search for text? That is easy in VI. To search for text just enter a backslash followed by the text you want to search for. Let's search for the word "the". To do that enter "/the" and press enter.



To find the next iteration of the word "the" press "n".

7) So here we are. We are VI noobies who can effectively work with text. That's a great start. There is only one thing left to do. Let's quit VI. Type ":wq". This will save our file and exit VIM.

8) So what are you waiting for? Go listen to the song. It truly rocks. You have to hear it. What? You don't know where to listen to it? OK, I'll give you the link "http://www.last.fm/music/Fear+Before+the+March+of+Flames".


*** Important note. This blog is in no way affiliated with "Fear Before the March of Flames". I hope they will take this as free publicity (hopefully more than just my Mom will read this) and not sue me.