SMU School of Engineering
General Use UNIX Machines

Useful Vi Tips

What is the best way to learn Vi?

My advice is for people to follow the tutorials and read the man page. Experience is the best teacher!

How do I get into a file using Vi?

Just type Vi filename (where filename is any name you want).

How do I move the cursor in the file?

How do I insert text?

Once you entered input mode, the way to return to command mode is by hitting the Escape key: ESC or CONTROL-[.

How do I delete something?

How to undo and repeat commands?

I just finished my document, how do I save?

There are several save options. One of these should work for you:

How can I include text in my document?

How can I edit my text?

Is there a way I can cut and paste?

The yanked lines can be put as often as one likes, which is a good way to repeat things. Also, deleted lines can be put as well, so dd is the same as yy dd. Also, there is only one default buffer. To move two things, or to yank and put with other work in between, you can save to named buffers. For example "ayy will yank a line in buffer a. Then buffer a can be pasted using "ap.

How do I change the case of letters?

Use the tilde (~) to toggle the case (lower/upper) of letters. It will ignore non alphabetical characters.

How do I switch between files when editing multiple files?

When editing several files, you can switch to file #x using the command: :e#x

Is there a way I can switch two characters?

You can reverse 2 characters in a word by typing xp (x to delete and p to paste).

How can I search for specific text?

Can deleting be used with a search operation?

d/pattern ESC deletes up to the pattern, but leaves the pattern. Then neat thing about this principle is it works with most other Vi commands such as y.

What are some examples of search and replace patterns?

As usual, type u to undo and & to repeat a substitution.

Is there a way to execute a unix command from inside Vi?

Type :!cmd where cmd is a unix command.

How can I set options inside Vi?

Type :!set name value where name is the option you want to change and value is what you want to set it to.
For a list of options, type :set all.

I'm tired of having to redefine all of my options every time I go into Vi, is there any way around this?

You can setup a file named .exrc in your home directory that has the following syntax:
set ai
set wrapmargin=1
map @* I/*^[A*/^[
map @f !}fmt^M
map @d :r !date +"\%a \%b \%d \%y"^M
map @t :%s/^I/    /g^M

I'll explain this line by line:

1: sets the autoindent mode
2: sets a wrapmargin so text will auto-wrap at end of line
3: implements line commenting (C languagne). To comment a line, hit @*
4: implements paragraph formatting. To reformat a paragraph, hit @f
5: implements a current date function. To insert the current date, hit @d
6: converts tabs to 4 spaces. All you need to do is hit @t

Notes:

How do I reformat paragraphs in Vi?

Rather than write a set of macros to do this for you, it's usually easier just t o use an external program that does it for you. If you have fmt(1), then {!}fmt will reformat the paragraph under the cursor. This can be fitted into a macro easily enough.

I hope this answers many of your questions. If you have more questions, you can check the USENET Vi FAQ.
Good luck, and have fun!