Previous Table of Contents Next


Moving Text (cut & paste)

By using the yank command you can yank any amount of text based on a range of lines. The yank command is referred to as cutting the text in some editors. The put command is used to put the text back in the buffer. You can also use the move command to move a range of text to a new location, see the previous command.

Yanking (cutting) The text may be yanked to the undo register or to a named register.
range ya reg count Yank the lines specified by range or count to the named register reg. The originals lines are not changed. Valid register names are a through z. If no register name is provided, the text is placed in the undo register. To retrieve text from registers use the put command. If you use capital letters for register names, the text is appended to the register.
range d reg count Delete the lines specified by range or count to the named register reg. The originals lines are deleted. Valid register names are a through z. If no register name is provided, the text is placed in the undo register. To retrieve text from registers use the put command. If you use capital letters for register names, the text is appended to the register.
Example:
:ya 5
Yank five lines to the undo register.
:.,$ ya a
Yank all lines from the current line to the end of the buffer into register a.

Putting (pasting)  The put command is used to retrieve text from a register. The text is not deleted from the register, thus is can be retrieved multiple times.

line pu reg Put data from register reg after line line. If reg is not specified then the undo register is used. If line is not specified then put text after current line.

Moving text between files

You can move text from one file to another without leaving ex. 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.

range ya reg Save the text to a named register (a-z).
e# Switch to alternate buffer/file in the session buffer.
or
e file Switch to a new file in the session buffer.
adr Move to the desired location in the alternate or new file.
pu reg Put lines from register a after current line.

Indenting Text

The following two commands are used to indent and deindent text defined by a certain motion.

range > count Indent the lines specified by range or count by the number of spaces specified in the shiftwidth option.
range < count Deindent the lines specified by range or count by the number of spaces specified in the shiftwidth option.

Filtering Text

Filter commands take the text specified by the motion and send it to a UNIX filter. The text is deleted from the buffer and replaced by the output of the UNIX filter.

range!cmd Send range lines of text to standard input of any UNIX filter command to be manipulated and then returned; the original text is replaced. A filter command must be standard input and output capable (e.g., grep, sed, sort, etc.)
An example is well worth explaining for this command. Assume you have 20 lines of text you need to convert into two-column format instead of the one-column format you currently have in the buffer. To do this you just type in the following command
.,+19!pr -t2
and press Return. The .,+19 range takes all of the text from your current line plus the next 19 lines and sends it to the pr -t2 command. The pr -t2 command reformats the input text into two-column output. Your screen is redrawn with the same data in two-column format. If you don't like the way it looks you can always press u for undo.

Join Lines

The following command is used to join lines together. It removes the new-line that forms a physical line.

range j count flags Joins the lines specified by range or count to create one line. If no range or count are given, the current and next line are joined. If a line ends with a sentence punctuation (., !, ?) two spaces are placed between the last character of the line and the next line joined to it. If the next line begins with a right parenthesis ()) no spaces separate the joined lines.
range j! count flags Same as the j command except no space adjustments are made to the lines that are joined.
Example:
:%p
This is line one.
This is line two.
:1 j p
This is line one. This is line two.

Undo, Redo, and Retrieve

The following commands are used to undo previous commands, repeat previous commands, or retrieve text that was deleted or saved earlier in the edit session.

u Undo last change.
U Undo all changes on current line. Must not move off of the current line.
. Repeat last command.
& Repeat last ex substitute command.
[1-9]pu Retrieve 1-9th last delete from a number register.
pu reg Retrieve previously stored contents from a letter register.
1pu.u.u Scan the number registers for last deleted text.

Edit/Re-edit a File

These commands are used to edit different files without leaving the editor. You can switch between various files by using the e command.

e +cmd file Edit file after interpreting ex command cmd. If the current buffer has been modified since the last save to disk (:w), ex displays a warning and does not edit the new file. If cmd is not provided, the editor enters the new buffer at the last line of the file. The cmd must be an address command.
e! +cmd file Edit file regardless of unsaved changes in current buffer. All modifications you have made since the last save to disk are lost. If no file is specified then the current file is reedited.
e# Edit alternate file if two files are being edited. This does not affect the argument list. If you are editing five files and you decide to edit a different file using the e command, you can use the e# command to return to the previously edited file. If you use the n command, the next file in the argument list is edited.
Examples:
:e text.c Replace the current file being edited with the file text.c.
:e +/main/ myprog.c Edit myprog.c and set the current line to the first line containing the word main.

Edit/Re-edit Multiple Files

These commands allow you to edit a new set of files and move between the different files.

n newargs Replace current argument list with new list of file(s) specified by newargs. If no arguments are specified, then the next file in the argument list is edited. If the current buffer has been modified since the last save to disk, ex displays a warning and does not edit the next file.
n! Edit next file in argument list regardless of unsaved changes. All modifications you have made since the last save to disk are lost.
ar Display current argument list of files to be edited ([current file] is enclosed in brackets).
rew Rewind argument list to beginning, reedit first file.
rew! Rewind argument list to beginning, reedit first file regardless of unsaved changes.
Examples:
:n myfile yourfile afile newfile
The argument list maintained by ex is reset to the four files listed. The file myfile is placed in the buffer for editing.
:n The next file in the list is edited. In this case yourfile is placed in the editor's buffer.
:ar Display the current list of files being edited. The file yourfile is enclosed in brackets since it is the file currently being edited.
:rew Rewind the argument list to the beginning and edit the first file listed.

Read from a File

The r command allows you to read from a file or the output of a UNIX command.

liner file [...] Read in file(s) to current buffer after line line. If line=0 then file is placed at the beginning of buffer.
liner !cmd Read in output of UNIX command(s) and place after line specified by line.
Examples:
:0r !ls -x Reads the output of the ls - x command and places it at the beginning of the buffer.
:$r ../C/myprog.c Reads the file ../C/myprog.c in at the end of the buffer.

Write to a File

The w command allows you to write specified lines of the buffer to a file or to the input of a UNIX command.

range w file Write lines specified by range to the file named file. If range is omitted, then the entire buffer is written. If file is omitted, the current filename is used. If no current filename is known, then ex complains, not knowing where to write the contents.
range w! file Write lines specified by range to disk, overwriting the existing file.
rangew >> file Append lines specified by range to disk file.
rangew !cmd Write the range of lines to the UNIX command. The command will process the input.
Examples:
:w! Write current buffer to disk, overwriting existing file.
:w # Write to the alternate file. You must have an alternate file active; using the :e, :n, or :w command will set the last filename used to the alternate file.
:w >> TMP.% Append current buffer to disk file TMP.currentfile.
:w %.old Write current buffer to file currentfile.old.


Previous Table of Contents Next