Event specification for the C shell

To select words from an event, follow the event specification with a colon (:) and one of the following word designators (the words of an input line are numbered sequentially starting from 0)

Item Description
0 First word (the command name)
n nth argument
^ First argument
$ Last argument
% Word matched by an immediately preceding ?string? search
x-y Range of words from the xth word to the yth word
-y Range of words from the first word (0) to the yth word
* First through the last argument, or nothing if there is only one word (the command name) in the event
x* xth argument through the last argument
x- Same as x* but omitting the last argument

If the word designator begins with a ^, $, *, -, or %, you can omit the colon that separates the event specification from the word designator. You can also place a sequence of the following modifiers after the optional word designator, each preceded by a colon:

Item Description
h Removes a trailing path name extension, leaving the head.
r Removes a trailing .xxx component, leaving the root name.
e Removes all but the .xxx trailing extension.
s/OldWord/NewWord/ Substitutes the value of the NewWord variable for the value of the OldWord variable.
The left side of a substitution is not a pattern in the sense of a string recognized by an editor; rather, it is a word, a single unit without blanks. Normally, a slash (/) delimits the original word (OldWord) and its replacement (NewWord). However, you can use any character as the delimiter. In the following example, using the % as a delimiter allows a / to be included in the words:
s%/home/myfile%/home/yourfile%
The shell replaces an ampersand (&) with the OldWord text in the NewWord variable. In the following example, /home/myfile becomes /temp/home/myfile.
s%/home/myfile%/temp&%

The shell replaces a null word in a substitution with either the last substitution or with the last string used in the contextual scan !?String?. You can omit the trailing delimiter (/) if a newline character follows immediately. Use the following modifiers to delimit the history list:

Item Description
t Removes all leading path name components, leaving the tail
& Repeats the previous substitution
g Applies the change globally; that is, all occurrences for each line
p Displays the new command, but does not run it
q Quotes the substituted words, thus preventing further substitutions
x Acts like the q modifier, but breaks into words at blanks, tabs, and newline characters

When using the preceding modifiers, the change applies only to the first modifiable word unless the g modifier precedes the selected modifier.

If you give a history reference without an event specification (for example, !$), the shell uses the previous command as the event. If a previous history reference occurs on the same line, the shell repeats the previous reference. Thus, the following sequence gives the first and last arguments of the command that matches ?foo?.
!?foo?^ !$

A special abbreviation of a history reference occurs when the first nonblank character of an input line is a carat (^). This is equivalent to !:s^, thus providing a convenient shorthand for substitutions on the text of the previous line. The command ^ lb^ lib corrects the spelling of lib in the command.

If necessary, you can enclose a history substitution in braces { } to insulate it from the characters that follow. For example, if you want to use a reference to the command:
ls -ld ~paul
to perform the command:
ls -ld ~paula
use the following construction:
!{l}a
In this example, !{l}a looks for a command starting with l and appends a to the end.