将 Zsh 历史记录保存到 ~/.persistent_history

ast*_*lrx 9 bash shell zsh

最近想在Mac上试试Z shell。但是我想继续将命令历史记录保存到 ~/.persistent_history,这就是我在 Bash ( ref ) 中所做的。

但是,参考链接中的脚本在 Zsh 下不起作用:

log_bash_persistent_history()
{
   [[
     $(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
   ]]
   local date_part="${BASH_REMATCH[1]}"
   local command_part="${BASH_REMATCH[2]}"
   if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
   then
     echo $date_part "|" "$command_part" >> ~/.persistent_history
     export PERSISTENT_HISTORY_LAST="$command_part"
   fi
}
run_on_prompt_command()
{
   log_bash_persistent_history
}
PROMPT_COMMAND="run_on_prompt_command"
Run Code Online (Sandbox Code Playgroud)

有没有人可以帮助我让它工作?非常感谢!

ast*_*lrx 6

经过如此多的谷歌搜索,我终于找到了这样做的方法。首先,在 ~/.zshrc 中,添加以下用于历史操作的选项:

setopt append_history # append rather then overwrite
setopt extended_history # save timestamp
setopt inc_append_history # add history immediately after typing a command
Run Code Online (Sandbox Code Playgroud)

简而言之,这三个选项会立即将每个 input_time+command 记录到 ~/.zsh_history 中。然后,将此函数放入 ~/.zshrc 中:

precmd() { # This is a function that will be executed before every prompt
    local date_part="$(tail -1 ~/.zsh_history | cut -c 3-12)"
    local fmt_date="$(date -d @${date_part} +'%Y-%m-%d %H:%M:%S')"
    # For older version of command "date", comment the last line and uncomment the next line
    #local fmt_date="$(date -j -f '%s' ${date_part} +'%Y-%m-%d %H:%M:%S')"
    local command_part="$(tail -1 ~/.zsh_history | cut -c 16-)"
    if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
    then
        echo "${fmt_date} | ${command_part}"  >> ~/.persistent_history
        export PERSISTENT_HISTORY_LAST="$command_part"
    fi
}
Run Code Online (Sandbox Code Playgroud)

由于我同时使用 bash 和 zsh,所以我想要一个可以保存所有历史命令的文件。在这种情况下,我可以使用“grep”轻松搜索所有这些。


mft*_*rhu 2

还不能发表评论(这超出了简单的更正),所以我将其添加为答案。

例如,当最后一个命令花了相当多的时间来执行时,对已接受答案的更正并不太有效 - 您将;在命令中得到杂散数字,如下所示:

2017-07-22 19:02:42 | 3;micro ~/.zshrc && . ~/.zshrc
Run Code Online (Sandbox Code Playgroud)

这可以通过用稍长的替换sed -re '1s/.{15}//'in来解决,这也避免了我们使用管道:command_partgawk

local command_part="$(gawk "
  NR == $line_num_last {
    pivot = match(\$0, \";\");
    print substr(\$0, pivot+1);
  }
  NR > $line_num_last {
    print;
  }" ~/.zsh_history)"
Run Code Online (Sandbox Code Playgroud)

在处理其中一行以 开头的多行命令时,它也会遇到问题:。这可以(大部分)通过替换grep -ane '^:' ~/.zsh_historyline_num_last来解决grep -anE '^: [0-9]{10}:[0-9]*?;' ~/.zsh_history- 我说主要是因为命令可能包含与该表达式匹配的字符串。说,

% naughty "multiline
> command
> : 0123456789:123;but a command I'm not
> "
Run Code Online (Sandbox Code Playgroud)

这将导致 中的记录被破坏~/.persistent_history

为了解决这个问题,我们需要依次检查之前的重记录是否以以下结尾\(可能还有其他条件,但我还不熟悉这种历史格式),如果是,则尝试之前的匹配。

_get_line_num_last () {
  local attempts=0
  local line=0
  while true; do
    # Greps the last two lines that can be considered history records
    local lines="$(grep -anE '^: [0-9]{10}:[0-9]*?;' ~/.zsh_history | \
                 tail -n $((2 + attempts)) | head -2)"
    local previous_line="$(echo "$lines" | head -1)"
    # Gets the line number of the line being tested
    local line_attempt=$(echo "$lines" | tail -1 | cut -d':' -f1 | tr -d '\n')
    # If the previous (possible) history records ends with `\`, then the
    # _current_ one is part of a multiline command; try again.
    # Probably. Unless it was in turn in the middle of a multi-line
    # command. And that's why the last line should be saved.
    if [[ $line_attempt -ne $HISTORY_LAST_LINE ]] && \
       [[ $previous_line == *"\\" ]] && [[ $attempts -eq 0 ]];
    then
      ((attempts+=1))
    else
      line=$line_attempt
      break
    fi
  done
  echo "$line"
}
precmd() {
  local line_num_last="$(_get_line_num_last)"
  local date_part="$(gawk "NR == $line_num_last {print;}" ~/.zsh_history | cut -c 3-12)"
  local fmt_date="$(date -d @${date_part} +'%Y-%m-%d %H:%M:%S')"
  # I use awk itself to split the _first_ line only at the first `;`
  local command_part="$(gawk "
    NR == $line_num_last {
      pivot = match(\$0, \";\");
      print substr(\$0, pivot+1);
    }
    NR > $line_num_last {
      print;
    }" ~/.zsh_history)"
  if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
  then
    echo "${fmt_date} | ${command_part}" >> ~/.persistent_history
    export PERSISTENT_HISTORY_LAST="$command_part"
    export HISTORY_LAST_LINE=$((1 + $(wc -l < ~/.zsh_history)))
  fi
}
Run Code Online (Sandbox Code Playgroud)