【问题标题】:Show current branch on prompt on zsh shell在 zsh shell 的提示符下显示当前分支
【发布时间】:2021-05-18 13:41:09
【问题描述】:

我正在尝试在 Big Sur 的提示符下显示 git 分支。 所以我创建了一个脚本文件来为每个新会话运行.zshrc

# Git branch in prompt.
parse_git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ [\1]/'
}
export PS1="\u@\h \W\[\033[01;33m\]\$(parse_git_branch)\[\033[00m\] $ "

以上不起作用,它实际上显示的是字符串而不是它的输出。

\u@\h \W\[\033[01;33m\]$(parse_git_branch)\[\033[00m\] $

如何在 zsh shell 的提示符中显示当前分支?

【问题讨论】:

标签: macos shell


【解决方案1】:

我假设您想显示类似 [username@computername directory](branch) 的内容

下面的代码应该为 Zsh 做这件事,这比在 bash 中完成相同的任务要复杂一些。对此有多种解决方案,有兴趣的可以查找更多信息here

# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }

# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats '%b'

# Set up the prompt (with git branch name)
setopt PROMPT_SUBST

PROMPT='[%n@%m %1~]%F{green}(${vcs_info_msg_0_})%F{white}$ '

阅读bashzsh 之间的区别会很有帮助,因为您不能互换使用在线找到的解决方案。

【讨论】:

    【解决方案2】:

    Bash PS1 不是 ZSH PS1,它们不同。 \u \h 等序列被 bash 替换,它们在 ZSH 中不起作用。研究这两种贝壳以及它们之间的区别。

    【讨论】:

      猜你喜欢
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      相关资源
      最近更新 更多