5

I would like to both share history across terminals and keep track of elapsed time for history commands. The intuitive way seems to be to use both EXTENDED_HISTORY and SHARE_HISTORY, but this sets all elapsed timestamps to 0 after restarting the shell. Is this the correct way to do it? Is it even possible?

Scenario 1: EXTENDED_HISTORY and SHARE_HISTORY

$ history -Dn | sed 's|\\n|\n      |g'
0:00  cat << EOF > ~/.zshrc
          HISTFILE=~/.zhistory-test
          HISTSIZE=100
          SAVEHIST=100
          setopt EXTENDED_HISTORY
          setopt SHARE_HISTORY # the only change compared to below
      EOF
0:00  exec zsh

0:00sleep 1

0:00  exec zsh

Scenario 2: only EXTENDED_HISTORY

$ history -Dn | sed 's|\\n|\n      |g'
0:00  cat << EOF > ~/.zshrc
          HISTFILE=~/.zhistory-test
          HISTSIZE=100
          SAVEHIST=100
          setopt EXTENDED_HISTORY
      EOF
0:00  exec zsh

0:01sleep 1

0:00  exec zsh

Scenario 3: INC_APPEND_HISTORY and SHARE_HISTORY

Same as scenario 1, but replacing SHARE_HISTORY with INC_APPEND_HISTORY seems to have the same effect.

System info

$ zsh --version

zsh 5.4.2 (x86_64-unknown-linux-musl)

$ ldd /usr/bin/zsh

        /lib/ld-musl-x86_64.so.1 (0x7f0f8b1d8000)
        libcap.so.2 => /lib/libcap.so.2 (0x7f0f8acfa000)
        libncursesw.so.6 => /lib/libncursesw.so.6 (0x7f0f8aa89000)
        libc.so => /lib/ld-musl-x86_64.so.1 (0x7f0f8b1d8000)

$ uname -a

Linux hostname 4.12.13_1 #1 SMP PREEMPT Thu Sep 14 13:15:00 UTC 2017 x86_64 GNU/Linux

$ lsb_release -d

Description:    Void Linux
  • I was looking for this too. I like how INC_APPEND_HISTORY_TIME works, but it doesn't appear to work with SHARE_HISTORY. I guess we are looking for a SHARE_HISTORY_TIME. – whatsthatitspat Feb 2 '20 at 20:57
0

I tested several ways of trying to share extended history between multiple terminal sessions and this is the one that seems to work flawlessly:

setopt extendedhistory incappendhistorytime
autoload -Uz add-zsh-hook

import-history() {
  # Pop the last history list, so we don't grow the history stack endlessly
  fc -P

  # Initialize a new history list from $HISTFILE (and push it on the history stack).
  fc -p $HISTFILE
}

# Import the history at the start of each new prompt.
add-zsh-hook precmd import-history

Now all you need to do to sync your history in any terminal session is to press Enter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.