Miscellaneous vi features



Last revision August 2, 2004

Table of Contents:
  1. Editor choices on Unix
  2. Characteristics, advantages, and disadvantages of vi
  3. Basic text editing operations in vi
  4. Regular expressions
  5. File searching with grep
  6. More about regular expressions
  7. Intermediate text editing with vi
  8. Vi Quick Reference

Several set commands have been introduced in these pages. These commands control some aspect of the vi environment, such as whether end-of-line characters are automatically added ("wrapmargin" feature) or line number are shown on the screen ("number" feature). There are many other environment characteristics that can be modified by set commands. See the online manual entry (man vi) for the full list of environment variables.

If you always want a particular environment variable to be set, you can create an initialization file containing the set commands you want, just as you would issue them while in a vi session (but without the colon (:) command). Whenever vi starts up, it will read the initialization file and apply the set commands listed there automatically. This initialization file must be named .exrc (the leading dot character is part of the name). If this file is located in your home directory, it will apply to all vi sessions. If you want different settings while editing files in a particular sub-directory (such as editing computer source code), you can put a separate .exrc file in that sub-directory with the alternate set of commands that you want for editing files there.

For example, if you generally want automatic line breaking to be in effect, you might create the file .exrc in my home directory containing the single line

set wm=5

But in a sub-directory named program, you might be working on a Fortran source code program where you do not want automatic line breaking, but you do want to see line numbers on screen. So in that sub-directory, you might create a separate .exrc file with these two lines:

set wm=0
set number

Use CTRL-L to redraw the screen if messed up (by a write command from another person, for instance).

The parentheses, square brackets, and curly braces characters are commonly used in programming (and text formatting) to delimit sections and are often nested. All kinds of bizarre errors occur when they are not properly nested. Vi helps you find whether your bracketing characters are correctly paired up with the % command. Just put the cursor on one bracketing character (for example, an opening parenthesis), and press the % key. Vi will now move the cursor to the matching character (for example, a closing parenthesis), after correctly accounting for all instances of intervening nested pairs.

To move the cursor to a particular column in the line, use this command:

x|

where x is the column number.

Comments or Questions?