3.0 - How do you do a search and replace?

Well, there are a few methods. The simplest is:

:s/old/new/g But, this only does it on the current line... So:
:%s/old/new/g In general,

Where [range] is any line range, including numbers, $ (end of file), . (current location), % (current file), or just two numbers with a dash between them. (Or even: .,+5 to mean the next five lines). [cgi] is either c, g, i, or nothing. c tells vi to prompt you before the changes, g to change all of the occurrences on a line. i tells vi to be case insensitive on the search. No character after the last slash will only change the first occurrence on the line.

My favorite method is:

:g/foobar/s/bar/baz/g This searches for foobar, and changes it to foobaz. It will leave jailbars alone, which the other method will not. This is my favorite method, but is harder to remember. Of course you can also use regular expression search patterns, and a few other commands in the replacement part of the text. If you use \( and \) in the pattern to escape a sequence, you can do lots of nifty things.

For example:

:g/\(foo\)\(bar\)/s/\2/\1baz/g will change foobar for foobaz.

Special sequences allowed are:

& everything which was matched by the search
\[1-9] The contents of the 1st-9th \(\) pair
\u The next character will be made uppercase
\U The characters until \e or \E will be made uppercase
\l The next character will be made lowercase
\L The characters until \e or \E will be made lowercase
\[eE] end the selection for making upper or lowercase

3.1 - How do I run a program from within vi?

:!cmd will run the program cmd. :sh will run an interactive shell. Within this shell, you may, if you want, run vi again. This is particularly useful when you are editing makefiles and config files for programs in an attempt to get a program to compile. The advantage over :e is that you do not need to save the file, and it will be in its old place when you exit the shell. (I advise saving the file anyway...)

3.2 - Ahhh!! I was writing my dissertation, and the computer crashed!

Well, you should get mail about this, but you should be able to recover the file by typing vi -r <filename> where <filename> is the name of the file that you were editing at the time of the crash. vi -r will give you a list of files that can be recovered.

3.3 - Any tips for making vi programmer friendly?

:set ai will make it auto-indent for you.
:set sw=# where # is a number will set the shiftwidth (tabwidth).

You can then use <<, >> to shift a line left or right. Plus, you can use <% to shift a {, ( or [ set left or right (with >%). You must be on top of the specific {, }, (, ), [ or ] of the pair to shift them.

:set sm will show the matching {, ( or [ when you type the closing one.
:set lisp will make some changes that are useful for lisp programming. () will move back and forth over s-expressions, and {} will move without stopping at atoms.

3.4 - Macros -- How do I write them?

:map <lhs> <rhs> where <lhs> is up to ten characters and <rhs> is up to 100. This will make it so that whenever you type <lhs> it will replace it with <rhs>. All macros should start in command mode, but may end in any mode you desire. Remember to use ^V before any control characters that you may use.

:unmap <lhs> will remove the macro. :map! <lhs> <rhs> will make <lhs> insert <rhs> into the text of the document.

3.5 - How do I make a function key a Macro?

If <lhs> is #n where n is 0-9, it will be mapped to the appropriate function key.

3.6 - Is there anyway to abbreviate text?

Yep, of course. This is vi, it can do anything. :ab email ellidz@midway.uchicago.edu will make it so that whenever you type email as a specific word, it will extend it to the entire unabbreviated word. :una email will unabbreviate it.

3.7 - How do I spell check the current document?

Here is a macro to do it. These should be put in your .exrc file. (More on .exrc files later on.) It is a pretty simple macro, it just calls ispell on the current file. Of course, to use this you need ispell on your system. To use it, just hit V with vi. (V is not used by vi, so it makes a good key.)

map V :w^M:!ispell % ^M:e!^M^M

The second ^M makes it so that one does not need to hit return after it is done checking the spelling.

3.8 - I've got a hardcopy terminal, can I still use vi?

Okay, okay, so I don't expect anyone to actually ask this... But, I thought it was bizarre enough to throw in anyway. (And, it actually answers a very common question...)

vi will start up in a specific mode, called "open mode" in this situation. Things work more or less the same. Deleted characters will appear on your print out at \'s. vi will act as if the size of the window is only one line. ^r will retype the current line. z redraws the window around the current line.

3.9 - Oh, okay, is THAT what open mode is? But I don't have a hardcopy terminal, and it still starts in open mode!

Well, what is happening here is that vi doesn't know what type of terminal you have. It decides that in this situation the best thing to do is to assume that you have the worst terminal possible. This might not seem useful, as not very many people need open mode, but it also is the mode that needs to know the least information about your terminal.

Now, how to deal with it. It is possible to change it for the specific session, but in general, this is not useful. If you know your terminal type, you can set it from the Unix prompt (setenv TERM <termtype> under csh and it's variants, and: TERM=<termtype> ; export TERM under sh and its variants.).

Better yet would to be to edit your .profile or .chsrc to include this so it is automatically done for you when you login. (Of course, you need to either know ed or be able to set it at the unix prompt before you'll be able to edit the file...)

If you do not know your terminal type, try vt100. Most modern terminals and terminal emulators can emulate vt100. If this does not work, find someone to help you.

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.