Previous Table of Contents Next


Module 50
fc (ksh) / ! (csh)

DESCRIPTION

The internal fc command provides access to your history file. The Korn shell creates a history file containing the commands you have previously entered during your current login session. You use the fc utility to:

*  Display the contents of the history file.
*  Edit (Visually) the contents of the history file.
*  Reexecute previous commands stored in the history file.

C Shell
The csh supports history file manipulation with different syntax and functionality. There is no command like fc in csh; instead, a character is used to access the history file. The default character is an exclamation mark (!). You can reset this using the histchar variable. Unlike the fc command in the Korn shell the C shell does not support visual editing of the history file.

COMMAND FORMAT

Following is the general format of the fc command.

     fc      [ -e editor ] [ -lnr ] [ first [ last ] ]
     fc -e - [ old=new ]   [ command ]

history is an alias for fc -l.

r is an alias for fc -e -. The r command is called the repeat command. It searches for a string in the history file that matches the given argument and then executes the entire line in which the string was found.


C Shell
!reference_string
^l^r[^]
The ! mark signals the shell that you wish to reexecute a command from the history file. The reference_string is composed of three parts. They are the event separator, word designator, and modifiers. These are described in the following sections. The command lines stored in the history file are often referred to as events.
The second format is referred to as Quick Substitution. It is equivalent to the history substitution command,
!:s^l^r[^]
which substitutes string l with string r on the previous command line and executes the new line if the substitution was successful.

Options

The following list describes the options and their arguments that may be used to control how fc functions.

First format
-e editor Use editor editor to edit the commands in the history file. The editing of the history file is referred to as in-line command editing. If you do not specify the -e option, fc checks the value of the FCEDIT variable for an editor name. If FCEDIT is not defined then /bin/ed is used. Valid editors are vi, emacs, gmacs, and ed.
-l List the contents of the history file.
-n Suppress the command numbers when displaying the commands from the history file.
-r Reverse the order of the commands in the history file when they are listed.
-first Beginning command. The number of lines to go back from the last command to start listing out the history file. For example,
                     fc -l -25
                      history -25
lists the last 25 commands saved in the history file.
-last Ending command. The number of lines to go back from the last command to end listing out the history file. For example,
                     fc -l -n -25 -5
                      history -n -25 -5
lists out the last commands starting 25 commands ago down to 5 commands ago without command numbers.
Second Format
-e - Reexecutes the previous command in the history file. If you do not specify a command or partial command, then the last command you executed is re- executed. This command is the r alias (alias r="fc -e -").

Arguments

The following list describes the arguments that may be passed to the fc command.

First Format
first List commands starting at line first in the history file. For instance,
                     fc -l 15
                      history 15
displays the commands beginning at line 15 to the end of the history file.
last List commands up to line last in the history file. For example,
                     fc -l 20 30
                      history 20 30
displays lines 20 through 30 of the history file.
Second Format
old=new Substitutes old string for new string in the appropriate command before reexecuting it.
command The command to reexecute from the history file. Command may be one of the following:
*  Positive number denoting the event number associated with a command.
*  Negative number subtracted from the current command number. The event number of the subtraction is executed.
*  A string. The latest command starting with the string is executed. For instance,
            fc -e c

or
            r c

searches the history file for the last command that began with a c.
*  If no command is specified then the previous command is executed.

Variables

There are two variables that control the location and size of the history file. The HISTFILE variable informs the shell where to locate the history file. For example,

     HISTFILE=$HOME/.history

places the history file in your HOME directory in the .history file. If you do not specify a path in the HISTFILE variable, the default for the location is $HOME/.sh_history.

The HISTSIZE variable is used to set the number of commands that are stored in the history file. If you do not set the HISTSIZE variable to some number it defaults to 128. Therefore, the history file will contain the last 128 commands you have entered.

The FCEDIT variable is used to define the editor used to edit your history file. The possible editors are vi, emacs, gmacs, and ed.


C Shell
The C shell uses different variables than the Korn shell. To control the size of the file, you use the history variable. For example,
set history=100
would cause the shell to save the last 100 commands in the history file.
The savehist variable is available in the csh to save n commands in your history file when you log out. The next time you log back in to the system, those n commands are still available in your history file. If you do not set savehist to a number of history commands to save, your history file is recreated each time you log in.
You can set the savehist variable by placing the set savehist=50 command in your .login file. This will save the last 50 commands in your history file when you log out of the system. The next time you log in the same last 50 commands will be available in your history file.
Another variable may be set to change the history access characters. By default you type ! or ^ to access a previous command residing in the history file. You can change these characters to alternative characters by setting the histchar variable. For example,
set histchar=.,
changes your history substitute characters to a period (.) and a comma (,). Now you type a .34 to execute the 34th command in your history file. To perform quick substitution on the last command executed use the , notation instead of the caret (^). For example, type ,-xa,-la, to change xa to la in your last command.
If the verbose variable is set, each command is displayed after history substitution is performed. You set this variable using the -v option of the csh.

IN-LINE EDITING

In-line editing allows you to make corrections to a command you are currently typing or to a previous command stored in the history file. To activate in-line editing you must have the EDITOR variable set to vi or emacs. You may use the set -o vi or set -o emacs command to select an in-line edit mode. This informs the Korn shell you want to be in in-line edit mode on each command line.

Once the in-line editing mode is activated you can edit your commands and resend them to UNIX by typing a Return. The Return must be a Ctrl-J character for some shells to send the command line to UNIX.

The window is a single line that can be scrolled left or right. If text is scrolled off on the left of the screen a > appears. If text is scrolled off on the right side a < appears. The width of the screen can be set by using the COLUMNS variable. The default for COLUMNS is 80.

Vi Editing Mode

To use the vi edit command in your history file you must first have the FCEDIT and EDITOR variables set to vi. To edit commands saved in your history file simply press the Escape key. You are now in vi command mode. If you want to make a correction to the previous command simply press k. Now move around on the line using vi commands. Make changes to the line then press Return. You don't have to press Escape before pressing Return.

The vi editing mode is the same as using the vi editor, with a few limitations. The window is only one line and cannot be expanded. Some of the standard vi commands are not supported because of this reduced window size. The following is a list of commands NOT supported by in-line vi editing mode. Refer to Module 151 for available vi commands.

o Open a line below for inserting text.
O Open a line above for inserting text.
scrolling Screen scrolling (Ctrl-U, Ctrl-D, Ctrl-E, Ctrl-Y).
macros Storing editor commands in registers.
mapping Mapping a character to perform multiple edit commands.
: Ex mode. You cannot use any colon commands.


Previous Table of Contents Next