blog icon indicating copy to clipboard operation
blog copied to clipboard

macOS 11 手动配置 Terminal (zsh)

Open qingquan-li opened this issue 3 years ago • 0 comments

  • macOS 11 Terminal 默认使用 zsh 。
  • 此处不使用 oh-my-zsh 配置,而是选择手动配置:highlighting 高亮和 git分支名显示。

~/.zshrc 文件下新增以下配置:

# $ brew install zsh-syntax-highlighting
# To activate the syntax highlighting, add the following at the end of your .zshrc:
source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

# /private/etc/zshrc 中默认配置的命令提示符(Default prompt)如下:
# PS1="%n@%m %1~ %# "
# %n 是帐户的用户名。
# %m 是Mac的名称(在 System Preference > Sharing > Computer Name 可修改)。
# %1~ 符号表示当前工作目录(0显示完整的目录),~表示 $ HOME 目录位置。
# %# 表示如果外壳程序以root(管理员)特权运行,则提示符将显示#,否则,则提供%
# PS1="%n@%m %0~ %# "

# Add Git Branch Name to Terminal, set branch highlighting
# https://gist.github.com/reinvanoyen/05bcfe95ca9cb5041a4eafd29309ff29
# https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html
function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/(\1)/p'
}
setopt PROMPT_SUBST
export PROMPT='%F{245}%n@%m%f %~ %F{yellow}$(parse_git_branch)%f %# '

Apply change by running:

% source ~/.zshrc

效果如下:

Screen Shot 2021-11-15 at 1 51 53 PM

qingquan-li avatar Jul 31 '21 19:07 qingquan-li