【问题标题】:Zsh prompt function doesn't workzsh提示功能不起作用
【发布时间】:2015-01-27 20:43:04
【问题描述】:

为什么它不起作用?提示区是空的,我没有错误。

setopt prompt_subst
git_prompt() {
  temp=`git symbolic-ref HEAD 2>/dev/null | cut -d / -f 3`
  if [ "$temp" != "" ]; then
        RPROMPT='%{$fg_no_bold[green]%}git:($temp%)%{$reset_color%} %{$fg_no_bold[yellow]%}[%1~]%{$reset_color%}'
  else
        RPROMPT='%{$fg_no_bold[yellow]%}[%~]%{$reset_color%}'
  fi
}
RPROMPT='$(git_prompt)'

RPROMPT 的值拼写正确且不包含错误。

【问题讨论】:

  • 你想做什么?

标签: linux unix terminal console zsh


【解决方案1】:

函数git_prompt 不产生任何输出,而是直接设置RPROMPT。然而,您随后将RPROMPT 设置为git_prompt 的输出,实际上将其设置为空字符串。

返回字符串而不是在git_prompt 中设置RPROMPT

setopt prompt_subst git_prompt() { temp=`git symbolic-ref HEAD 2>/dev/null |剪切-d / -f 3` 如果 [ "$temp" != "" ];然后 return '%{$fg_no_bold[green]%}git:($temp%)%{$reset_color%} %{$fg_no_bold[yellow]%}[%1~]%{$reset_color%}' 别的 return '%{$fg_no_bold[yellow]%}[%~]%{$reset_color%}' 菲 } RPROMPT='$(git_prompt)'

或者只是将git_prompt设置为在打印提示之前自动运行:

git_prompt() { temp=`git symbolic-ref HEAD 2>/dev/null |剪切-d / -f 3` 如果 [ "$temp" != "" ];然后 RPROMPT='%{$fg_no_bold[green]%}git:($temp%)%{$reset_color%} %{$fg_no_bold[yellow]%}[%1~]%{$reset_color%}' 别的 RPROMPT='%{$fg_no_bold[黄色]%}[%~]%{$reset_color%}' 菲 } 自动加载-Uz add-zsh-hook add-zsh-hook precmd git_prompt

您可能还想查看the vcs_info function,它允许您生成带有版本控制信息的提示,而无需自己进行数据检索。

【讨论】:

  • 谢谢你,我使用了 git_prompt 就像魅力一样!如果有任何需要,我会尝试获取该分支的当前更改编号。像这样改变温度 'temp='git diff --shortstat | sed -e 's/insertions//' -e's/deletions//''
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多