User Tools

Site Tools


vim

Introduction to Vim.

by Stormm Dragon

Editor's Note

This article first appeared as a log of an irc class. We have kept the pedagogical character of these instructions.

Introduction

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.

Getting Started

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:

vi viclass.txt

you should start in command mode but just to make sure press "escape".

Writing a Document

  1. Now, to enter insert mode press the letter "i". type the following:
The quick brown fox jumped over the lazy dog.
  1. Now, let's say you need to copy it and paste it, because your mean old teacher assigned you to write it a specific number of times.
  2. press "escape" to get back in to command mode.
  3. Press"yy" to yank the line. It's 2 y characters.
  4. Press "p" to paste the line after where the cursor is. Note:if you were to press"Shift+p" (capital p) it would put the line before the cursor.

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.

  1. Use up arrow to get to the top of the file: the first line.
  2. Type the number of lines you want to yank followed by" yy".

So it would be:

          2yy
  1. Press "p" to paste them.

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.

          4yy

Move to the end of the line.

         p

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.

Editing 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.

  1. Find the word "lazy".
  2. Go to the end of it after the space.
  3. Press "i" to get into insert mode.
  4. Type:
    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.

  1. Find the "d" of the final word "dog". Note:make sure you are back in command mode first
  2. The way you delete a word is with the command "dw": "dw" for "delete word".
  3. The final word "dog" vanishes with a puff of blue smoke and a faintly heard yelp that vanishes into the distance.

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".

  1. Find the space you want to get rid of and hit "x".
  2. "escape" then
    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!

  1. Find the first letter of" dog".
  2. The way we change a word is with the command "cw" for "change word". So, type
    cw
  • Type the word "cat".
    cat
  • "escape" to get back to command mode.

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 …

  1. Press "down" to get to the next line.
  2. find the "d" in "dog".
  3. Press "period) ".".

the "period" "." will repeat the last action.

Ok, so the cat needs a "meow" 'cause it's a cat

  1. To append to the very end of the line press "shift+a".
  2. Press "space".
  3. Type:
    meow

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

5x

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:

/dog

Press "enter".

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.

Quitting Vim

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

:q!

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

vi viclass.txt

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:

dd

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.

  1. Find the" l" in "lots".
  2. press "Shift+d" to delete to the end of the line.
  3. Using append to the end of the line, "Shift+a", put your thoughts 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:

:wq

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.

vim.txt · Last modified: 2013/07/03 14:27 (external edit)