> Sford wiki > Sford vi tutorial

A tutorial for the "vi" (pronounced "vee-eye") text editor.

Contents


This tutorial assumes you have a Unix shell in front of you and are ready to type what I tell you to type. Note that there are different versions of vi out there, and you might see some minor differences in behavior.

Also, be aware that each step of the tutorial builds on the steps before it. I.e. it would be confusing to start in the middle. Please start at the beginning and go straight to the end.

In the instructions below, when you see "<R>" it means press the "Return" (or "Enter") key. If you see "<E>" it means press the "Esc" (or "Escape") key. If you see "<Cx>" it means control-x.


First steps

 rm tutorial.txt<R>

Maybe this gives you an error saying that "tutorial.txt" doesn't exist. Don't worry, that just means that this is the first time the tutorial's been tried. You will be creating that file, and I want you to start out without that file existing.

 vi tutorial.txt<R>

Your screen should be mostly "~" characters down the left side. It might say "tutorial.txt [New File]" at the bottom. Be aware that the tutorial.txt file is not yet actually created! It won't exist on disk until you save it later on. At this point, you are in "command" mode. (Don't worry what that means yet.)

 i

Note that I did not include "<R>" at the end. Don't press return/enter. This puts vi into insert "mode". You should see this reflected at the bottom of the screen.

 This is a test<R>
 of the vi<R>
 editor

You are still in insert mode. Your cursor is at the end of the word "editor", ready for you to type more.

 <E>

The "esc/escape" key takes vi out of insert mode and back into command mode. The cursor is now on top of the "r" of "editor".

Most modern versions of vi will let you use the arrow keys. Try it now. Move your cursor to be on top of the "v" of "vi" in line 2. If the arrow keys don't work, or if you like to keep your fingers on the home keys, you can use letters instead of arrow keys. Here are the equivalents:

I usually use the "hjkl" letters instead of arrows. It takes some practice, but soon you will be able to move around the file with ease.

So anyway, assuming your cursor is on top of the "v" if "vi".

 iwonderful <E>    (note the space after the "l" before you press "Esc/Escape")

This inserted the word "wonderful", leaving the cursor on top of the space after the "l". At this point you are not in insert mode.

 dd

This deleted the "of the wonderful vi", leaving your cursor over the "e" of "editor".

 ithis is only a test<R><E>

This inserted the line"this is only a test", moving the "editor" line down. Note that as you typed, it kept moving "editor" over to the right. This can be annoying when you know you want to enter a whole new line.

 Oif this had been an actual emergency<E>    (note the capital "O")

Pressing capital "O" opens a new line above the one you were on.

 oyou would be in trouble<E>    (note the lower-case "o")

Pressing lower-case "o" opens a new line below the one you were on.

Now use the left arrow (or "j") to get the cursor over the "t" of "trouble". Then:

 xxx

Each "x" deletes a character.

 :wq<R>

Pressing ":" put your cursor to the bottom of the screen. The "wq" command saves the file and quits vi. You should be back to the Unix prompt.

 vi tutorial.txt<R>

And we're back in again.


Moving around

You already know to use the arrow keys (or hjkl) to move around the file. But that can be very slow.

 :3<R>

As before, the colon moves your cursor to the bottom of the screen. From there, you can type a number, press "return/enter", and vi will jump to that line number. So you should be over the "I" of "If".

 $

The dollar sign zooms you to the end of the line.

 ^

The carat zooms you to the start of the line.

 :1<R>

Jump to the top of the file. But how do you get to the end of the file? Since you can see the file has 5 lines, you could just do ":5<R>". But usually you don't know now many lines there are.

 :$<R>

Dollar sign is a short-cut meaning the last line of the file.

 yy99p

The above commands will be explained in more detail later. For now, suffice it to say that it takes the line under the cursor and makes 99 additional copies of it. This has the effect of making the file 104 lines long.

 <Cf>

Control-f moves down one screen's worth. Now the whole screen is nothing but "editor".

 <Cb>

Control-b moves up one screen's worth.

 :1<R>

Back to line one, cursor on the "T" of "This".

 w

Move to the right one full word. Let's keep going.

 wwwww

Five more words, the third one of which went to the next line. The cursor should now be over the "o" of "only".

 bb

Each "b" goes backward one word. Cursor over "t" of "this".


Deleting blocks of text

Let's get rid of all those extra editors.

 :6,$d<R>

The "number,number" construct in front of the "d" command told vi to delete the range, inclusive. You could also have used ":6,104d<R>", but again it is not practical to remember how many lines there are.

 :2<R>
 :.,.+2d<R>

