11

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)
shirish
  • 12,356
  • FYI http://zsh.sourceforge.net/Doc/Release/Options.html#index-EXTENDED_005fHISTORY – cuonglm Jan 11 '18 at 09:44
  • @counglm, I added a bit more info. so now it might make it easier to understand why it doesn't seem to help in anyway. – shirish Jan 11 '18 at 13:33
  • what happens if you put -E on the history command to show the timestamps when EXTENDED_HISTORY is on or off between different invocations of the shell? – thrig Jan 11 '18 at 14:45
  • I don't see a difference in either way. I'll add it to the question as well. – shirish Jan 11 '18 at 18:04
  • @shirish Can you start a clean shell session and retry? – cuonglm Jan 12 '18 at 02:16
  • By clean shell session, do you mean by removing ~/,zsh/.zshrc , Rebooting the system and then trying the command you shared then yes would be able to do it in few hrs. – shirish Jan 12 '18 at 08:50

2 Answers2

6

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).

muru
  • 72,889
pol
  • 61
3

I will note that answer in https://unix.stackexchange.com/a/453336/277040 is slightly misleading. Indeed, the output format of history -E will be the same regardless of the EXTENDED_HISTORY status, but the timestamps will be relatively meaningless if unsetopt EXTENDED_HISTORY was always the case for your Zsh sessions. Each line of the HISTFILE will contain a command without any timestamp info, so history -E outputs the same time for all of these commands (the time that particular shell was opened and the HISTFILE was read into it's memory).

For example, given ~/.zsh_history containing commands executed at different hours in the past:

echo 1
echo 3
echo 5

When I open a shell at 5:03 pm on the day I wrote this answer, history -E outputs:

    1  24.6.2020 17:03  echo 1
    2  24.6.2020 17:03  echo 3
    3  24.6.2020 17:03  echo 5