简体   繁体   English

如何在ZSH提示中输出当前历史记录事件编号(%!或%h)-1?

[英]How do I output the current history event number (%! or %h) -1 in my ZSH prompt?

In my ZSH prompt I want to be able to output the current history event number ( %! or %h ) -1 在我的ZSH提示符下,我希望能够输出当前历史记录事件编号( %!%h )-1

If current history event number is !256, I want to subtract it by one and output the result in my prompt (ie !255). 如果当前历史事件编号为!256,我想将其减去1,然后在提示符下输出结果(即!255)。

Here's the way it looks now and how I'd like it to be: 这是现在的样子以及我希望它的样子:

我当前的ZSH提示符下的屏幕截图

Below is my current ZSH theme (and the code pertaining to this question lies in the previous_history_event_number () function, which is triggered from the return_code_enabled= declaration: 以下是我当前的ZSH主题(与该问题有关的代码位于previous_history_event_number()函数中,该函数从return_code_enabled =声明触发):

# ------------------------------------------------------------------------------
#          FILE:  hced.zsh-theme
#   DESCRIPTION:  oh-my-zsh theme file.
#                 (Credits to authors of blinks.zsh-theme and dieter.zsh-theme
#                  from which themes I've taken useful bits.)
#        AUTHOR:  hced
#       VERSION:  0.0.1
#    SCREENSHOT:
# ------------------------------------------------------------------------------

function _prompt_char() {
  if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
    echo "%{%F{blue}%}±%{%f%k%b%}"
  else
    echo ' '
  fi
}

ZSH_THEME_GIT_PROMPT_PREFIX=" [%{%B%F{blue}%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{%f%k%b%K{black}%B%F{green}%}]"
ZSH_THEME_GIT_PROMPT_DIRTY=" %{%F{red}%}*%{%f%k%b%}"
ZSH_THEME_GIT_PROMPT_CLEAN=""

PROMPT='%{%f%k%b%}
%{%K{black}%B%F{green}%}%n%{%B%F{blue}%}@%{%B%F{cyan}%}%m%{%B%F{green}%} %{%b%F{yellow}%K{black}%}%~%{%B%F{green}%}$(git_prompt_info)%E%{%f%k%b%}
%{%K{black}%}$(_prompt_char)%{%K{black}%} %#%{%f%k%b%} '

#RPROMPT='!%{%B%F{cyan}%}%!%{%f%k%b%}'

function previous_history_event_number () {
    prev_hist_num=("%!"-1)
    declare -i prev_hist_num
    echo "$prev_hist_num gave exit code: "
}

# elaborate exitcode on the right when >0
return_code_enabled="%(?..%{$fg[red]%}$(previous_history_event_number)%?%{$reset_color%})"
return_code_disabled=
return_code=$return_code_enabled

RPS1='${return_code}  !%{%B%F{cyan}%}%!%{%f%k%b%}'

function accept-line-or-clear-warning () {
    if [[ -z $BUFFER ]]; then
        time=$time_disabled
        return_code=$return_code_disabled
    else
        time=$time_enabled
        return_code=$return_code_enabled
    fi
    zle accept-line
}
zle -N accept-line-or-clear-warning
bindkey '^M' accept-line-or-clear-warning

Disclaimer: I'm not a programmer so the code within the previous_history_event_number () function is pretty (vastly) clueless right now. 免责声明:我不是程序员,所以previous_history_event_number ()函数中的代码现在非常(非常)毫无头绪。

If current history event number is shown when you add %! 如果在添加%!时显示当前历史记录事件号%! atom in a prompt then you can get it into a variable by using (%) modifier: 提示中的原子,然后可以使用(%)修饰符将其放入变量中:

V='%!'
integer HISTORY_EVENT_NUMBER=${(%)V}-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM