History Facilities

As commands are entered they are stored in the history list and are retained across sessions.
Commands can be recalled with the key, modified as if they had just been entered and executed (see readline)
When using command-line editing, search commands are available in each editing mode that provide access to the history list (see Commands For History).

history

history [n]
history -ps args
history [-anrw] [filename]
history -c
history -d offset

Display the last n lines of the history list with line numbers, * indicates modified line.

Example:

 > history 6
  829  2016-02-17 16:03:39 vi diskUtilityDefaultsAll.html
  830  2016-02-17 16:12:57 chmod 704 diskUtilityDefaultsAll.html
  831  2016-02-17 16:13:15 vi defaults.1.html
  832  2016-02-17 18:25:30 cd docs
  833  2016-02-17 18:25:40 ls -l disk*
-p Perform history substitution on args and display the result without executing it or storing the results in the history list.
-s args args are added to the end of the history list as a single entry.
-a Append the history list to the history file. (as would be done when exiting)
-n Append the history lines, not already read from the history file, to the current history list.
-r Read the current history file and append it to the history list. (i.e. append previous session's activity)
-w Write out the current history to the history file.
-c Clear the history list, with the other options replaces history list completely.
-d offset Delete history entry offset specified as it appears when the history is displayed. Example: history -d832

set -o history enables the history (default for interactive shells).
filename defaults to $HISTFILE usually .bash_history.
At login the history is initialized from $HISTFILE. When an interactive shell exits, the last $HISTSIZE (default 1000) lines are copied from the history to $HISTFILE (or appended based on $HISTAPPEND ) and then truncated to $HISTSIZE .

$HISTIGNORE control the text of the command, prior to parameter and variable expansion but after history expansion.
For example:

HISTIGNORE=ls:head:tail:df:du
causes commands that don't modify anything to be ignored when commands are added to history .

$HISTCONTROL

$HISTTIMEFORMAT (default sometimes: %F %T) format string for strftime to display time stamps included with each entry, marked with the history comment character. When the history file is read, lines beginning with the history comment character followed immediately by a digit are interpreted as timestamps.

shopt can be used to set :

HISTCHARS='!^#'
HISTCMD=1092
HISTFILE=/Volumes/DATA/duser/.zsh_sessions/0D6D1465-07C0-420A-A201-C09CB6BAE820.historynew
HISTIGNORE=ls:lt:w:df:du:taill
HISTORYymd_hms=/log/zsh_hist.0812_0809
HISTSIZE=2000
HISTTIMEFORMAT='%F %T '
SAVEHIST=1000

SHELL_SESSION_DID_HISTORY_CHECK=1
SHELL_SESSION_HISTFILE=/Volumes/DATA/duser/.zsh_sessions/0D6D1465-07C0-420A-A201-C09CB6BAE820.history
SHELL_SESSION_HISTFILE_NEW=/Volumes/DATA/duser/.zsh_sessions/0D6D1465-07C0-420A-A201-C09CB6BAE820.historynew
SHELL_SESSION_HISTFILE_SHARED=/Volumes/DATA/duser/.zsh_history
SHELL_SESSION_HISTORY=1

fc

Fix Command.

Lists or edits and re-execute a portion of the history list.

fc [-e editor] [-lnr] [first] [last]

fc -s [pattern=replacment] [command]

first and last may be strings (to locate the most recent command beginning with that string) or
numbers, an index into the history list, a negative number is used as an offset from the current command number.

If first is not specified it is set to -1 i.e. the previous command for editing and -16 for listing.
If last is not specified it is set to first reulting in a range on one.

-l lists commands.
-n no command numbers .
-s substitute replacment for pattern
-r reverses the order of the listing. (without -l, results in commands being executed in the reverse order then when they were entered !)

editor or $FCEDIT or $EDITOR or vi is invoked on a file containing those commands.
When editing is complete, the edited commands are echoed and executed.

A useful alias is r='fc -s', so that typing r cc runs the last command beginning with cc and typing r re-executes the last command (see Aliases).

History Expansion

Introduced by the appearance of the history expansion character, default: !.
Enabled by default for interactive shells, and can be disabled using set +H .

History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.

History expansion is performed immediately after a complete line is read, before the shell breaks it into words. It takes place in parts.

  1. determine which line from the history list should be used during substitution.
  2. select portions of that line for inclusion into the current one.
The line selected from the history is called the event, and the portions of that line that are acted upon are called words. Modifiers are available to manipulate the selected words. The line is broken into words in the same fashion that Bash does, so that several words surrounded by quotes are considered one word. Only\ and' may be used to escape the history expansion character.

shopt may be used to tailor the behavior of history expansion.
If histverify is enabled, expanded line is loaded into the editing buffer for further modification.
If histreedit is enabled, a failed history expansion will be reloaded into the editing buffer for correction.

The shell allows control of the various characters used by the history expansion mechanism with the histchars variable, as explained above (see Bash Variables). The shell uses the history comment character to mark history timestamps when writing the history file.

  1. Event Designators: How to specify which history line to use.
  2. Word Designators: Specifying which words are of interest.
  3. Modifiers: Modifying the results of substitution.

History Event Designators

A reference to a command line entry in the history list.
! Start a history substitution, except when followed by a space, tab, the end of the line,
  = (as in != but NOT as in !-w i.e. file is writable. use ! -wfile or (
(when the extglob shell option is enabled using the shopt builtin).
!n line n.
!-n n lines back.
!! previous command (!-1).
!string most recent command starting with string.
!?string[?␤] most recent command containing string.
The trailing ? may be omitted if the string is followed immediately by a newline.
^string1^string2^ Quick Substitution. Repeat the last command, replacing string1 with string2.
Equivalent to !!:s/string1/string2/.
!# The entire command line typed so far.

Word Designators

Select desired words from the event.

A : separates the event specification from the word designator and may be omitted if the word designator begins with a ^ , $ , * , -, or %.
If no event specification the previous command is used as the event.
Words are numbered from the beginning of the line, beginning with 0̸ (zero).
Words are inserted into the current line separated by single spaces.
For example: preceding command. When you type this, the preceding command is repeated in toto.: !! last argument of the preceding command. !!:$ shortened to !$. second argument of the most recent command starting with the letters fi. !fi:2 word designators:

first word. Most likely the command.
n The nth word.
^ first argument; that is, word 1.
$ last argument.
% word matched by the most recent?string? search.
x-y A range of words;-y abbreviates 0-y.
* All of the words, except the firstr,. aka 1-$. with no arguments the empty string is returned in that case.
x* Abbreviatesx-$
x- Abbreviatesx-$ likex*, but omits the last word.

Modifiers

After the optional word designator, you can add a sequence of one or more of the following modifiers, each preceded by a:.
h head, Remove a trailing pathname component
t tail, Remove all leading pathname components,
rRemove a trailing suffix of the form.suffix, leaving the basename.
eRemove all but the trailing suffix.
pPrint the new command but do not execute it.
qQuote the substituted words, escaping further substitutions.
xQuote the substituted words as withq, but break into words at spaces, tabs, and newlines.
s/old/new/ Substitute new for the first occurrence of old in the event line. Any delimiter may be used in place of/. If& appears in new, it is replaced by old. A backslash will quote the delimiter or the &.
The ending delimiter is optional if it is the last character on the line.
g globally change over the entire event line, in conjunction with s, as in gs/old/new/, or with &.
& Repeat the previous substitution.
G Apply the following s modifier once to each word in the event.

Histfilenew format

: 1691944208:0;type echo -p 
time EXTENDED_HISTORY

The second number is the duration of the command. 0 command finished quickly or the duration is not saved.
If either $INC_APPEND_HISTORY or $SHARE_HISTORY< is enabled (setopt | grep -E '^(incappend|share)history$'),

To use of the duration metric while still saving the history to file during shell sessions, set $INC_APPEND_HISTORY_TIME , zsh waits for command completion before writing the entry. Obviously this will otherwise behave like $INC_APPEND_HISTORY.
Only one of $INC_APPEND_HISTORY, $INC_APPEND_HISTORY_TIME and $SHARE_HISTORY should be active

bash timstamp is no present by default, $HISTTIMEFORMAT