2.0 - Learning vi.

These are some basic hints for the novice vi user. First, keep a command summary with you at all times. A quick reference guide/command summary is included later on. Second, get a good book that covers vi. This document is not the best way to learn it (at least not yet.) I'm not sure if this document should teach people to to use vi from scratch, as there are many good books on it already. However, there are hints here. As for choosing a book, the standard rules apply: look at it before buying it. See if any of it makes sense to you. Make sure that it has exercises that you can practice with. Compare it with other books -- after all, vi can be very confusing, and you want to make sure that it is the book that is confusing, and not just that you underestimated the difficulty of learning vi.

Also, seek out vilearn or vitutor, programs designed to teach you the basics of vi.

2.1 - What games will help me learn vi?

This may seem a bit silly, but there are many games on Unix systems that can help you learn to use vi. These help particularly with the basics. Although I don't know of any games that help with every vi command, I do know of a few that will help you learn to use hjkl to move the cursor around. NetHack, a rouge-like game, is particularly good for this, as it is a large game and can be entertaining for quite some time. Not to make the other games sound worse, but some other ones are: rouge, moria, omega, worm, and snake.

2.2 - What is the difference between Command mode & Insert mode?

Often sited as one of the main problems with vi, and equally often sited as being one of its best strengths, vi differentiates between a "Command mode" and an "Insert mode." Understanding this difference is VITAL to learning vi. When one starts vi it starts in command mode. In this mode, one can move around the file, and issue commands to change certain areas of the text, cut, copy and paste sections of the text and do much more. Insert mode is where one can actually insert text. In other words, command mode is used to move around the document, and insert mode is used to type text into the document.

Commands such as: a, i, c, C, O, o and others will switch one from command mode to insert mode.

<Esc> or ^C will take one out of insert mode and return one to command mode.

Get used to this distinction. It is one of the things that makes vi different from most other editors. It also allows one to do a lot of things without taking one's hands from the standard keyboard position.

2.3 - Wait, my keyboard doesn't have a <Esc> key! What should I do?

Try hitting ^[ instead. If your keyboard has a <Meta> key, try that. If neither of these work, try ^3.

2.4 - What are all of those ~s?

They're just there to let you know where the bottom of your file is, they are not actually in your document, and you do not need to worry about them.

2.5 - I can't get used to using hjkl, do you have any suggestions?

First, if your terminal is set properly, you should be able to use the arrow keys. However, if you think that you will be using vi a lot, then it makes sense to learn hjkl, as they are faster to type.

2.6 - How do I quit without saving?

:q! will do it. If vi appears to have frozen, make sure that you haven't hit ^S by mistake. In order to undo a ^S, hit ^Q.

2.7 - How do I insert a file?

:r <filename>

For example, to insert the file /etc/motd, type: :r /etc/motd

This will insert the file at the current location in the file you are working on. If you specify a number before the r, it will insert it at that location in the file.

2.8 - How do I search for text?

/<text> will search forward. ?<text> will search backwards. ?? or // will repeat the last search. It is worth noting that these are pretty much standard in Unix. In addition, in vi, n will find the next occurrence. N will repeat the last search, reversing the direction. Regular Expressions may be used within searches.

2.9 - How do I search for a control sequence?

/^V^<seq>

^V will tell vi to take the next character literally, and not to take it as a command.

2.10 - How do I reformat text?

If your computer has the program fmt on it, all you need to do is type !}fmt from insert mode. This will rejustify the text from the current location until the end of the paragraph. If your machine does not have fmt, you need to find a similar program. (I gather there are many such programs available from the public domain, but I do not know much about them.)

2.11 - How do I copy text?

Okay, this might be a bit complicated. Take from this section what you can, and reread it a few times. Also, experiment.

"<letter>yy will copy one line of text into register <letter>. (A register is vi-lingo for a place to store data that was cut or copied.) <letter> must be between a and z. "<letter>dd will delete one line and place it in register <letter>. You may use a number before the yy or dd to specify the number of lines. Using an uppercase <letter> will append the text into the register leaving what was there before.

"<letter>p will put the text after the cursor. "<letter>P will put it before the cursor. If the register contains the beginning or end of a line, the line will be placed on another line as appropriate. Y may be used as a short cut for yy. In addition, y$, yH, yM, etc. are valid, as are the equivalent d commands. For quick cuts and pastes, no register need be specified. In this case, no appending is allowed, and the register will be removed if another delete command is given. (Including x).

For example, to move the previous paragraph, one would go to the top of the paragraph, type "a13dd, move to the position in which one wishes to put the paragraph, and then type "ap to put it below the current line.

Now, presumably you want to be able to cut and paste into areas that are not just the end of the line. In order to do this, use m to mark an area. This letter may be the same as a cut/copy register, they are stored in different area of memory. Then, type "<register>`<letter>[yy or dd]. Where <register> is the register to put the text into, <letter> is the letter used to make, and yy or dd as appropriate.

2.12 - Ahhhh!!! I just hit dG and lost my dissertation! What can I do? (Or, I've just made a mistake, what should I do?)

u will undo the last command. U will undo changes to the current line. (Granted, a one line dissertation would not be much.) :e! will reload the current document without saving any changes. In addition, deleted text gets stored in the registers numbered from 1 to 9. "<n>p will put the last nth deletion. You can quickly search the registers by trying one, hitting u, and trying the next. (In order to expedite this, vi uses . slightly differently than normal. Instead of repeating the last command, it will try the next register, so all you need to do is: "1p u . u ., etc. until you undo the delete you want to undo.)

2.13 - I've just written my dissertation and have been told that I need to have each section in a different file, what should I do?

:[m],[n]w <filename> will save between lines m and n to <filename>. This line numbering method works for almost every : command. If you use :[m],[n]w >> <filename> it will append it to the file.

2.14 - What's the deal with all of these : commands?

The commands that follow a : are commands from the ex editor. These allow a lot of flexibility and power. For example, there are many different ways to search and replace, all of with have some similarities (in fact, they are in some ways the same...)


[Index][next][previous]
This HTML version of the vi Faq was compiled by Baruch Promislow of Macom Networking LTD.

(C)opyright, E. Larry Lidz, 1994, 1995. All Rights Reserved.