8.8. HistoryThe C shell keeps a record of the commands that you enter from the keyboard so that they may be edited and re-executed at a later stage. This facility is sometimes known as a history mechanism. The ! metacharacter gives you access to history. 8.8.1. Numbered CommandsWhen you're using history, it's very handy to arrange for your prompt to contain the "number" of the command that you're about to enter. To do this, insert the \! character sequence into your prompt: % set prompt = '\! % ' ...include event num in prompt. 1 % echo Genesis ...this command is event #1. Genesis 2 % _ ...the next command will be event #2. 8.8.2. Storage of CommandsA C shell records the last $history commands during a particular session. If $history is not set, a default value of 1 is used. If you want these commands to be accessible from the next session, set the $savehist variable. If you do this, the last $savehist commands are maintained in the file specified by the HISTFILE variable (defaults to $HOME/.history). A history file is shared by all of the interactive C shells created by the same user unless HISTFILE is purposely set to a unique value in each different shell. In the following example, I instructed my shell to remember the last 40 commands in my history list and to store the last 32 commands between sessions: 2 % set history = 40 ...remember the last 40 commands. 40 3 % set savehist = 32 ...save 32 across sessions. 32 4 % _ 8.8.3. Reading HistoryTo obtain a listing of a shell's history, use the built-in command history (Figure 8-17).
Here's an example: 4 % alias h history ...make a useful alias. 5 % h ...list current history. 1 13:25 set prompt = '\! % ' 2 13:26 set history = 40 3 13:26 set savehist = 32 4 13:26 alias h history 5 13:27 h 6 % h -r 3 ...list last 3 commands in reverse order. 6 13:27 h -r 3 5 13:27 h 4 13:26 alias h history 7 % _ 8.8.4. Command Re-executionTo re-execute a previous command, use the ! metacharacter in one of the forms shown in Figure 8-18.
These sequences may be used anywhere in a command line, although they're usually used in isolation. The recalled command is echoed to the terminal before it is executed. The value of prefix or substring may not contain a space. The special meaning of ! is not inhibited by any kind of quote, but may be inhibited by preceding it with a space, tab, =, (, or \. Here are some examples: 41 % echo event 41 ...a simple echo. event 41 42 % echo event 42 ...another simple echo. event 42 43 % !! ...re-execute last command. echo event 42 ...echo command before re-execution. event 42 44 % !41 ...re-execute command #41. echo event 41 ...echo command before re-execution. event 41 45 % !ec ...re-execute command starting with "ec". echo event 41 ...echo command before re-execution. event 41 46 % _ 8.8.5. Accessing Pieces of HistoryYou may access a portion of a previous command by using history modifiers. These are a collection of options that may immediately follow an event specifier. Each modifier returns a single token or range of tokens from the specified event. Figure 8-19 is a list of the modifiers.
The colon before the ^, $, and * options is optional. To use one of these modifiers on the last command, you may precede the modifier by "!!" or just "!". Here are some examples: 48 % echo I like horseback riding ...original line. I like horseback riding 49 % !!:0 !!:1 !!:2 !!:4 ...access specified arguments. 8.8.6. Accessing Portions of FilenamesIf a history modifier refers to a filename, it may be further modified in order to access a particular portion of the name. The existing modifiers may be followed immediately by the following filename modifiers (Figure 8-20).
In the following example I accessed various portions of the original filename by using this filename access facility: 53 % ls /usr/include/stdio.h ...the original. /usr/include/stdio.h 54 % echo !53:1:h ...access head. echo /usr/include /usr/include 55 % echo !53:1:r ...access root. echo /usr/include/stdio /usr/include/stdio 56 % echo !53:1:e ...access extension. echo h h 57 % echo !53:1:t ...access tail. echo stdio.h stdio.h % _ 8.8.7. History SubstitutionThe substitution modifier is replaced by the specified portion of a previous event after a textual substitution is performed. Figure 8-21 gives the syntax.
Here's an example: 58 % ls /usr/include/stdio.h ...the original. /usr/include/stdio.h 58 % echo !58:1:s/stdio/signal/ ...perform substitution. echo /usr/include/signal.h /usr/include/signal.h 59 % _ It's quite common to want to re-execute the previous command with a slight modification. For example, say you misspelled the name of a file. Instead of typing "fil.txt", you meant to type "file.txt". There's a convenient shorthand way to correct such a mistake. If you type the command: ^pat1^pat2 then the previous command is re-executed after the first occurrence of pat1 is replaced by pat2. This shortcut procedure applies only to the previous command. Here's an example: % ls -lG fil.txt ...whoops! ls: File or directory "fil.txt" is not found. % ^fil^file ...quick correction. ls -lG file.txt ...OK. -rw-r-xr-x 1 ables 410 Jun 6 23:58 file.txt |