效果图示例:
提示符内容规则
命令提示符部分: MBA-Zhou:etc zhouxirun (stable)$
是由变量 PS1
来控制的, 查看变量如下:
echo $PS1 # 注意大写
例如: \h:\W:\u $
对应: 主机名:上级目录:登陆用户 $
提示符高亮
可以改下颜色来更好的区分提示符和命令, 如下:
vim ~/.bash_profile
export PS1 = "\[\033[01;34m\]\h\[\033[00m\]:\[\033[01;33m\]\W\[\033[00m\] \[\033[01;32m\]\u\[\033[00m\] \[\033[01;31m\]$"
source ~/.bash_profile
[\033[01;34m] 蓝色; [\033[00m] 黑色; [\033[01;33m] 黄色; [\033[01;32m] 绿色;
Git分支提示
修改PS1变量规则
# 先通过brew安装git(brew 安装的git自带命令提示脚本)
brew install git
vim ~/.bash_profile
export PS1 = "\[\033[01;34m\]\h\[\033[00m\]:\[\033[01;33m\]\W\[\033[00m\] \[\033[01;32m\]\u\[\033[00m\] \[\033[01;31m\]$(git rev-parse --is-inside-work-tree &>/dev/null && echo "($(git symbolic-ref --short HEAD))")\[\033[00m\]\$"
source ~/.bash_profile
如果当前目录是git仓库,则显示git分支名
KaTeX parse error: Expected 'EOF', got '&' at position 38: …side-work-tree &̲>/dev/null && e…(git symbolic-ref --short HEAD))")[\033[00m]$
获取git当前分支名
git symbolic-ref --short HEAD
Git 命令提示
# 进入homebrew目录查看git提示脚本
cd /opt/homebrew/etc/bash_completion.d
# 会发现两个文件 git-completion.bash 和 git-prompt.sh
# 编辑~/.bash_profile
vim ~/.bash_profile
# 设置git的语法提示和自动完成
if [ -f /opt/homebrew/Cellar/git/2.33.1_1/etc/bash_completion.d/git-prompt.sh ]; then
source /opt/homebrew/Cellar/git/2.33.1_1/etc/bash_completion.d/git-prompt.sh
fi
if [ -f /opt/homebrew/Cellar/git/2.33.1_1/etc/bash_completion.d/git-completion.bash ]; then
source /opt/homebrew/Cellar/git/2.33.1_1/etc/bash_completion.d/git-completion.bash
fi
# 应用
source ~/.bash_profile
其他语法的高亮:
# ls 高亮配置
alias ll="ls -lG"
alias ls="ls -G"
# grep 高亮配置
alias grep='grep --color=auto'