> Sford wiki > Sford vi tutorial
A tutorial for the "vi" (pronounced "vee-eye") text editor.
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.
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.
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.)
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.
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.
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:
Each "x" deletes a character.
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.
And we're back in again.
You already know to use the arrow keys (or hjkl) to move around the file. But that can be very slow.
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.
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.
Dollar sign is a short-cut meaning the last line of the file.
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.
Control-f moves down one screen's worth. Now the whole screen is nothing but "editor".
Control-b moves up one screen's worth.
Back to line one, cursor on the "T" of "This".
Move to the right one full word. Let's keep going.
Five more words, the third one of which went to the next line. The cursor should now be over the "o" of "only".
Each "b" goes backward one word. Cursor over "t" of "this".
Let's get rid of all those extra editors.
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.
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>".
This deletes all lines in the file.
There is an undo command:
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.
Vi doesn't want you to accidentally lose your changes. So we need to tell vi that we really mean it.
The exclamation mark tells vi to exit for real.
Back to the file.
In vi, the cut-and-paste buffer is called the "yank" buffer. Copying text into it is called "yanking" the text.
This yanks one line into the buffer.
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.
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.
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.
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").
This found one instance. To find the next, you could re-type the above line, or just type:
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.
There, that instance is fixed. Let's find the next.
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.
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.
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".
Undo to the rescue.
Let's try one more find:
It didn't find it. Vi is case-sensitive when it finds text.
Notice that the third and ninth lines don't start with capitals? Let's fix them in one command!
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.
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.
This puts the cursor on line 2.
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.
The single quote followed by jumps back to the marked line.
Now the cursor is on line 4.
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.
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.