What does extended_history in zsh do

command historyzsh

I saw the option extended_history and saw it being used as in Per-directory history in zsh but haven't really understood what does extended_history actually do ?

What info. would become available in zsh_history which otherwise wouldn't if this option is not set.

From http://zsh.sourceforge.net/Doc/Release/Options.html#Options it says about –

EXTENDED_HISTORY <C>

    Save each command’s beginning timestamp (in seconds since the epoch) and the duration (in seconds) to the history file. The format of this prefixed data is:

    ‘: <beginning time>:<elapsed seconds>;<command>’. 

But I don't see any benefit to it at least while running the history command –

 shirish@debian ~ % history | grep tail -5
      601* exit
      602* history
      603* exit
      604  cd
      605  cat ~/.zsh/.zshrc
      606   tail -5 history

This is how my ~/.zsh/.zshrc is set up –

shirish@debian ~ % cat ~/.zsh/.zshrc
# Lines configured by zsh-newuser-install
HISTFILE=~/.zsh//.histfile
HISTSIZE=1000
SAVEHIST=100000
setopt inc_append_history autocd nomatch notify share_history extended_history
bindkey -e
# End of lines configured by zsh-newuser-install

# display how long all tasks over 10 seconds take
export REPORTTIME=10

# The following lines were added by compinstall
zstyle :compinstall filename '/home/shirish/.zsh//.zshrc'

autoload -Uz compinit promptinit
compinit
promptinit
# End of lines added by compinstall

prompt adam1

I didn't see any difference either after setting the option or taking it out, no meaningful difference.

shirish@debian ~ % cat ~/.zsh/.zshrc | grep setopt
setopt inc_append_history autocd nomatch notify share_history extended_history
shirish@debian ~ % source ~/.zsh/.zshrc           
shirish@debian ~ % history -E                     
  337  11.1.2018 23:33  history | grep apg
  338  11.1.2018 23:33  man apg
  339  11.1.2018 23:33  cd
  340  11.1.2018 23:33  apg -a 1 -M n -n 3 -m 20
  341  11.1.2018 23:33  apg -a 1 -M SNCL -n 3 -m 20
  342  11.1.2018 23:33  history 
  343  11.1.2018 23:33  history -E
  344  11.1.2018 23:33  leafpad ~/.zsh/.zshrc
  345  11.1.2018 23:33  cat ~/.zsh/.zshrc
  346  11.1.2018 23:33  source ~/.zsh/.zshrc
  347  11.1.2018 23:33  history -E
  348  11.1.2018 23:33  exit
  349  11.1.2018 23:33  history -E
  350  11.1.2018 23:34  leafpad ~/.zsh/.zshrc
  351  11.1.2018 23:35  cat ~/.zsh/.zshrc | grep setopt
  352  11.1.2018 23:35  source ~/.zsh/.zshrc
shirish@debian ~ % leafpad ~/.zsh/.zshrc          
shirish@debian ~ % source ~/.zsh/.zshrc 
shirish@debian ~ % history -E           
  340  11.1.2018 23:33  apg -a 1 -M n -n 3 -m 20
  341  11.1.2018 23:33  apg -a 1 -M SNCL -n 3 -m 20
  342  11.1.2018 23:33  history 
  343  11.1.2018 23:33  history -E
  344  11.1.2018 23:33  leafpad ~/.zsh/.zshrc
  345  11.1.2018 23:33  cat ~/.zsh/.zshrc
  346  11.1.2018 23:33  source ~/.zsh/.zshrc
  347  11.1.2018 23:33  history -E
  348  11.1.2018 23:33  exit
  349  11.1.2018 23:33  history -E
  350  11.1.2018 23:34  leafpad ~/.zsh/.zshrc
  351  11.1.2018 23:35  cat ~/.zsh/.zshrc | grep setopt
  352  11.1.2018 23:35  source ~/.zsh/.zshrc
  353  11.1.2018 23:35  history -E
  354  11.1.2018 23:36  leafpad ~/.zsh/.zshrc
  355  11.1.2018 23:36  source ~/.zsh/.zshrc
shirish@debian ~ % cat ~/.zsh/.zshrc | grep setopt
setopt inc_append_history autocd nomatch notify share_history

As can be seen there doesn't seem to be any difference.

% zsh --version
zsh 5.4.2 (x86_64-debian-linux-gnu)

Best Answer

The output of history -E will always be the same regardless of the EXTENDED_HISTORY option. The duration of the command is stored directly in the history file (just examine the file to see the values).

However, another gotcha is that there are a few options that will override this behavior. You can test to see if it is working by running a command like sleep 3, which should result in an entry that looks like this:

pol@host ~ $ sleep 3
pol@host ~ $ tail -1 ${HISTFILE}
: 1530663493:3;sleep 3
pol@host ~ $ setopt | grep hist
extendedhistory
histignoredups
incappendhistorytime

You can see that the "duration" value is 3. If it is not 3, then it is likely that you have another option set that is preventing EXTENDED_HISTORY from working. These include SHARED_HISTORY and INC_APPEND_HISTORY. If you need the former, then you are out of luck. For the latter one, there is an alternative INC_APPEND_HISTORY_TIME that you can use instead if you want to also have EXTENDED_HISTORY values (as I have above).

Related Question