by Stormm Dragon
This article first appeared as a log of an irc class. We have kept the pedagogical character of these instructions.
Vim or vi, is a very powerful console based text editor. In a lot of ways it is faster than its gui counter parts. Instead of taking your hand off the keyboard to get the mouse and click menus, you handle everything through vi's modes. Nano, for example, is a great editor, and in many ways it's easier to use. Vi, has more features though, and it has a whole coolness factor all its own. Note: You can use Vim in the terminal, too.
When you start vi you begin in command mode. You can get to command mode any time by pressing "escape". I usually press "escape" as soon as vi opens. It's not necessary, but I like to make absolutely sure. To get in to insert mode press the letter "i". so, if you have a terminal open and are ready to practise with vi, type the following line:
you should start in command mode but just to make sure press "escape".
The quick brown fox jumped over the lazy dog.
So, now our file is 2 lines long. we could just keep pasting the same line over and over or we could make our job much easier.
So it would be:
ok, so now we have 4 lines of code. So now, see if you can yank and paste 4 lines. Hint: Don't forget to press "escape" first to be sure you're in command mode.
Move to the end of the line.
Copying the line includes a carriage return.
Note:You also need to be in command mode before arrowing around in your document. otherwise you get some very interesting but unwelcome additions to your file.
Ok, let's fix the dog's description. He's lazy sure, but no one mentioned his floppy ears, and he's very proud of his floppy ears.
dog with floppy ears.
Oops, now we have lazy dog with floppy ears. dog. We can't have that, it's just plain bad grammar or something.
Oops! We have ears followed by a space and a period. We need to delete the space before the period. The dog turned on us for getting rid of its playmate. Anyway, we took out the dog, so a bit of space should present no challenge.
the way you delete one character is with the letter "x".
Now we shall take vengence on the dog for corrupting our sentence like that and making us all confused. What's the best way to get revenge on a dog: Change it into a cat!
But, we aren't just mad at the one dog; they travel in packs after all. Just look at them! There's gotta be like 8 of them here already. We gotta change them all.
We could go down through the pack and change each of them by doing cw and typing cat and pressing "escape" to hunt down the next one, but why do that when we can …
the "period" "." will repeat the last action.
Ok, so the cat needs a "meow" 'cause it's a cat
No, maybe it doesn't need it after all. We changed our mind. We could delete it with "dw", to delete the word, but then we'd still be stuck with the "space". If we move to the "space", we could delete all the characters
We're still not done with the dogs though. Let's search for the next one. Use the character "/" to search then type "dog" like this:
When you press "enter", you should find the first matching "dog".
Press "escape" to get to command mode and change it with "cw" into another animal. For example, The "dog" is now a "horse".
Press "n" to find the next occurance of the last thing you searched for.
Press "." ("period") to repeat the last change.
You should now have 2 horses.
Note: This is very useful when you have to edit a large file and change the same bit of text over and over again.
One of the most useful things to know is how to ditch the whole thing if you have messed up and don't know how to fix it. to quit without saving
That's a ":" ("colon") followed by the letter "q" followed by an "!" ("exclamation point" or "bang"). It will return to your prompt
Now, if we open "viclass.txt" we should get a blank file
insert the line
The quick brown fox jumped over the lazy dog.
But maybe that's not the last thing we should end class with. Let's "Shift+a" to append, then press "enter" to get a new line, then type:
Vim class was lots of fun!
ok, now we should probably delete the first line. we don't need to leave it and just yank it, we need to get rid of it. So, select anywhere on the "dog" line and type:
that's 2 "d" characters. Of course we can delete any number of lines with this just like with all the other commands. So, "300dd" would delete 300 lines.
Finally, I will let you put what ever you want about vim class.
The final part. Let's save your work so you can prove to all your friends you were here. to save, in command mode, type:
That means write and quit. It's preceeded by a ":" ("colon"). You should be back to your prompt
That's pretty much everything. there are more commands, but once you grasp the different modes it's easy to just look up the other commands if you need them. It just takes practise.
A good place to find all the commands is on vi complete key binding list Some of them are different than what I have shown. there are always more than one way to do things in Linux.