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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1156069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:45:51  来源:igfitidea点击:

How to configure Mac OS X term so that git has color?

macosgitcommand-line-interface

提问by yinglcs

I've seen a Mac OS X git demo online in which it's configured to have multiple colors.

我在网上看过一个 Mac OS X git 演示,其中配置了多种颜色。

For example, his prompt is amber, his lsdirectory is purple and his git diffoutput has ~ 4 colors (pink, light green, red, pale yellow).

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

Can you tell me how can I configure Mac OS X terminal to achieve that? It's definitely Mac OS X Terminal.app, not iTerm.

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

回答by phloopy

William Purcell's answeronly enables color for the 'git diff' command. Do this to enable colors for all git commands:

William Purcell 的回答只为“git diff”命令启用颜色。这样做可以为所有 git 命令启用颜色:

$ git config --global color.ui true

回答by William Pursell

To display color in the output of git diff, you need to configure git. Try running

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

$ git config --global color.diff true

to set your $HOME/.gitconfig appropriately.

适当地设置你的 $HOME/.gitconfig 。

回答by Fredrik

It is not normally something you configure the terminal to do... The terminal is unaware of what it is showing but try this in your shell (if you're using bash, in some other shells you don't export but call setenv or something else):

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

export CLICOLOR=1
export TERM=xterm-color

You can then use LSCOLORS generatorto setup something that you can export using something like:

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

export LSCOLORS=fxfxcxdxbxegedabagacad

(the above should give you purple directories)

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

When you're done and satisfied with the result, add the three lines to either your /etc/bashrc or the .bashrc file in your user's home directory.

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

Edit:Also, in your terminal, make sure the checkbox "Display ANSI colors" (on the "Text" page) is checked.

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

回答by Marc M

This is what I use in my .profile file. Works like a charm because it allows me to see the current git branch as well as its state through the color. If you want to modify it please note that it's important to escape color codesin order to avoid line feed problems in long lines.

这是我在我的 .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}\]$ '

回答by codey

Open the terminal app, then open the preferences dialogue either through the menu (Terminal -> Preferences) or by pressing Command+,. Once the preferences dialogue opens, select the terminal style from the pane on the left, select Text from the button bar, than make sure the "Display ANSI colors" check box is checked.

打开终端应用程序,然后通过菜单(终端 -> 首选项)或按Command+打开首选项对话框,。首选项对话框打开后,从左侧窗格中选择终端样式,从按钮栏中选择文本,然后确保选中“显示 ANSI 颜色”复选框。

That will enable the colors on the terminal. To get colors in the output on the terminal, you will need to embed ANSI color commands in the data being sent to the terminal. How this is done is dependent on the commands. For example (as was shown above) the lscommand has a colors option. For the color codes, do a google lookup for "ansi color".

这将启用终端上的颜色。要在终端的输出中获取颜色,您需要在发送到终端的数据中嵌入 ANSI 颜色命令。这是如何完成的取决于命令。例如(如上所示)该ls命令有一个颜色选项。对于颜色代码,请在谷歌上搜索“ansi 颜色”。

回答by Amok

For colored ls output I would recommend installing the gnu coreutils and using that version of ls instead. For either version of ls you'll need to pass the correct flag to it, which is --color for the gnu version or -G for the standard OS X version. So you can do something like

对于彩色 ls 输出,我建议安装 gnu coreutils 并改用该版本的 ls。对于 ls 的任一版本,您都需要将正确的标志传递给它,即 gnu 版本的 --color 或标准 OS X 版本的 -G 。所以你可以做类似的事情

alias ls='ls --color'

in your .bashrc.

在您的 .bashrc 中。

To color your prompt you'll need to use the correct colors codes for your terminal, but mine uses

要为您的提示着色,您需要为您的终端使用正确的颜色代码,但我的使用

PROMPT="$(print '%{\e[0;38m%}%{\e[1;1m%]%}[%m:%c] %n%%%{\e[0m%}') "

to produce

生产

[hostname:directory] username%

in bold white.

在大胆的白色。