Previous Table of Contents Next

Moving text between files

You can move text from one file to another without leaving vi. You must first save the text to a named register then switch to the other file, move to the location you wish to place the saved text, and then put it in the buffer.

REGyMOT Save the text to a named register (a-z).
:e# Switch to alternate buffer/file in the vi session buffer.
- or -
:e file Switch to a new file in the vi session buffer.
MOT Move to the desired location in the alternate or new file.
REGp Put lines from register after current line.


Macros are a special feature of the vi editor that allows you to store multiple keystrokes and reference them with a single or shorter set of keystrokes. They are designed to save keystrokes. You can have multiple vi or ex commands defined in one macro. There are two types of macros, the string macro and the register macro.

Macros can be used in ex or vi. The string macros are set up in ex mode. The register macros may be set up in either ex or vi mode. The descriptions listed below for register macros are using the vi mode method because it is easier and more common.

String Macros (Mapping)

String macros are stored in the map area of ex. There are two types of string macros; one is used in command mode, the other is used in insert mode. You create string macros using the ex map command. You can remap an existing command key to have a new meaning or you can use one of the unused characters.

Command Mode String Macros  The following command formats are used to create command mode string macros.

:map c act Creates a command mode character command named c that performs the act action when pressed. Act may be any series of actions to create your own single-character command.
:map #n act Map function key n to a specific action.
:map List all map commands.
:unmap c Remove c from map table.

For example, you may want to edit multiple files from the same command line. Normally, you have to type :n and press Return to edit the next file. You can map a character to perform this function for you. The following key sequence sets the character \ (backslash) to perform the :n<cr> for you.

     :map \ :n<Ctrl-V><Ctrl-M>

The Ctrl-V is used to escape the meaning of the Ctrl-M (Return).

Insert Mode String Macros  The following formats are used to create insert mode string macros.

:map! short long Creates an insert mode macro named short that inserts the characters in long when it is typed. You can place an Escape character in the long string to enter command mode, then return to insert mode.
:map! List all insert map commands.
:unmap! short Remove insert c from map table.

The string defined by short is interpreted as soon as the last character of the string is entered. For example, if you defined the following macro

     :map! usv UNIX System V Release 3.2

and you enter insert mode and type usv, vi immediately expands the string to UNIX System V Release 3.2.

Input Mode Motion Definitions  You can make vi perform somewhat like a word processor or true screen editor by defining input macros. For example, if you press the Up Arrow key in command mode, the cursor moves up one line. But in insert mode you insert strange characters or return to command mode. In a word processor you press the Up Arrow key in insert or noninsert mode and the cursor moves up a line.

In vi you set the definition of your cursor keys to the command that would leave insert mode, perform the motion command, and then return to insert mode. The following table defines the more popular definitions for a vt100 type terminal.

Input Key
Input Key

Up Arrow <Esc>OA <Esc>ka Move up one line
Down Arrow <Esc>OB <Esc>ja Move down one line
Left Arrow <Esc>OC <Esc>ha Move left one space
Left Arrow <Esc>OD <Esc>la Move right one space

To map the first one of these in vi simply type the following:

     :map ^V^[OA ^V^[ka

You will probably want to place these definitions in your .exrc file. The command is the same except you leave off the : (colon).

Register Macros

Register macros are stored in named registers. They may be multiple lines in length and consist of vi or ex commands. They can be used in vi or ex mode. To invoke a register macro you press @x, where x is the name of the register where the commands are stored. To store commands for use as a register macro, you enter insert mode and type in the vi or ex commands you wish to execute when the macro is used. Then exit insert mode and delete the lines displayed to the desired named register.

The following commands are used to invoke a register macro.

@x Invoke commands (macro) stored in register x while in vi.
:@x Invoke commands in register x while in ex; acts strange on some REs.
@@ Repeat last macro.

Example Macro:

o:%p Press o to enter insert mode, enter the ex command :%p (used to print out the entire buffer contents to your screen). Exit insert mode by pressing Esc.
"pdd Save the :%p line to register p and remove it from the session buffer.
@p Invoke the macro, the :%p command is executed and the entire file is displayed on your terminal screen.


Abbreviations are almost exactly like the insert mode string macros. They are designed to save keystrokes. The difference between abbreviations and insert string macros is that abbreviation strings are interpreted only if the string is followed by a space, tab or new-line.

:ab abr string Abbreviate abr for string. Each time abr<space> is entered in insert mode, vi automatically replaces it with string.
:ab List all abbreviations.
:unab abr Unabbreviate abbreviation abr.

The following examples may be useful when using vi.

:ab hte the Abbreviate the each time hte is entered in insert mode. A file of commonly misspelled words can be created to provide an automatic spell correction utility based on this use of abr.
:ab swbt Southwestern Bell Telephone
Substitute a short acronym for a long name. Then when you enter swbt<space> in insert mode the long name is substituted in its place.


~ Invert current character case (upper/lower) and move right one space.
xp Transpose two characters (delete and put).
dwwP Transpose two words.
ddp Transpose two lines.


u Undo last change.
U Undo all changes on current line.
. Repeat last command.
& Repeat last ex substitute command.
[1-9]p Retrieve 1-9th last delete from a number register.
REGp Retrieve previously stored contents from a named register.
1pu.u.u Scan the number registers for last deleted text.


=nMOT Pretty print n LISP s-expressions.
. Repeat last vi command.
J Join next line to current line.


ZZ Save buffer to disk and quit vi.
:wq [ file ] Save buffer to disk and quit vi.
:x [ file ] Save buffer to disk and quit vi.
:q Quit vi if no updates since last save.
:q! Quit vi without checking for unsaved updates.
:sh Leave vi, go to subshell, press Ctrl-D, or enter exit to return to vi.
Ctrl-Z Stop editor, place in background, and return to top level shell. Berkeley UNIX only, fg command resumes editing.
: Enter temporary ex mode; perform one ex command.
Q Enter ex mode; perform multiple ex commands until "open" or "vi" is entered at the colon prompt.


Substitutions are handled by the ex portion of the vi editor. Refer to Module 43 for further discussion about ex and substituting.


Tags are discussed in Module 43, which discusses the ex editor.


Appendix G contains a quick reference for vi.


The ex/vi editor has many options that you can set to customize how the editor functions. Please refer to Module 43 for a description of all supported options.


The vi editor uses the terminal definition entries stored in the TERMINFO database. If these entries are corrupted or deleted, vi cannot function correctly for the affected terminal type.

If you have saved text in named registers, vi will not warn you of this fact when you decide to exit the file. Thus you may lose the only copy of the text since you stored it in a named register. The solution is to always put any text back from your named register before you write (:w) your buffer to disk.


Refer to the ed, ex, sed, grep, egrep, and fgrep commands described in modules 39, 43, 117, 60, 40, and 52.


The vi editor uses the TERMINFO database files to know what screen control sequences relate to your terminal type. It also keeps a buffer copy of your original file in the /tmp directory. The files usually start with EX followed by a number.


You use vi to edit any ASCII text file. It is the most common editor and is distributed on most, if not all, UNIX systems. It can be used to enter program source code, create documents, edit ASCII databases, or any other text file. It has many powerful features that are well worth spending the time to learn. An expert vi user can perform a task up to 5 times faster than a novice vi user. The more you understand how vi works and the shortcuts it offers, the more efficient you will become.


In this activity you use the vi command to edit multiple files. Begin at the shell prompt.

Before starting the following activity make sure your TERM variable is set correctly. If you need help deciding the proper type or have problems when you enter the vi editor, contact your system administrator.

Previous Table of Contents Next