[英]How can I modify my zsh terminal prompt to show the current working directory and current git branch (if applicable)?
[英]Show current branch on prompt on zsh shell
我试图在 Big Sur 的提示符下显示 git 分支。 所以我创建了一个脚本文件来为每个新的.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\] $ "
以上不起作用,它实际上显示的是字符串而不是它的输出。
\u@\h \W\[\033[01;33m\]$(parse_git_branch)\[\033[00m\] $
如何在 zsh shell 的提示中显示当前分支?
Bash PS1 不是 ZSH PS1,它们不同。 \u \h
等序列被 bash 取代,它们在 ZSH 中不起作用。 研究这两种贝壳以及它们的不同之处。
我假设您想显示类似这样的内容[username@computername directory](branch)
下面的代码应该为 Zsh 做这件事,这比在bash
中完成相同的任务要复杂一些。 对此有多种解决方案,如果您有兴趣,可以在此处找到更多信息。
# 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}$ '
阅读bash
和zsh
之间的差异将是有益的,因为您不能互换使用在线找到的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.