Zsh

zsh 提示符中的擴展

  • March 9, 2014

我正在努力使用 zsh 提示符,試圖將我的 bash 提示符轉換為 zsh 鉤子。

此程式碼應顯示目前的 kerberos 票證並相應更新,但顏色不起作用,更新也不起作用:

# initialize advanced prompt
autoload -U promptinit && promptinit
# allow functions in prompt
setopt prompt_subst

#initialize colors
autoload -U colors && colors

# add the add-zsh-hook command
autoload add-zsh-hook

# Configure History
setopt hist_ignore_all_dups
setopt inc_append_history
export HISTIGNORE="history"

source $HOME/.zsh/prompt_krb.zsh

update_krb_env() {
if [ "`which klist 2>/dev/null`" != "" ]; then

   # got kerberos support installed
   ticket=`klist 2>/dev/null | grep "Principal:" | sed 's/.*Principal:\ //'`;

   klist -s
   if [ $? -eq 0 ]; then
       __KRB_PROMPT="%{$fg_no_bold['green']%}krb id \"$ticket\"";
   else
       if [ "$ticket" != "" ]; then
           __KRB_PROMPT="%{$fg_no_bold['orange']%}expired krb id \"$ticket\"";
       else
           __KRB_PROMPT="%{$fg_no_bold['yellow']%}no krb id";
       fi
   fi
else
   __KRB_PROMPT="%{$fg_no_bold['yellow']%}no krb id";
fi

_KRB_PROMPT="${__KRB_PROMPT}%{$reset_color%}";
}

krb_hook_preexec() {
   case "$2" in
       kinit*|kdestroy*)
           __EXECUTED_KRB_CMD=1;
       ;;
   esac
}

krb_hook_postexec() {
   if [ -n "$__EXECUTED_KRB_CMD" ]; then
       update_krb_env;
       unset __EXECUTED_KRB_CMD;
   fi
}

add-zsh-hook preexec krb_hook_preexec
add-zsh-hook precmd krb_hook_postexec
update_krb_env;

get_krb() {
   echo -n $__KRB_PROMPT;
}

PROMPT="$(get_krb) >"

我一直在研究多個教程,試圖以程式碼為基礎,但我似乎遺漏了一些東西。

命令替換和變數在雙引號內展開,所以在

PROMPT="$(get_krb) >"

$(get_krb)分配時擴展 ,而您希望在每次提示時通過 擴展prompt_subst。所以:

PROMPT='$(get_krb) >'

引用自:https://unix.stackexchange.com/questions/118861