简体   繁体   English

在 zsh shell 提示时显示当前分支

[英]Show current branch on prompt on zsh shell

I am trying to display the git branch on prompt on Big Sur.我试图在 Big Sur 的提示符下显示 git 分支。 So I have created a script file to run for each new session .zshrc所以我创建了一个脚本文件来为每个新的.zshrc .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\] $ "

The above doesn't work it actually displays the string not its outpupt.以上不起作用,它实际上显示的是字符串而不是它的输出。

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

How can I show current branch in prompt on zsh shell?如何在 zsh shell 的提示中显示当前分支?

Bash PS1 is not ZSH PS1, they differ. Bash PS1 不是 ZSH PS1,它们不同。 The \u \h etc. sequences are substituted by bash, they do not work in ZSH. \u \h等序列被 bash 取代,它们在 ZSH 中不起作用。 Research both shells and how they differ.研究这两种贝壳以及它们的不同之处。

I am assuming you want to display something like this [username@computername directory](branch)我假设您想显示类似这样的内容[username@computername directory](branch)

The code below should do it for Zsh which is a little more involved than accomplishing the same task in bash .下面的代码应该为 Zsh 做这件事,这比在bash中完成相同的任务要复杂一些。 There are multiple solutions to this, and you can find more information here if you are interested.对此有多种解决方案,如果您有兴趣,可以在此处找到更多信息。

# 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}$ '

It would be beneficial to read up on the differences between bash and zsh as you cannot use solutions found online interchangeably.阅读bashzsh之间的差异将是有益的,因为您不能互换使用在线找到的解决方案。

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

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