Previously executed commands are stored in a history list. The C shell lets you access this list so you can verify commands, repeat them, or execute modified versions of them. The history built-in command displays the history list; the predefined variables histchars, history, and savehist also affect the history mechanism. Accessing the history list involves three things:
Making command substitutions (using ! and ^)
Making argument substitutions (specific words within a command)
Using modifiers to extract or replace parts of a command or word
| ! | Begin a history substitution | 
| !! | Previous command | 
| !N | Command number N in history list | 
| !-N | Nth command back from current command | 
| !string | Most recent command that starts with string  | 
| !?string? | Most recent command that contains string  | 
| !?string?% | Most recent command argument that contains string  | 
| !$ | Last argument of previous command | 
| !!string | Previous command, then append string  | 
| !N string | Command N, then append string | 
| !{s1}s2 | Most recent command starting with string s1, then append string s2  | 
| ^old^new^ | Quick substitution; change string old to new in previous command; execute modified command  | 
The following command is assumed:
3% vi cprogs/01.c ch002 ch03
| Event Number | Command Typed | Command Executed | 
|---|---|---|
| 4 | ^00^0 | vi cprogs/01.c ch02 ch03 | 
| 5 | nroff !* | nroff cprogs/01.c ch02 ch03 | 
| 6 | nroff !$ | nroff ch03 | 
| 7 | !vi | vi cprogs/01.c ch02 ch03 | 
| 8 | !6 | nroff ch03 | 
| 9 | !?01 | vi cprogs/01.c ch02 ch03 | 
| 10 | !{nr}.new | nroff ch03.new | 
| 11 | !!|lp | nroff ch03.new | lp | 
| 12 | more !?pr?% | more cprogs/01.c | 
Word specifiers allow you to retrieve individual words from previous command lines. Colons may precede any word specifier. After an event number, colons are optional unless shown here.
| :0 | Command name | 
| :n | Argument number n | 
| ^ | First argument | 
| $ | Last argument | 
| :n-m | Arguments n through m | 
| -m | Words 0 through m; same as :0-m  | 
| :n- | Arguments n through next-to-last | 
| :n* | Arguments n through last; same as n-$  | 
| * | All arguments; same as ^-$ or 1-$ | 
| # | Current command line up to this point; fairly useless | 
The following command is assumed:
13% cat ch01 ch02 ch03 biblio back
| Event Number | Command Typed | Command Executed | 
|---|---|---|
| 14 | ls !13^ | ls ch01 | 
| 15 | sort !13:* | sort ch01 ch02 ch03 biblio back | 
| 16 | lp !cat:3* | lp ch03 biblio back | 
| 17 | !cat:0-3 | cat ch01 ch02 ch03 | 
| 18 | vi !-5:4 | vi biblio | 
Command and word substitutions can be modified by one or more of these:
| :p | Display command but don't execute. | 
| :s/old/new | Substitute string new for old, first instance only.  | 
| :gs/old/new | Substitute string new for old, all instances. | 
| :& | Repeat previous substitution (:s or ^ command), first instance only.  | 
| :g& | Repeat previous substitution, all instances. | 
| :q | Quote a word list. | 
| :x | Quote separate words. | 
| :r | Extract the first available pathname root. | 
| :gr | Extract all pathname roots. | 
| :e | Extract the first available pathname extension. | 
| :ge | Extract all pathname extensions. | 
| :h | Extract the first available pathname header. | 
| :gh | Extract all pathname headers. | 
| :t | Extract the first available pathname tail. | 
| :gt | Extract all pathname tails. | 
From the table in Section 5.5.4, command number 17 is:
17% cat ch01 ch02 ch03
| Event # | Command Typed | Command Executed | 
|---|---|---|
| 19 | !17:s/ch/CH/ | cat CH01 ch02 ch03 | 
| 20 | !:g& | cat CH01 CH02 CH03 | 
| 21 | !more:p | more cprogs/01.c (displayed only) | 
| 22 | cd !$:h | cd cprogs | 
| 23 | vi !mo:$:t | vi 01.c | 
| 24 | grep stdio !$ | grep stdio 01.c | 
| 25 | ^stdio^include stdio^:q | grep "include stdio" 01.c | 
| 26 | nroff !21:t:p | nroff 01.c (is that want I wanted?) | 
| 27 | !! | nroff 01.c (execute it) | 
Copyright © 2003 O'Reilly & Associates. All rights reserved.