Previous Table of Contents Next


Event Designators

An event designator references a command line stored in the history file. There are several formats of event designators, they are as follows.

! Start a history substitution command, except when followed by a space, tab, new-line, =, or (.
!! Reference the previous command line. Typing !! and pressing Return executes the previous command.
!n Reference command line with event number n from the history file. This references a specific event number. For example, if the current event number is 7, you can specify an event number from 1 to 6. The command !3 would reexecute event 3, vi wsr.092989.
!-n Reference the nth previous command event. The current event number -n. For example, if the current event number is 7 and you type !-3, event number 4 is executed.
!str Reference the most recent command line starting with string str. For example, !vi reexecutes the "vi apiprog.c" command.
!?str[?] Reference the most recent command line containing the string str. For example, !?dly reexecutes the "find ..." command.
!{str} Insulate a reference string from adjacent characters.

Word Designators

Word designators provide a way to reference specific arguments (words) from a command line stored in the history file. A colon (:) separates the event designator and the word designator. If the word designator begins with $, %, ^, *, or - the : is not needed. If you want the word to be selected from the previous command line, the second ! is not required. For example, !!:2 and !:2 both refer to the second word on the previous command. Words are zero relative, that is, word zero is the command itself. The first argument (arg1) on the command line is word 1.

The available word designators are as follows:

# The entire command line.
0 The first word on the command line, argument 0, which is the command itself. For example, !2:0 refers to the word "write" in the previous history listing.
$ The last argument on the command line. For example, !4:$ refers to the word "-print".
% The word matched by the most recent ?s event designator.
^ The first argument (second word) on the command line. For example, !4:^ refers to "/usr/local" on the "find ..." command line.
n The nth (nth + 1 word) on the command line.
* All arguments on the command line. Does not include word 0. This is the same as 1-$.
x-y A range of words starting at word x and continuing through word y.
x* An abbreviated form of x-$, which refers to word x through the last word.
x- The same as x* except the last word is not included.
-y Same as the range 0-y.

Modifiers

You can add modifiers after the word designators. A modifier is used to modify the referenced command line. Each modifier must be preceded with a colon (:).

& Repeat the previous substitution(s/old/new/).
e Remove everything but the suffix.
g Globally apply the change to the first occurrence of a match in each word. The g must precede other modifiers.
h Remove the last filename or directory name from the path. Same as performing a dirname on the designated word.
p Print (display) the command but do not execute it.
q Quote the substituted words, preventing further substitutions.
r Remove a suffix of the form ".xxx," leaving the basename.
s/old/new/ Substitute the new string in place of old string.
t Remove all leading pathname components, leaving the basename.
x Same as q, except words are delimited by spaces, tabs, and new-lines.

History Editing Examples

The following examples illustrate how to use the Event Designators, Word Designators, and the Modifiers.

history List out the history events.
!10 Execute history event number 10.
!{l};.c Search for first event beginning with l and add .c to the end of the command line.
!vi Execute the last command that started with vi.
^bni^bin Substitute "bni" in the previous command line to "bin."
!vi:s/1.c/2.c/ Substitute "1.c" to "2.c" on the last command that started with vi.

RELATED COMMANDS

Refer to the history command described in Module 64.

RELATED FILES

The fc command searches, displays, and edits your ksh history file. The ! (exclamation mark) signals the csh to access your history file as requested.

APPLICATIONS

You use the fc command or an alias thereof to execute previous commands stored in the history file. The commands can be referred to by string or event numbers. This allows you a quick and easy way to reexecute a command without retyping it.

In-line editing lets you edit a previous command before executing it. This provides flexibility in accessing the history file and saves you even more time from retyping commands.


C Shell
The csh provides similar functionality with the ! notation. You use the ! to access previous commands stored in the history file.

TYPICAL OPERATION

In this activity you use the fc (ksh only) command to list the last commands from the history file. Then you execute a command beginning with a specified letter. Begin at the shell prompt.

1.  Type fc -l and press Return. Notice the last 16 (default value) lines of your history file are displayed with event numbers. The history alias command performs the same function.

C Shell
Type history and press Return.

2.  Press <Esc> and type /cat and press Return. Notice that the last cat command you execute is displayed. To execute the cat command press Return or type n to search for the next cat command.

C Shell
Type !cat and press Return to execute the last cat command.

3.  Type r and press Return. The last command that began with a c is executed.

C Shell
Type !c and press Return. The last command that began with a c is executed.

4.  Press <Esc> and type kv and press Return. You are now in the vi editor. Change the name of the file to another filename. Type wc$/etc/motd and press <Esc>. Now type ZZ to exit vi and run the command.
5.  Turn to Module 62 to continue the learning sequence.


Previous Table of Contents Next