簡體   English   中英

在 zsh shell 提示時顯示當前分支

[英]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}$ '

閱讀bashzsh之間的差異將是有益的,因為您不能互換使用在線找到的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM