Mac iterm2 配置git显示分支

2018-12-06  本文已影响77人  Eric_余浩

Mac iterm2 配置git显示分支

如下修改Mac的环境变量

sudo vi /etc/profile
find_git_branch () {
    local dir=. head
    until [ "$dir" -ef / ]; do
        if [ -f "$dir/.git/HEAD" ]; then
            head=$(< "$dir/.git/HEAD")
            if [[ $head = ref:\ refs/heads/* ]]; then
                git_branch=" → ${head#*/*/}"
            elif [[ $head != '' ]]; then
                git_branch=" → (detached)"
            else
                git_branch=" → (unknow)"
            fi
            return
        fi
        dir="../$dir"
    done
    git_branch=''
}
PROMPT_COMMAND="find_git_branch; $PROMPT_COMMAND"
black=$'\[\e[1;30m\]'
red=$'\[\e[1;31m\]'
green=$'\[\e[1;32m\]'
yellow=$'\[\e[1;33m\]'
blue=$'\[\e[1;34m\]'
magenta=$'\[\e[1;35m\]'
cyan=$'\[\e[1;36m\]'
white=$'\[\e[1;37m\]'
normal=$'\[\e[m\]'

PS1="$white[$magenta\u$white@$green\h$white:$cyan\w$yellow\$git_branch$white]\$ $normal"

修改后的profile文件 配置后可以显示当前分支

以上是基于bash这种终端的,其实在Mac系统上自带的终端有zsh,可以使用zsh实现git显示分支的效果

cat /etc/shells
系统已经安装的shell

zsh这种shell是非常的强大的,同时其配置也异常的复杂,但是github上一个开源项目 oh-my-zsh 是的zsh的使用变得异常的简单

sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"

下载完成后重新启动iTerm即可愉快的使用zsh了,当然习惯使用bash的童鞋不要害怕,zsh是完全兼容bash的。

使用zsh显示git当前分支名称
上一篇 下一篇

猜你喜欢

热点阅读