Linux|Mac自定义命令行格式及命令提示: 终端命令提示符高亮 & Git命令提示

效果图示例:

提示符内容规则

命令提示符部分: 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'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值