Adding or inserting text



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

Adding or inserting arbitrary amounts of text requires that you change from "command" mode to "insert" mode. In command mode, single character commands usually take effect immediately when typed. In insert mode, you type the text to be inserted and terminate it (returning to command mode) with the ESC key.

People used to word processing programs on Macintosh or PC will notice a significant difference in text input with vi. On most Mac/PC word processing programs, you do not need to press the RETURN key to signal the end of a line; the program will automatically "wrap" text onto the next line, breaking the lines at a word boundary. If you print such text, the line boundaries are preserved. Using the vi editor, unless you have set a special option (see below), the editor does not break new text into separate lines for you as you type. It "wraps" text around on the screen, but this is just its way of showing a very long line. If you print such text, you will only get the first part of the line (whatever fits on one line for that printer) - the rest will be off the page.

This behavior is a consequence of the fact that Mac/PC word processing programs are mixing the editing and formatting functions, but vi is strictly an editor. In vi, you should remember to press the RETURN key where you want to end each line of new text that you enter.

You can force vi to break lines for you at the right margin when you are typing new text, so you don't have to press the RETURN key on every line. Remember, however, that there are many situations where you would not want this automatic line breaking, for example, when editing data files or computer source code files that may need to include long lines that must not be broken.

If you really want vi to break lines automatically for you, use this command to set automatic line break mode:

:set wrapmargin=n <CR>

The n in this command must be replaced by an integer number of your choosing, which is interpreted by vi to be how close it is allowed to get to the right margin of the screen before it must insert a RETURN character and start a new line. It will only break lines on a blank space between words. Therefore, it may actually break the line sooner than n columns before the right margin. As virtually all terminals and terminal emulation programs use 80 columns as the width of the screen, you can think of this wrapmargin environment variable as forcing the maximum line length for new text to be (80-n) columns.

The "wrapmargin" variable can be abbreviated "wm", for example:

:set wm=5<CR>

To remove this automatic line breaking mode, you can turn off this variable with the command:

:set wm=0<CR>

When typing new text in insert mode, you can use the backspace key to erase the character you just typed, the CTRL-U to erase all new text on the current line, and CTRL-W to erase the last entire word just typed. The "erased" text does not disappear from the screen until you type over it or press the ESC key; this saves time redrawing the screen. The cursor does move to the left, back over the erased text, however, to show that it has been erased from the file (actually, from the buffer).

The actual commands that switch vi into insert mode so you can add new text:

i and a These are the basic insert mode commands. You position the cursor where you desire to insert text and type i to insert before the cursor, or a to insert after the cursor. You then type your text, which can have carriage returns in it (to make multiple lines). When done typing the new text, press the ESC key.

I (uppercase) is the equivalent of i, except that it inserts just before the first non-blank character of the line the cursor is on, regardless of which column the cursor is in. A (uppercase) is the equivalent of a, except that it always adds text at the end of the current line. As usual, terminate new text with ESC.
 

s replaces the character under the cursor with any amount of new text. This differs from i and a, which inserted or added text before or after the cursor position. As usual, terminate new text with ESC.
 
o opens up a new line below the line on which the cursor is positioned and then starts inserting your new text at the beginning of that new line. O (uppercase) opens the new line above the line on which the cursor is positioned. Terminate new text with ESC.
 

 

Comments or Questions?