zsh: SHARE_HISTORY 或 INC_APPEND_HISTORY 中断 EXTENDED_HISTORY

gnu*_*chi 7 command-history zsh configuration timestamps

我想在终端之间共享历史记录并跟踪历史命令的经过时间。直观的方法似乎是同时使用EXTENDED_HISTORYand SHARE_HISTORY,但这将在重新启动 shell 后将所有经过的时间戳设置为 0。这是正确的方法吗?甚至有可能吗?

场景 1:EXTENDED_HISTORYSHARE_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
Run Code Online (Sandbox Code Playgroud)

0:00sleep 1

0:00  exec zsh
Run Code Online (Sandbox Code Playgroud)

场景2:仅 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
Run Code Online (Sandbox Code Playgroud)

0:01sleep 1

0:00  exec zsh
Run Code Online (Sandbox Code Playgroud)

场景 3:INC_APPEND_HISTORYSHARE_HISTORY

与场景 1 相同,但替换SHARE_HISTORYINC_APPEND_HISTORY似乎具有相同的效果。

系统信息

$ zsh --version

zsh 5.4.2 (x86_64-unknown-linux-musl)
Run Code Online (Sandbox Code Playgroud)

$ 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)
Run Code Online (Sandbox Code Playgroud)

$ uname -a

Linux hostname 4.12.13_1 #1 SMP PREEMPT Thu Sep 14 13:15:00 UTC 2017 x86_64 GNU/Linux
Run Code Online (Sandbox Code Playgroud)

$ lsb_release -d

Description:    Void Linux
Run Code Online (Sandbox Code Playgroud)

Mar*_*ert 3

我\xe2\x80\x99已经测试了几种尝试在多个终端会话之间共享扩展历史记录的方法,这是唯一一种似乎完美工作的方法:

\n
autoload -Uz add-zsh-hook\n\nsetopt extendedhistory incappendhistorytime\n\nload-shared-history() {\n  # Pop the current history off the history stack, so we don\'t grow\n  # the history stack endlessly\n  fc -P\n\n  # Load a new history from $HISTFILE and push it onto the history \n  # stack.\n  fc -p $HISTFILE\n}\n\n# Import the latest history at the start of each new command line.\nadd-zsh-hook precmd load-shared-history\n
Run Code Online (Sandbox Code Playgroud)\n

现在,在任何终端会话中同步历史记录所需要做的就是按 Enter 键。

\n