Just as dollar sign is a shortcut for "the last line of the file", a period is a short cut for "the line under the cursor". Note that you can do simple arithmetic in a range. So from line 2, doing ":.,.+2d<R>" is the same as doing "2,4d<R>".

 :1,$d<R>

This deletes all lines in the file.


Oops, I made a mistake!

There is an undo command:

 u

This brings back the text that was just deleted. Most modern versions of vi will let you press "u" again to undo the change prior to that, etc. Some versions allow multiple undos, but have a rather small limit to the number of undos that it can handle.

Anyway, lets assume that you've made a royal mess of the file and don't want to use "undo". You just want to quit without writing the file.

 :q<R>

Vi doesn't want you to accidentally lose your changes. So we need to tell vi that we really mean it.

 :q!<R>

The exclamation mark tells vi to exit for real.

 vi tutorial.txt<R>

Back to the file.


Copying text around

In vi, the cut-and-paste buffer is called the "yank" buffer. Copying text into it is called "yanking" the text.

 yy

This yanks one line into the buffer.

 p

This pastes the contents of the yank buffer below the line that the cursor is on. You now have two copies of "This is a test". You could also use upper-case P to paste above it.

 :1,$y<R>

The colon form of the yank command can take a range, just like the delete command. The above yanks the whole file into the yank buffer.

 :$<R>p

This jumps to the end of the file, and pastes the contents.

Note that deleting text generally also yanks the text. E.g. ":1,$d<R>" deletes all lines, and "p" pastes it back.


Finding and fixing text

If you've followed the instructions carefully, there should be two instances of the word "uble" (it was from when you deleted the first three characters of "trouble").

 /uble<R>

This found one instance. To find the next, you could re-type the above line, or just type:

 n

This finds the next instance of the last thing you looked for. Note that if it gets to the end of the file, it will wrap to the top and continue finding. Keep half an eye on the bottom line when finding; it is often good to know when it wraps.

 itro<E>

There, that instance is fixed. Let's find the next.

 n

Interesting. It only moved forward one character. This is a frequently annoying thing about vi. When you insert some text and press "esc/escape", it leaves the cursor over the last character you inserted. This has the net effect of moving the cursor backwards. So doing a find next moves forward from the cursor and finds the same one it found before. So let's go to the one we meant to go to.

 n

There. Now we could re-type the "itro<E>" to fix it. Or just type:

 .

The period tells vi to repeat the previous text-modifying command.

 n.

HAH! Did you forget that the insert command performed by the period left the cursor back a spot? So the "n" found the same one! Now you've got "trotrouble".

 u

Undo to the rescue.

Let's try one more find:

 /THIS

It didn't find it. Vi is case-sensitive when it finds text.


Find and replace

Notice that the third and ninth lines don't start with capitals? Let's fix them in one command!

 :1,$s/this/This/<R>

The substitute command takes a range like delete and yank. It looks at each line for the first instance of "this" and changes it to "This'.

But look at the line where the cursor is. "If This had been..." We replaced some instances of "this" that we shouldn't have. We could undo it, but let's instead fix the problem with another find-and-replace:

 :1,$s/ This/ this/<R>

The instances that we want to change all have a space in front of them. So including the space in the find part excludes the instances of "This" at the start of the line.


X marks the spot

Vi has a simple bookmarking function called "marking". It means you don't have to count lines to do things like deleting or yanking text.

 :2<R>

This puts the cursor on line 2.

 ma

The "m" command followed by a letter marks line 2 with that letter ("a"). Unfortunately, vi does not tell you that it did. No feedback. Just trust it.

 :$

This goes to the last line of the file.

 'a

The single quote followed by jumps back to the marked line.

 :4

Now the cursor is on line 4.

 :'a,.y<R>

This yanks the text lines, starting with the line marked "a", and ending with the line under the cursor. This is a common method for yanking or deleting text - navigate to the first line of interesting text and drop a mark. Then navigate to the last interesting line and use the mark and period as the range. This works with the delete command as well.


Repetition

We've looked at several commands that don't start with colon:

All of these can be repeated by typing a number before you type the command. For example:

 12oWhat a lot of typing<E>

This inserts 12 copies of that line. (You might need to wait a second for it to complete.)

Earlier in the tutorial, you entered "yy99p" and I said I would explain it later. It was actually two commands: first it yanked one line, then pasted it 99 times.

Retrieved from "http://wiki.geeky-boy.com/w/index.php?title=Sford_vi_tutorial"