CSCI 1320 (Principles of Algorithm Design I):
vi Hints and Tips

Caveat: Last updated in 2007. Use with caution!

Versions/variants of vi

vi is available on just about every Unix system you will ever encounter, but it is a somewhat primitive editor. Newer and more capable variants exist, and in fact vi on the lab machines starts one of them -- vim ("vi improved"). It has many features not found in standard vi, including online help and optional syntax coloring. Start it with one of the following commands: An interactive tutorial can be started by typing vimtutor from the command line.

Modes in vi

vi, unlike many editors, has two distinct "modes", and what happens when you press keys depends on which mode you are in. The modes are: Several commands put you into insert mode -- i, a, etc. The version of vi installed on the CS lab machines displays INSERT at the bottom of the screen if you are in insert mode. To get out of insert mode, press the ESC key.

If you try to type commands while in insert mode, the commands will just be entered in your file. Press the ESC key, delete the commands from your file, and try the commands again.

If you try to enter text while not in insert mode, the text will be interpreted as commands, often with surprising results. If this happens, first get out of insert mode if necessary and then use the "undo" command (u) repeatedly to clean up.

Useful commands

This is a list of the more basic standard vi commands. In the following: In typing a command, do not include the square brackets used to denote optional text. We assume you've started the editor with vi myfile.

vim has one additional feature that those accustomed to more recent editors will likely find useful for working with blocks of text, namely "visual mode". To select a block of text using this feature, put the cursor at the start of the block of text and type v. Now move the cursor to the end of the block. The selected text should show up highlighted. You can now type d to delete the text. Typing p inserts the deleted text just after the current position of the cursor, so you can move a block of text by highlighting it, deleting it, moving to the new location, and typing p.