【问题标题】:macOS terminal configuration problems, setting colours and git branching informationmacOS终端配置问题,设置颜色和git分支信息
【发布时间】:2020-12-23 01:42:44
【问题描述】:

我在 macOS Big Sur 11.1 中遇到终端显示配置问题 我希望它看起来像 CMDer 编辑器中的 Windows 终端。可以在下图中使用

(img) I want to have something like this

我已经习惯了终端的这种外观,所以我开始寻找解决方案。 我发现,我需要配置 .zshrc 文件来更改颜色并在需要时添加 git 信息。

我找到了需要在.zshrc文件中设置的代码:

parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;44m'
COLOR_DIR=$'\e[38;5;106m'
COLOR_GIT=$'\e[38;5;208m'
NEWLINE=$'\n'

setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DEF}in ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}Ⲗ '

它在颜色方面很有效。我无法让 parse_git_branch 函数工作。我不知道这里发生了什么。在其他帖子中,有信息表明该方法有效。

(img) currently, I have something like this. With no git branching information

【问题讨论】:

  • 请阅读、查看并记住此页面上的项目:stackoverflow.com/tags/bash/info。搜索“在询问有问题的代码之前”和“如何将糟糕的脚本变成好问题”。您可能可以将您的问题归结为 2-3 行复制/粘贴代码。使用演示问题的示例代码,您将更快地获得答案。请务必包含任何需要的特殊环境的说明。祝你好运。
  • zsh 有自己的终端独立方式在提示中指定颜色;不要乱用原始的 ANSI 转义码。

标签: bash macos terminal configuration zsh


【解决方案1】:

给你:

parse_git_branch() {
  local branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)

  # If the branch is non-zero
  if [[ -n $branch ]]; then

    # -n: Don't print a newline at the end.
    # -P: Substitute prompt expansions.
    # %F{y}: Foreground color yellow
    print -nP - "%F{y}($branch"

    local tracking=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)
    [[ -n $tracking ]] &&
      print -nP - " -> $tracking"
    print - ')'
  fi
}

setopt promptsubst

# %F{g}: Foreground color green
# %~: pwd with named dir substitution
# %f: Default foreground color
export PS1='%F{g}%~ $(parse_git_branch)
%fⲖ '

参考资料:

【讨论】:

    猜你喜欢
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多