While there are indeed many ways described above to reduce the work involved in command line input, it can still be a daunting task to repeat a command over and over. There will often be times where a command or filename has been misspelled and requires reentering, or other times where a command line will be purposely reentered, such as a debugging session with multiple recompilations. The C shell introduces a mechanism for making this much easier, the history mechanism. This is simply a record that is kept detailing the previous commands that the user has entered. These commands can then be accessed in a variety of ways. The simplest is with the up and down arrows on the keyboard. If the keyboard is one with the arrows on the number pad as well as the arrow pad, the current key mapping will determine if the number pad arrow keys will work. They may not. As a simple example of the history mechanism, take the following session:
% emacs myprog.cc % g++ -I /users/normb/my_include_files -o myprog myprog.cc % myprog various annoying run time errors %
The user must now attempt to find the cause of the errors and fix them. Rather than retype the lines again he uses the arrow keys:
% myprog (up once) % g++ -I /users/normb/my_include_files -o myprog myprog.cc (up twice) % emacs myprog.cc (up three times -- the desired result)
It should be noted that the text in parentheses is the author's notes, and that each successive arrow keypress would display the command on the same line. For this particular example only a few keystrokes are saved but as the process continued it is rather obvious that the arrow keys are the preferred alternative. The command history has a retricted size as it is stored in memory as opposed to on the disk. The actual number will vary from machine to machine, but usually 20 to 40 is reasonable. The number can be set with the history environment variable which will be covered later. When the number of history commands exceeds the limit, the oldest are removed to make room for the newest.
While the arrow keys prove very useful in accessing previous command lines, they are not the only way to access the history list. The C shell provides different ways to access the command lines depending on taste and situation. Before looking at the different mthods of accessing the list, it would probably be a good idea to see what the list looks like. The history list for the previous example would look something like this:
% history 1 emacs myprog.cc 2 g++ -I /users/normb/my_include_files -o myprog myprog.cc 3 myprog %
The actual numbering may vary depending on how many commands have been
entered.
Now, looking at this command list it is easy to see that if the list
gets quite long, say 10 or more items, the number of keystrokes saved
begins to diminish.
The C shell alleviates this problem by giving the user a variety of
ways in which to access earlier commands.
The C shell uses the exclamation mark (!) as one quick
method to make history substitutions.
By entering two consecutive !'s and the enter key the user will
have the last command in the history list executed.
The last command would now appear twice as the last two entries in the
history list.
By entering the ! followed directly by a number will execute the
entry in the history list with the entered number.
For example, the command !1 would execute the command
emacs myprog.cc in the history list above.
Another method is to enter a string after the !, which would
have the command starting with that string in the history list
executed.
For example (again using the above history list), the command
!g++ would invoke the rather length command:
g++ -I /users/normb/my_include_files -o myprog myprog.cc
Notice that the string was entered immediately after the !
character.
If this was not the case, an error would occur as the !
character alone is not a recognized command by the shell.
If there is more than one item in the history list that starts with
the string, the one closest to the end of the list (the one with the
highest number) will be substituted.
By framing the string with question marks, like !?string?, the
shell will execute the command which contains the string anywhere in
it.
To execute the command in the last example the user could have entered
the command !?-o.
The history list can also be executed from the bottom up.
For instance, !-2 would execute the second last command added
to the history list.
By default, when the user logs out, the history list is purged from
memory and lost.
The user can however set the variable savehist to a number will
save that number (or less if not as many commands exist in the history
list) to a file called .history in the users home directory.
The larger the number of commands saved from the history list, the
longer it will take the C shell to start at login time.
This is because the saved history list has to loaded into memory.
Typically 20 to 40 is a good range to set the savehist variable
to.
To ensure that this variable is set for each session, it can be added
to the .cshrc file.
At this point the reader may well be overwelmed by the number of
different ways to save keystrokes.
It might seem that there is an awful lot of remembering involved in
saving a few keystrokes.
While this is definitely true, the old adage holds, practice makes
perfect.
It will likely take many hours of UNIX use before many of these tricks
will be burned into a users mind - but they will.
Every UNIX user has his own bag of tricks to get the job done, and it
will differ from one user to the next.