-->
Previous Table of Contents Next


Command History

The tcsh shell provides a mechanism for accessing the command history that is similar to ones provided with bash and pdksh. The shell remembers the last history commands that were entered into the shell (where history is a user-definable tcsh variable).

tcsh stores the text of the last history commands in a history list. When you log in to your account, the history list is initialized from a history file. The default filename for the history file is .history, but you can change it using the histfile tcsh variable. This file is located in your home directory. Notice that the file begins with a period. This means that the file is a hidden file and appears in a directory listing only if you use the -a or -A options of the ls command.


Note:  
In order for the history list to be saved in the history file, make sure that the savehist variable is set to the number of commands that you want to save. Refer to the .login file listing in the “Customizing tcsh” section of this chapter for an example of setting this variable.

The simplest way of using the history list is to use the up- and down-arrow keys to scroll through the commands that were entered earlier. Pressing the up-arrow key causes the last command entered to appear on the command line. Pressing the up-arrow key again puts the command before that on the command line, and so on. If you move up in the command buffer past the command that you want, you can move down the history list one command at a time by pressing the down-arrow key.

The command that is on the command line can be edited. You can use the left- and right-arrow keys to move along the command line, and you can insert text at any point. You can also delete text from the command line by using the Backspace or Delete key. Most users should find these simple editing commands sufficient, but for those who do not, tcsh also supports a wide range of equivalent emacs and vi editing commands. See the “Key Bindings” section of this chapter for more information on vi and emacs command-line editing.

Another method of using the history file is to display and edit the history list using a number of other editing commands that tcsh provides. The history command can be invoked by any one of three different methods. The first method has the following command-line syntax:


history [-hr] [n]

This form of the history command displays the history list to the screen. The n option is used to specify the number of commands to display. If the n option is not used, the history command displays the entire history list. The -h option causes history to remove the command numbers and timestamps that are usually present in the output of the history command. The -r option tells history to display the commands in reverse order, starting with the most recent command. The following command displays the last five commands that were entered:


history 5

The second method of invoking the history command is used to modify the contents of the history file or the history list. It has the following command-line syntax:


history -S | -L | -M [filename]

The -S option writes the history list to a file. The -L option appends a history file to the current history list. The -M option merges the contents of the history file with the current history list and sorts the resulting list by the timestamp contained with each command.


Note:  
All of the options for the second form of the history command use the filename option as the name of the history file. If no filename is specified, the history command uses the value of the histfile variable. If the histfile variable isn’t set, it uses the ~/.history (home directory) file.

The history command using the -c option clears the current history list.

In addition to the history command and its options, tcsh also contains many history navigation and editing commands. The following commands are used to navigate through the history list:

  !nre-executes the command with the history number of n.
  !-n re-executes the command that is n commands from the end of the history list.
  !! re-executes the last command that was entered.
  !c re-executes the last command in the history list that begins with the letter c.
  !?c? re-executes the last command in the history list that contains the letter c.

The history editing commands enable you to replace words and letters in previously entered commands as well as add words to the end of previously entered commands. More information on these editing commands can be found by referring to the tcsh man page. You can view this man page by entering the following command at the shell prompt:


man tcsh


Previous Table of Contents Next