tcsh History Mechanisms


tcsh keeps a list of commands you have typed recently.


  • Some features are available to help you correct mistakes or save you from typing whole commands repeatedly.
  • You can change one string in the previous command to a different string with ^ (caret) characters on the next command line:
    % ls
    % rm long-flie-name
    long-flie-name: No such file or directory
    % ^li^il
    rm long-file-name
  • As with all history operations, the shell shows you resulting command line after its editing but you do not get a chance to say whether or not you really want to do the resulting command.
  • The general form of substitution is:
    % ^from^to^
  • The shell will look through the previous command and change the first occurence of from to to.
  • Make sure to give enough characters to match what you want and not a set of characters farther to the left in the previous command.
  • tcsh uses the history shell variable as the number of commands to save.
  • The default value is somewhat small, but can set it with:
    % set history = 100
  • Set the histdup variable to indicate that duplicate events should not be saved.
  • If the first word of the savehist shell variable is a number, then that is the number of commands that will be saved when you log off.
  • The second word can be merge if you want current history merged with contents of your ~/.history file.
  • The history command shows you commands entered in the history:
    % echo $history
    % set history = 200
    % echo $history
    % history
    	1  21:37   echo $history
    	2  21:37   set history = 200
    	3  21:37   echo $history
    	4  21:37   history
  • The first number is the number of the history item, the second number is the time of day it was executed, and the rest of the line is the command.
  • Because the command number is sometimes useful, many people make that part of their prompt:
    % set prompt = "\! % "
    11 % ls
    12 %
  • If the string making up the prompt has "!" in it, that "!" will be replaced with the number of the command about to be entered.
  • The "!" character is used to indicate history substitutions:
    • !n gets replaced by the command nin the history listing.
    • !-n gets replaced by command nback in the history listing (relative).
    • !! gets replaced with the previous command (can be just ! if followed by a word selector described below).
    • !string gets replaced by the most recent command beginning with string.
    • !?string? gets replaced by the most recent command with string in it, can skip trailing "?" if string is last thing on this command line.
  • A quick example of how this might be useful, when programming with a language that needs to be compiled (e.g. C) working involves doing the same basic three things in a loop - edit, compile, test, edit, compile, test, edit, compile, test ...
    % vi prog.c
    % cc prog.c
    % a.out
    % !v
    vi prog.c
    % !c
    cc prog.c
    % !a
  • Any of the history substitutions may be followed by ":" and a word designator, which specifies a word or range of words from the previous command.
  • Word designators are:
    • :0means first word (command)
    • :n means nth argument, so :1means first argument
    • :^ same as :1
    • :$is last argument no matter what number it is
    • :% may be used with !?string?and becomes the argument matched by string
    • :x-y gives arguments x through y
    • :* is same as :^-$ but returns nothing if there were no arguments (would be an error for :^-$)
    • :x* is arguments x through last argument
  • If a word designator begins with ^, $, *, %, or - then you can skip the :
  • Again, the most common use of this is to reuse the last argument of the previous command:
    % more prog.c
    % rm !$
    rm prog.c
  • You can grab any argument though:
    % ls -l mbox.old mbox
    -rw-------   1 csgst00  visitors     402 Mar  3 06:00 mbox
    -rw-------   1 csgst00  visitors    1391 Feb 27 15:51 mbox.old
    % rm !!:2
    rm mbox.old
  • You can end a history substitution with :pto print the new command line but not execute it.
  • This is useful to make a previous command the most recent to make editing it easier.