Using 'history' to Repeat Commands
Ever want to repeat the command you just entered? The "history" command gives you a method for repeating commands entered earlier in your session.
A complete description of "history" can be found in the man page for the C shell. Enter
man csh
This help text provides a brief overview of "history".
History Functions
The C shell will keep a record of your most recent commands and allow you to reuse them.
You can control how many commands are remembered by putting a command in your .login file.
set history=23
This statement tells the C Shell that you would like to keep track of the last 23 commands you have given the system. Twenty-three is a good number to use here as most CRT terminals can hold only 24 lines of text; therefore, setting "history" to 23 gives you a full screen of previous commands plus the prompt.
To see this history listing, enter the command "history". You should see a display like this one.
1 mv taxes/prop/house folks/htax
2 cd folks
3 vi cshinfo
4 more ../.login
5 man tset
6 cp /student/math5744/jdeere/calcs/grp3 proj/hisfile
7 vi sctest
8 more duo
9 lpr cshinfo
10 cd ~/handbooks/unix
11 vi uhist.mss
12 history
This user has performed twelve jobs (there have been twelve "events") since he signed on. Notice that the system lists "history" as the most recent command.
Using the History List
The UNIX Programmer's Manual has detailed information about using the history substitution capabilities: in CSH(1), volume 1, and "An Introduction to the C Shell," in volume 2C. However, the following information should be sufficient for most users. There are three parts to the statement which you use to access the history list:
historical reference:word reference:modifier
Note that you separate these three variables from each other by colons. The historical reference begins with an exclamation mark (at the beginning of the command). The characters typed immediately after the exclamation mark identify the event you wish to refer to. Here are the four most common ways you can tell the system to reference your history list:
General
Form Example Explanation
------- ------- -----------
!n !11 repeat event 11
!-n !-2 repeat the command
entered 2 events
!alpha !v repeat the most recent
command beginning with "v"
!alpha !ma repeat the most recent
command beginning with
"ma"
!! !! repeats command
Note that the first three examples above are all ways to repeat command 11 from the sample history list. You would choose one of these commands since all three commands are shorthand for "vi uhist.mss". Note that the fourth example, "!ma", refers to command 5 in the sample list. "!m" would cause the history mechanism to repeat the most recent command beginning with "m"-- that is, command 8, "more duo".
Frequently, however, you do not want to repeat a command verbatim but need to modify it in some way: for instance, you might want to replace one argument, add another, or correct a spelling mistake. Adding something to the end of a repeated command is easy: just reference the command and append what you wish to add. Modifying commands or correcting errors is a little more involved; you must use the other two variables as well as the historical reference.
You must tell the system what word or words you wish to reuse from the command you have referred to. Here is a list of the more useful word references:
Reference Description
--------- --------------------------------------------
n use argument number "n" of the referenced
command
0 use the command word i.e. the first word of
the referenced command
x-y use word number "x" through word number "y"
\$ use the last word
x-\$ use all words beginning with word number "x"
ending with the last one
x- use all words beginning with word number "x"
ending with the next-to-last
Next, you must tell the system what modifications to make. If you want to see what modifications are possible, see CSH(1) in volume 1 of The Unix Programmer's Manual.
There are many "modifiers" you could use, but the most useful is "s", short for "substitute":
Substitute
Commands Description
-------- ----------------------------------------------
s/x/z/ substitute "z" for "x"
s?/x?x? substitute "x" for "/x" (Note: There are
other ways of doing this, but this is one of
the easiest. "s//x/x/" will not work. You
have to remember that "/" cannot be used both
as a delimiter and as part of the character
string!)
Sample Commands
Examples Description
----------------- ---------------------------------
!!:0- hf reproduces the previous command
substituting "hf" for the last
argument
!8:0-:s/tx/nw/ ff reproduces command 8
substituting "nw" f and ff for
the last argument
!9:s/bok/book/ reproduces command 9 substituting
"book" for "bok"
