Manual for Unix's Standard-Texteditor VI

This text has been sucked down from The Linux Documentation project, and glued altogether.
It is intended for downloading and reading offline.
Back home

Overview

Vi (officially pronounced `vee-eye'/unofficially pronounced `six' because of the feeling one gets when using vi that it may be the text editor of the antichrist) is a display oriented interactive text editor.

Vi makes one major philosophical deviation from every other text editor I have come in contact with. The basic idea is that your hands don't have any business straying from the home row keys. This can be an advantage for the touch typist, but the guy who needs to see the letter on the key before pushing it down tends to be less enamored with this characteristic.

Since your fingers can only reach about fifty (50) keys without moving your hands, and since vi has in excess of 100 commands, something drastic must be done in order to designate all the functions a decent text editor must have. Rather than relying on extra keys on your keyboard that seem a little too far away or special key combinations that involve keys that your keyboard may or may not have, vi simply assigns a couple functions to the keys in reach.

Vi operates in two modes (insert and command) in order to determine which function should be performed when a key is pressed. This two mode novelty, in my opinion, is what causes some to confuse vi with the devil himself and causes others to place vi equal with God.

Although I find it hard to justify worshiping a text editor, I can (after much effort) appreciate the utility of vi.

Why vi? Vi is the default editor for Unix. It is possible to use other editors, but if you learn vi you can be confident that it will be on any Unix machine you use. However, the same level of confidence with another editor may be shortlived. It would be to your advantage to learn at least the basics of vi. For those who use a text editor on a daily basis, particularly for programming, vi will become a joy to use after a few months of friendship building.

As with any friendship, an emphasis must be placed on quality time, not just quantity. Those who have little use for a text editor may be satisfied with a cold professional relationship with conversations limited to a few basic commands. The rest of us would certainly benefit from a little quality time with vi. By quality time, I don't mean merely having the same conversations over and over (repeating commands you already know). I don't mean just reading about what makes vi tick. Although these are important activities, I mean telling vi things you've never told it before and observing its response. Don't discuss important issues with vi until you're pretty sure you know how it will react. Make sure you make a backup copy of the file you experiment with.

The art of conversation is often a difficult thing for people. Whether it be a young man on his first date, a newlywed struggling to entertain her mother-in-law, a new manager trying to learn about what makes her employees tick, or a computer novice trying to build a new relationship with a idiosyncratic text editor, knowing what to say and when to say it can be a daughting task, particularly when you are responsible for initiating conversation. Some of you will know just what to say to Vi and others of you would rather run.

In what follows I have included an introduction to my friend Vi. Within you will see example conversations that we have had. Hopefully this will whet your appetite to develop your own relationship with Vi. In order to fully appreciate Vi you will need to be creative in starting new conversations that are not contained in this document. The reference section describes the limits of Vi's conversational abilities, and will hopefully provide you with some ideas for future conversations. In the miscellaneous tips section, I give you a glimpse of some of the deeper conversations that can take place. Please don't get me wrong though. These are more than just conversations. These conversations can have powerful results that should make your life run more smoothly.


Starting vi

To start vi just type vi at the operating system prompt. You will see a screen with a column of tildes (~) down the left side of the screen. This signifies an empty workspace. To edit a file, just include the filename after it, e.g. vi filename. You will see the text of the file you included.
Vi is now in command mode. The most basic command to enter insert mode is i which lets you insert text to the left of the cursor.


Insert mode

I begin with a brief description of the insert mode because it is very straight forward. In insert mode the characters you type are inserted into your document. You can use the backspace key to delete any typing mistakes you have made on the current input line. The escape key (<esc>) takes you out of insert mode and back to the command mode. If you are ever in doubt about what mode you are in, just press <esc> a few times until vi starts complaining. You will then know that you are in the command mode.


Command mode

Command mode is where you do everything that isn't done in insert mode. In command mode the same keys that caused letters to appear on your screen in insert mode now represent totally different functions. Rather than go into a detailed discussion of the 100 or so commands, this section contains a list of the more popular commands. The next section contains a more comprehensive list of vi commands.



Moving around


     h         move the cursor one character to the left
     j         move the cursor one character down
     k         move the cursor one character up
     l         move the cursor one character to the right
     0         move to the beginning of a line
     $         move to the end of a line
     G         move to the end of a file
     1G        move to the first line of a file
     <ctrl-F>  move down one screen
     <ctrl-B>  move up one screen
If you try to move somewhere that vi doesn't want to go, e.g. pressing h when the cursor is in the left-most column, your terminal will complain by either beeping or flashing the screen.


Deleting text


     x     delete the character under the cursor
     dd    delete a line

Saving and quitting


     :w    write to disk
     ZZ    write to disk and exit
     :q!   exit without writing to disk
Actually, the command for quitting vi is :q. You can save and quit by typing :wq but ZZ does the same thing and takes one less keystroke. If there are unsaved changes to the text and you try to quit using :q, vi will warn you that you have unsaved changes and will prevent you from quitting. In order to quit without saving the changes you must use the override switch, !.


Copy, delete, and moving text

Knowing how to copy, delete, and move text is a prerequisite to any serious text editing task. If you have a small amount of text to delete or copy you may find it convenient to use dd or yy. These commands delete or yank the line of text that the cursor is on. dd deletes the current line of text and places it in a buffer. yy copies the current line of text to a buffer while leaving the original text unaltered. Many commands can be preceded by a number. This number indicates the number of times the command is repeated. These commands are no exception, e.g. 3dd deletes the line the cursor is on as well as the two lines below it and places them in a buffer. Text can be retrieved from the text buffer by typing p. The "pull" command inserts the text from the buffer into the text file beginning on the line below the cursor. This method of deleting and yanking works well for text blocks of known length or an easily countable number of lines, but is less satisfactory of large blocks of text.

As a result, vi has another method of text manipulation that involves marking text. Vi is capable of marking 26 different locations in a file. To mark a location in a text file move the cursor to the desired location and type m followed by the name you want to use. Each lowercase letter of the alphabet is a name.

Suppose we have a portion of text we wish to move from one location to another. We can do this by marking the beginning of the text block with the name q, i.e., mq will give the current cursor location the name q. Then we move to the end of the portion of text we wish to move and type d'q. This command deletes everything from the marked position to the cursor position and places it in a buffer. Text in the buffer is retrieved using the "pull" command already described. The "yank" command allows you to copy the text to the buffer instead of deleting it. Typing y'q instead of d'q will place a copy of the text in the buffer and leave the original text unaltered.[footnote]

Search and replace

The search command is /. To search for polite type /polite. n repeats the search in the same direction, and N repeats the search in the opposite direction.

The search option accepts most of the standard Unix pattern matching language. Suppose I had a file that contained the following text:

There was a young man of Milan
Whose poems, they never would scan;
When asked why it was,
He said, `It's because
I always try to get as many words into the last line as I possibly can'.
-anonymous

Here are a few examples (using this text) that you will probably never use but may find inspiring:


    /[a-z]as
will search for any lowercase letter followed by as. In this example, it would find was and last but not as or asked.


    /[^c]an
will search for any an preceded by any character other than a c. In our text it would find Milan but not scan or can.


    /^[A-Z].*\. *$
will search for any line that begins with a capital letter and ends with a period and any number of blanks. Our only match in the example text would be with the last line.

All of these search patterns can be used in the search and replace command that takes on the following structure:


    :s/search_string/replacement_string/g
This command replaces every search_string on the current line with replacement_string. Omitting the g (global) flag at the end of the command will cause only the first occurrence of search_string to be altered. Often you may wish to confirm each replacement. This can be done with the confirm flag c. The confirm flag should be placed after or in place of the g flag. Suppose I had the following line:

Give a skeptic and inch... and he'll take a mile.

and typed


    :s/take a mile/measure it/
I would be left with

Give a skeptic and inch... and he'll measure it.

Any command that begins with a ":" is called a line mode command and performs its duty on the line the cursor is currently on. However, you can override vi's default of operating only on the current line by preceding them with a range of line numbers. For example, if I wanted to replace guy with gal on lines 32 through 56 I would type


    :32,56s/guy/gal/g
Omitting the g would cause only the first occurrence of guy in each line to be replaced. The "." and "$" play a special role in this sort of designation. "." indicates the current line, and "$" indicates the last line of the file. Therefore, if I wanted to deletefrom the current line to the end of the file I would enter:


    :.,$d
I could even do something like:


    :.,/Edison/d
which would delete from the current line to the next line that contained Edison.

One other shortcut that might be worth mentioning is that 1,$ and % both indicate all the lines in the file. Therefore,


    :1,$s/search_string/replacement_string/g
and


    :%s/search_string/replacement_string/g
do exactly the same thing.

Undo

The undo command, u, is another feature that has saved me many times. Pressing u undoes the last command you told vi to perform. Another form of the undo command is U which undoes all the changes made to the current line since you moved there.


Repeat

Often times you may desire to repeat the last command performed. This can be done with the "." command. Place the cursor in the appropriate position and press "." to repeat the most recent command. Suppose I had a C program in which I wished to switch a variable name from no_way to yes_way in two different places. One way I could accomplish this would be to place my cursor on the beginning of the first no_way and type cw (change word) and then type yes_way <esc>. This would accomplish my task for the first case. Now all I would need to do to change the second no_way would be to place my cursor at the beginning of it and type "." to repeat the last command.


1