"I have traveled the length and breadth of this country and talked with the best people, and I can assure you that data processing is a fad that won’t last out the year.” – The editor in charge of business books for Prentice Hall, 1957
When you complete this tutorial you should be able to:
This class is divided into several sections (so I didn't get too confused as I wrote it):
Introduction - This file contains an introduction to the vi text processor. You should read this first.
Modes - You will use several modes in the vi processor. This file describes those modes for you.
Moving Around - Moving around in a vi file can prove to be quite challenging. This file will help you overcome this obstacle.
Inserting - This file will teach you how to insert new text into your vi document.
Correcting - In case you make a mistake, this file will teach you how to correct errors.
Searching - Linux uses a very power search tool called "Regular Expressions". This fill will teach you how to create Regular Expressions.
Substituting - Once you have found information in your vi document, you will want to change it. This file contains information to help you change text in your document.
Quitting - You will eventually be done with your document and want to save it and quit. Here is where you learn to do that.
This lesson introduces you to the VI text processor. You'll find this is a challenging lesson, but one that will reward you as you continue your studies in Unix or Linux.
Vi (rhymes with “pie”) is a visual editor that’s been around for many years. It was born in 1974 with the original BSD version of Unix. Since then, vi has changed considerably, but still retains much of its origins.
Vi is a full-screen text editor. It is very powerful and includes a couple hundred functions and commands. In this class, though, we will only use vi to create and save simple text files.
It is fair to mention that vi is, generally, very confusing and hard to use for most students. Remember, vi is not a “What You See Is What You Get” type of editor - like Word For Windows. Vi was born in the days when computer users were usually geeks who communicated with a computer using a Teletype machine. Vi does not have any capability to print italics or boldfaced text. Vi does not format indented quotations or bulleted lists. Vi is, to put it briefly, but a simple text processor.
Why, then, use vi? There are other word processors available for Linux - some just as powerful as Word (Word Perfect and Sun Office come to mind). Vi is a simple, quick text processor that does not add extra formatting code (like tab stops) into a file. For programmers, vi is perfect - it doesn’t mangle text that must be processed by a compiler. While you may have trouble adjusting to the world of vi; remember, it is still widely used for jobs where a “clean” text file is needed. It would be impossible to say how many Web pages and Perl scripts have been developed (and still are being developed) with vi.
In this tutorial I have included a lot of explanatory text along with commands for you to type in and try. To emphasize commands you should type in, I have used a special font (note - the following line is not an actual command - it is just an example of the type of font I used in this lab manual):
When you see something printed with that special font, enter the command as part of your tutorial. Of course, I would also encourage you to try vi on your own: kick its tires, take it out for a spin around the block. The more time you spend with vi the easier it will be to use.
Note: vi is case sensitive. If you accidentally press the caps lock key, commands will no longer work the way you expect. If vi suddenly seems to be “acting funny,” check the caps lock key.
The most common way to start vi is with a file name specification. If you enter vi myfile.txt the vi editor will start and automatically load the file named myfile.txt. If myfile.txt does not exist, then vi will create a new, empty file with that name.
You also can start vi by just entering vi (with no file specified). If you do that, Linux will start vi with an empty document - then you must supply a name for the document the first time you save it.
Note: when vi opens a new, empty document all the lines in that document start with a tilde (“~”). The first time you see this it looks a bit weird, but those are just placeholders and has no effect on your file. When you save your file the tildes will go away.
For this tutorial, I prepared a file named vi.txt (you’ll find that file in the CIS 140 directory). Please open that file:
Your screen should look like Figure 1 if you opened the file correctly.
Note: the original vi editor was later improved and renamed to vim (for “vi improved”). Most Linux systems administrators actually use vim rather than vi – but have created an alias in the system so if a user enters the command vi the vim editor is what starts. When you look at the help files for vi you may notice that they talk about vim - don’t worry about that difference. This tutorial was written to match the system we use in class. (What we need now is for someone to create a new version of vim called super-vim (or svim). The logo, of course, would be a “svimming pool”. - Sorry about that, I couldn’t resist).
Vi has three “modes” of operation and in order for you to become good with vi you must understand the differences between the modes and be able to comfortably move between them. Understanding these three modes are pretty important, so take a minute to be sure you understand this section. The three modes are:
The Master Mode is the “home” mode. From the Master Mode you can enter either of the other two modes – so you will frequently go to the Master Mode just to change from the Command Mode to the Text Mode and back.
The Master Mode is the mode you will use to move the cursor around in the file. The Master Mode is also where you will delete, change, or insert text. You’ll learn more about these functions as you work through this tutorial.
To enter the Master Mode, press the escape (esc) key; in fact, the only thing the escape key does in vi is to enter the Master Mode. You can press esc several times in a row if you are impatient - but you will still end up in the Master Mode.
The Command Mode is used to enter vi commands (duh). You can, for example, search for a particular phrase in your file by entering a “search” command along with the phrase of interest.
To enter the Command Mode, be sure vi is in the Master Mode, then press the colon key – that’s “:”. When you do that, the cursor will move to the bottom of the vi screen, display a colon, and wait for your command. Figure 2 shows vi in the Command Mode. This mode is sometimes called the “colon” mode.
To try out the command mode, enter this command:
You will find that a column of line numbers is now visible along the left side of the screen (Figure 3 shows a vi screen with the numbers turned on). If you want to turn off the line numbers you can enter:
From time to time in this tutorial I’ll ask you to look at a specific line number. If you turn the line numbers on it will be easier to find the line in question. (Of course, if you have added or deleted a line or two your line numbers will not match mine - but they should be pretty close.)
There’s one other command that students typically like: showmode. It is sometimes hard to remember which of vi’s modes you are currently using. If you turn on showmode you will see the word insert whenever you are in the Text Mode. This may help you keep the modes straightened out. To turn on showmode, enter this command:
Note: When you first turn showmode on you won't see any change to the screen. Showmode only works when you are in the insert or append mode (as described later in this lesson).
To turn showmode off, enter this:
Anytime you are able to actually type text into the document you are in the Text Mode. There are three different ways you can insert text into a document:
I’ll have more to say about the Text Mode later. Don’t worry, you’ll get plenty of practice with the Text Mode!
Remember that Master Mode is the “central” mode and you can only access the other two from the Master Mode. Figure 4 may help clarify this concept:
To practice changing modes, enter this command:
what happened? When you entered the w command the file should have been “written” to the disk (or “saved”). When that happens, vi reports the current file size on the bottom line of the screen. If you did not see the current file size, then the w command did not work. Perhaps you were not in the Command Mode? Did you start from the Master Mode and remember to type a colon first (in order to get into the Command Mode)?
Now, from the Master Mode, tap the letter
(Note: this is a lower-case O, not a zero.) When you did that, vi should have opened a blank line for input and automatically started the text insert mode.
Now, tap the escape key (to get back to the Master Mode), then type
(Note: this is a lower-case U.) What happened? U is used to “undo” the last command (a handy command to keep in mind). Since the last command was to open a blank line, u will close it again.
It’s important to learn how to move through the vi file. You can move the cursor to a specific word or letter so you can begin to edit the file - or, if the document is long, you can scroll it up or down to make the desired text visible. In this section you will learn how to move around in your vi document.
Note: all the commands in this section must be issued in the Master Mode. Be certain you are in that mode before you proceed.
Probably easiest way to move the cursor one character at a time is to use the arrow keys. Many geeks, though, prefer to use the “keyboard” move method (this way their hands stay on the keyboard at all times, rather than try to find the arrow keys). If you are in the Master Mode, you can move the cursor one character at a time with these four keys (all lower-case):
Take a minute to move the cursor around on the screen one character at a time using both the arrow keys and the letter keys.
If you want to move the cursor by words, you can use these two keys (again, from the Master Mode):
Take a minute to move the cursor around on the screen one word at a time using these keys.
If you want to move a full sentence at a time, use these keys:
Take a minute to move the cursor around on the screen one sentence at a time using these keys. What happens when you get to the bottom of the screen and want to skip to the next sentence?
If you have a hankering to move to the start or end of the line, use these commands:
Take a minute to move the cursor to the start and end of several different lines using these keys.
Sometimes you may want to move the cursor to a certain character on the line. For example, if you have a line where the letter “s” is repeated several times, you can move the cursor forward or backward to the next “s”. (Note: the following two commands only work on one line of text. They will not continue searching on later lines and move through the entire document.) Here’s the command:
One last way you can move the cursor is with these three commands:
Often, a document will be too long to fit on one screen; so you will need to scroll that document up and down. There are four commands used to scroll:
You can also scroll a document to a specific line number. If you know, for example, you need to edit line 30 in the document then you could enter this command:
vi will automatically scroll the document so line 30 is visible and your cursor will be on that line.
Take a minute to practice all these cursor movements. Be sure you can move the cursor anywhere you want in the document before you go on. If you are working with a lab partner, you should challenge each other to move to a specific word. You could make it a race - who can find the next “and” in the document first?
About the only reason to use vi is to produce some sort of text document. In this section we’ll cover how to get into the text mode and actually start typing words and sentences in your vi document.
As mentioned earlier, there are three different text input modes - but you’ll probably only use one or two with any regularity.
If you need to insert text before the cursor, place the cursor where you want the text, then enter:
(it’s a lower-case i) to insert text. When you have finished inserting text, tap the esc key to return to the Master Mode. To practice this task, move your cursor to line 36. That line currently reads: just ask a Linux guru. Move the cursor to the space before the word Linux and insert the word certified.
Now, you get to practice on your own. On line 53, insert the word Linux before the phrase “programs and scripts”
Insert will let you insert text before the cursor, but if you enter this command:
(a capital I) you will be able to insert text at the beginning of the line. To practice this command, try to insert the word awesome at the beginning of line 42. (It should read awesome program when you finish).
Append lets you add text after the cursor. To append text, use the command:
(That’s a lower-case a). Let’s practice. Put the cursor at the end of the word “bad” in line 13. Now, append to use so the line reads it’s not so bad to use after all. Here’s what you should end up with:
If you need to append text to the end of a line you can use the command:
(That’s a capital A). This will move the cursor to the end of the line and let you append text there. To try this out, add the text incredible, (including the comma) at the end of line 41.
This command opens a space in the file so you can enter a whole new line. When you enter the command:
(That’s lower-case letter o - not zero), a blank line will open up under the cursor and you can enter new text. When you finish entering the text, tap the esc key to close the insertion point.
To practice, put the cursor anywhere on line 23 (it states, “Of course, I will always help you learn vi - just ask.”). Now, open a blank line under line 23 and insert this text: If you ask a question, I’ll find an answer. When you finish your document should look like this:
If you enter the command:
(That’s a capital letter O – not zero), then vi will open a blank line above the line the cursor’s on. To practice this, try inserting this line above line 52 (it starts “Now, I think we have…”): I’ve had fun writing this short file for you to use.
At this point, you should practice inserting text. You may choose any point in the document, then insert some sort of text. Remember, this document is not going to be published so don’t worry that what you are inserting makes sense - the point is to just practice with all six of the insert commands.
I know it’s hard to believe, but even I (on occasion) make mustakes. Vi comes with a number of aids to help in the event you make a mistake in your file.
You can delete characters, words, or entire lines.
To delete a character, place the cursor over the offensive little bugger then tap
(that's a lower-case X). To try this out, let’s delete the word text on line 11. Place your cursor over the first t in that word, then tap the x key. Voila! You changed text to ext (whatever that is). Of course, now you can tap the x key four more times (one for the space following the word text) and completely wipe out the word!
However, suppose you wanted to wipe out a really big word. There must be a better way to do that than tapping the x key 10 or 11 times (as fun as that may be). Let’s wipe out the word important in line 25. Position your cursor on the first letter of important then enter the command:
(that stands for delete word). Now, the word important is gone!
However, suppose you are getting into this delete thing and want to kill an entire line. You can put your cursor anywhere on the offending line then enter:
(I guess that means double delete - or something). The entire line goes away. Try this with line 24 (I never liked that line anyway).
Perhaps you noticed a typing error (have mercy) and you would like to correct it. You can change a single letter, a word, or the entire line. Vi has a pretty slick way to change a single letter. Place your cursor on the offensive slip, then enter:
Were “x” will replace the offending character. Here’s an example. In line 26, let’s change pie to tie” Place the cursor on the P of pie then enter:
It just makes you think of magic, doesn’t it?
If you have a word you would like to change, place the cursor at the beginning of the word, then enter:
(This is for change word). The bad word will go away, then you will be placed in the insert mode to type something new. Your replacement word doesn’t need to be the same length as the one that went away. To practice, put your cursor on the first letter of note in line 25, then type:
Now, enter thing and tap the esc key. You’ve changed note to thing.
Finally, if you need to wipe out most of an entire line and enter something new, you can place your cursor anywhere in that line and enter:
(That’s a capital C). Everything from the cursor to the end of the line will be deleted, and then you will be placed in the insert mode so you can put new text in. For example, line 38 ends like this: you can do with. Place the cursor on the y in you, then enter:
Now, type this phrase possible when you use. Pretty cool, isn’t it?
At this point, you should practice deleting and changing text. You may choose any point in the document, then delete or change some of the text. Remember, this document is not going to be published so don’t worry that what you are changing makes sense - the point is to just have some practice with all of the delete and change commands.
Vi includes a wonderful search ability. You can search your document using all sorts of text options. In fact, it could be argued that the search capability for vi is better than that used in Word or other fancy word processors. This is vi’s shining moment. I’ll demonstrate a number of different search options, but I couldn’t possibly get all of them. I will, though, give you enough to enable you to search a vi document in any number of different ways.
The simplest search is to simply enter:
where phrase is what you are looking for. If you find the phrase once, you can press the n key to find the next occurrence.
If you actually entered /phrase (as listed above), you will get a message to let you know that search did not find the word phrase. Nice try.
Now, for an example that works, move the cursor to the top of the file (remember that a search starts at the current cursor location so you must move to the top of the file to search from there) then enter
You should find the word vi on line 2. Once you find the phrase, you can press the n key to get to the next occurrence. You'll find vi on line 3 and again on line 9 (and several other places in the file, but those three are enough for now). To stop a search, just tap the key.
So far, that’s nice, but no real thrill.
If you wanted to search the file backward, you could use:
where phrase is what you are looking for. Again, just tap the n key to stop the search.
What, still not impressed?
If you use a “.” in the search phrase, vi will find all instances of the phrase where the “.” is replaced by any letter. For example,
would look for a phrase that has the letter t then two characters of any sort (reminds me of a class I taught once - a bunch of characters of any sort), then the letter s. For example, this search would find:
If you are interested in finding only words that start a line, you can add a “^” to the search phrase. For instance,
If you are interested in finding only words that end a line, you can add a “$” to the search phrase. For instance,
Are you trying all of these out? Be sure to practice by substituting your own search phrases.
Suppose you only wanted to find whole words. For example, you wanted to find every instance of the word “and”, but didn’t want to find “band” or “sander” (sander?). To force vi’s search to find only whole words you could use these delimiters: \< for the beginning of a word and \> for the end of a word. For example, to find only words that begin with re I entered:
To find words ending in ing, I entered
So, to find the word and (but not sand) I entered:
and found it on lines 11, 29, 38, and 52.
At this point, you should stop and search for words that start or end with a specific group of letters.
Still not impressed? OK, here is search’s greatest trick: character class definitions! (Did you hear the trumpet fanfare?)
If you want, you can match phrases that include a certain class of characters. You define classes by placing them in square brackets. For example, you could find any occurrence of disk or disc by entering dis[ck] - the [ck] defines a “class” of characters (speaking of a class of characters - no, that’s too easy). Here are some examples:
found the word:
You can specify a range of characters in the class. For example, to find all instances of the numbers 1-5, enter:
Finally, I can combine any of these search patterns together to create a wonderful search. For example, if I only wanted to find the word “you” or “You”, but did not want “yourself”, “your” and other such words, I would use:
(Do you know why this odd-looking phrase would exclude yourself?)
At this point, you should review all these search rules and practice searching for text. Start at the top of the document and find these items:
Vi will not only find items, it will substitute another word for those items. For example, if you wanted to change the word will to can on line 21, put the cursor at the beginning of that line and enter
This will search the line for the word will and change it to can.
Try changing the word is to was on line 37:
What went wrong? Notice that the word this includes the target string is - so it was changed instead of the word is that you wanted. Change the line back to the way it started and try again, using this pattern:
:s/ is / was /
The spaces are important!
So far, we have only done a replacement on one line at a time. You can also specify a range of lines for the substitution. The command
:1,10s/remember/keep in mind/g
will look at lines 1 to 10, search for the word remember and change it to keep in mind. The g at the end of the command stands for global - that means the substitution will occur as many times as the word is found on each line, not just the first time per line (which is the default for substitute).
You can use these special characters in the range:
1 for the top of the file (that’s the number one)
$ for the end of the file
. for the current line (that’s a period)
Where would these substitution commands search?
At this point, you should practice substituting text. You may choose any point in the document, then substitute some text. Remember, this document is not going to be published so don’t worry that what you are inserting makes sense - the point is to just practice with all of the substitute commands.
To quit vi, issue the command:
If you want, you can combine w and q to save all your changes and quit - like this:
If you want to quit but not save changes, you would use this command:
That exclamation mark means, “I know I’m not saving my work - but let me quit anyway!”
Vi comes with a lot of help. To access the help system, enter the command
Vi will load a help file you can read. It is just a vi document, so you can scroll it up and down just as you would any other vi document.
Additionally, the help document has some hyperlinks embedded (really, hyperlinks!). If you see a word surrounded by bars (like this: |tips.txt| you can place your cursor anywhere between the bars, press CTRL-] (that means hold down the control key then tap the right bracket key) and jump to that topic. To return, use CTRL-t (that’s control and a lower-case t).
When you finish looking at help, exit it by issuing the quit command:
While I know vi is a bit tough to get used to, I think if you will just dive in and work with it you will soon find it’s pretty easy to use.
Good luck - and if you have questions about vi, just ask.