AIX Tip of the Week

AIX Tip of the Week: Entering Special Key Characters in vi

Audience: AIX Users

Date: September 17, 1999

This tip illustrates a little known, but often useful technique for entering special characters in the vi editor.

When using "vi" in Unix to edit a DOS text file, you will see the "^M" at the end of each line. The "^M" is DOS's end of line terminator. This terminator should be removed as it can cause problems in Unix programs. You can remove the "^M" character using either AIX's "dos2aix" command, or in the vi editor using search and replace. However, in vi there is no "^M" key, so this character has to be entered using a special escape sequence. The following vi search and replace command illustrates this technique. It removes all "^M" line terminators in a file.

<.ESC>.:%s/V//

Where <.ESC>.is the escape key, <.Ctrl>.V is the combination of the control and V keys, and <.Enter>. is the enter key.

I've found this technique to be useful in a number of situations. In vi, it can be used to join multiple lines, customize the function keys to do complex tasks, search across lines, not to mention removing the "^M" character in DOS files. I've also used it in shell scripts to simulate keyboard input from a file.