2015-05-15 313 views
3

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

然而,在裁判链接脚本在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" 

是否有任何人谁可以帮助我得到它的工作?非常感谢!

+1

[这](http://superuser.com/questions/735660/whats-the-zsh-equivalent-of-bashs-prompt-command)应与替换'PROMPT_COMMAND'帮助。替换[[''用法应该可以用'grep -o'或'cut'或类似的方法来实现,但是取决于zsh中'history'的确切输出。 –

+0

@EtanReisner非常感谢!对于'PROMPT_COMMAND',链接应该是有帮助的。对于''[''部分,我刚刚用'history'命令发现,bash会在最后一行给出最新的一个(在这种情况下,它是'history')。但是在Zsh下,'history'命令不会返回最新的命令,它会在最后一行返回'history'之前使用的命令。有任何想法吗? :-) – astroboylrx

+0

那么,我没有理由重新发明轮子。只需设置'HISTFILE'并将'HISTSIZE'和'SAVEHIST'设置为一个非常大的尺寸(我的尺寸是100,000),并且我没有理由让它们变大,因为我在iTerm2中记录了所有终端会话 - 这就是所有命令+输出,在提示中时间缩短到秒)。默认的历史记录格式具有关联的POSIX时间戳,由于您没有tzinfo,所以它比您的更准确。 – 4ae1e1

回答

2

经过这么多谷歌搜索,我终于找到了做到这一点的方式。 首先,在〜/ .zshrc,添加下列选项历史操作:

setopt append_history # append rather then overwrite 
setopt extended_history # save timestamp 
setopt inc_append_history # add history immediately after typing a command 

总之,这三个选项会立即记录下每一个input_time +命令到〜/ .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 
} 

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

+0

多线命令失败。以下工作:'local date_part =“$(gawk'substr($ 0,0,1)==”:“{print;}'〜/ .zsh_history | tail -1 | cut -c 3-12)”' –

1

原来的答案大多是好的,但要处理,也包含字符多行命令“:”比如这个作品:

local line_num_last=$(grep -ane '^:' ~/.zsh_history | tail -1 | cut -d':' -f1 | tr -d '\n') 
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')" 
local command_part="$(gawk "NR >= $line_num_last {print;}" ~/.zsh_history | sed -re '1s/.{15}//')" 
+0

凉!我没有想过多行命令。这非常有帮助。非常感谢! :-) – astroboylrx

+0

我添加另一行来使多行命令存储为单行命令:'local one_line_command = $ {(Q)command_part // \\ $'\ n'/}'。现在看起来非常棒。 :-) – astroboylrx

0

不能发表评论,但(这超出了一个简单的更正),所以我会添加这个答案。

This correctionthe accepted answer完全不是那么回事的时候,例如,最后一个命令了相当多的时间来执行 - 你会得到流浪数字和你的命令;,像这样:

2017-07-22 19:02:42 | 3;micro ~/.zshrc && . ~/.zshrc 

这可以通过在command_part与稍长gawk,这也避免了我们一个管道更换sed -re '1s/.{15}//'固定:

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

它也有问题时deali使用多行命令,其中一行以:开头。这可以(大部分)通过用grep -anE '^: [0-9]{10}:[0-9]*?;' ~/.zsh_history代替line_num_last中的grep -ane '^:' ~/.zsh_history来解决 - 我说主要是因为一个命令可能包含一个匹配该表达式的字符串。再说了,

% naughty "multiline 
> command 
> ::123;but a command I'm not 
> " 

这将导致~/.persistent_history一个惨败纪录。

为了解决这个问题,我们需要反过来,检查以前的redord是否有结束\(可能还有其他的条件,但我不是这样的历史格式熟悉却又),如果是这样尝试之前的比赛。

_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 
}