如何配置Mac OS X术语以便git有颜色?

yin*_*lcs 195 git macos command-line-interface

我在网上看过一个Mac OS X git demo,它配置了多种颜色.

例如,他的提示是琥珀色,他的ls目录是紫色的,他的git diff输出有~4种颜色(粉红色,浅绿色,红色,淡黄色).

你能告诉我如何配置Mac OS X终端来实现这一目标吗?它绝对是Mac OS X Terminal.app,而不是iTerm.

phl*_*opy 387

William Purcell的回答只能为'git diff'命令启用颜色.这样做是为所有git命令启用颜色:

$ git config --global color.ui true
Run Code Online (Sandbox Code Playgroud)

  • 这不起作用 (3认同)

Wil*_*ell 56

要在git diff的输出中显示颜色,您需要配置git.试试跑步

$ git config --global color.diff true
Run Code Online (Sandbox Code Playgroud)

适当地设置$ HOME/.gitconfig.


Fre*_*rik 12

这通常不是你配置终端做的事情......终端不知道它显示的是什么但是在你的shell中尝试这个(如果你正在使用bash,在其他一些shell你不导出但是调用setenv或者别的):

export CLICOLOR=1
export TERM=xterm-color
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用LSCOLORS生成器来设置可以使用以下内容导出的内容:

export LSCOLORS=fxfxcxdxbxegedabagacad
Run Code Online (Sandbox Code Playgroud)

(以上应该给你紫色的目录)

当您完成并对结果感到满意时,将三行添加到用户主目录中的/ etc/bashrc或.bashrc文件中.

编辑:此外,在您的终端中,确保选中"显示ANSI颜色"复选框(在"文本"页面上).


Mar*_*c M 10

这是我在.profile文件中使用的内容.像魅力一样工作,因为它允许我通过颜色看到当前的git分支以及它的状态.如果要修改它,请注意避免使用颜色代码以避免长行中的换行问题非常重要.

# Setting GIT prompt
c_cyan=`tput setaf 6`
c_red=`tput setaf 1`
c_green=`tput setaf 2`
c_sgr0=`tput sgr0`

branch_color ()
{
    if git rev-parse --git-dir >/dev/null 2>&1
    then
        color=""
        if git diff --quiet 2>/dev/null >&2 
        then
            color=${c_green}
        else
            color=${c_red}
        fi
    else
        return 0
    fi
    echo -n $color
}

parse_git_branch ()
{
    if git rev-parse --git-dir >/dev/null 2>&1
    then
        gitver="["$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')"]"
    else
        return 0
    fi
echo -e $gitver
}

#It's important to escape colors with \[ to indicate the length is 0
PS1='\u@\[${c_red}\]\W\[${c_sgr0}\]\[\[$(branch_color)\]$(parse_git_branch)\[${c_sgr0}\]$ '
Run Code Online (Sandbox Code Playgroud)