如何在 zsh 中只显示当前文件夹和 git branch 和 ~ 用于 home

Ser*_*rov 4 git macos terminal zsh prompt

Apple在其最新的操作系统中将shell 从 更改bashzsh,所以我现在正在尝试修复我的终端提示:(。

我希望我的提示只包含:

  • 我所在的当前目录(没有完整路径)
  • 没有用户名和计算机名
  • 当前的 git 分支(绿色)
  • ~ 如果我在主目录中
  • a$和最后一个空格

我曾经在我的这个剧本.bash_profile时,我使用bash

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

export PS1="\[\033[33;1m\]\W\[\033[32m\]\$(parse_git_branch)\[\033[m\]\$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -GFh'
Run Code Online (Sandbox Code Playgroud)

我重命名.bash_profile.zprofile,但除ls部分外,所有这些都不再起作用。

我如何使这项工作再次发挥作用?

Ser*_*rov 9

因此,经过更多的谷歌搜索并查看zsh可以通过运行显示的手册的特定部分后,man zshmisc我设法解决了这个问题。这是代码.zprofile

# 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
setopt PROMPT_SUBST
PROMPT='%1~ %F{green}${vcs_info_msg_0_}%f $ '
Run Code Online (Sandbox Code Playgroud)

%1~意味着只显示当前工作目录的最后一个尾随部分,主目录将替换为~.