One shell feature that I do get a lot of use out of is the command history. Command history is also a shell built-in. The basic idea is that the shell keeps track of every command line you have submitted to it and the system time when you hit enter. By default the list is stored in your home directory in a file called .history. This list may be accessed in a variety of ways, most of which are presented next.
If you're not familiar with the tcsh history mechanism, you might want to take a moment and try out some of the options described next. Many of them are worth remembering.
<Up Arrow> | Display older command in the history list. Note that the up and down arrows can be used to shuffle through the entire history list. |
<Down Arrow> | Display newer command in the history list. |
!! | Re-execute previous command. |
!N | Execute Nth command. (Numbering can be displayed by typing 'history'.) |
!-N | Re-execute Nth command back from current command. |
!?string | Re-execute last command starting with string. |
!string | Re-execute last command starting with specified string. |
Somewhat more usefully, you can also display and modify commands stored in the history list. The functionality is different depending on whether you are using vi or Emacs command line editing.
History Keys in vi Mode
Move up the history list | <Up Arrow>, <Esc> k, , <Ctrl> P |
Move down the history list | <Down Arrow>, <Esc> j, -, <Ctrl> N |
Move left on a line | <Left Arrow>, <Esc> h |
Move right on a line | <Right Arrow>, <Esc> l |
Back one word | <Esc> b |
Move to end of line | $ |
If you're not familiar with vi, there's a chapter on it in this book. Briefly, the <Esc> key puts you in command mode and makes your control keys (k, j, h, l, etc.) do things other than type out letters. The aforementioned list is somewhat misleading in that it seems to imply that the <Esc> key must be pressed each time you wish to use a command key. Not so. Once you've pressed <Esc>, you're in command mode and will stay that way until you press one of the following keys to get back out of edit mode:
a | Append after current position. |
i | Insert over current position. |
A | Append at end of line. |
I | Insert at beginning of line. |
History Keys in Emacs Mode
Move up the history list | <Up Arrow>, <Ctrl> P |
Move down the history list | <Down Arrow>, <Ctrl> N |
Move left on a line | <Left Arrow>, <Ctrl> B |
Move right on a line | <Right Arrow>, <Ctrl> F |
Note that the aforementioned list of key bindings is incomplete. Use the following command to display complete list of key bindings:
bindkey
history
[-hTr] [n]
history
-c
Display the user's command history list.
Example: To display the 10 most recent commands in the history list, enter
history 5
-h | Do not display leading numbers in the history list. |
-T | Print timestamps in comment form. |
-r | Reverse default ordering and print most recent commands first. |
n | This numeric variable specifies a limit on the number of history events to be displayed. |
history
-S|-L||-M [filename]
In this form, the history command can be used to store the history list in a file and retrieve it later.
Example: To store the history list in the file history.txt for later retrieval, use
history s history.txt
-S file | Save history list to specified file. |
-L file | Append the history list contained in the specified file to the active history list. |
-M file | Merge history list with specified file. |
history
-c
Clear the currently active history list. Generally this would be a bad idea, but you might use this form of the history command preparatory to loading in an old history list from a text file.