bash git 中的 ANSI 颜色显示不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8484167/
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
ANSI color in git is not displayed correctly
提问by Rocky
Recently, I switched to SLES 11. I found a problem for git command. All the ANSI color could not be rendered. Instead, it shows the ANSI code like this:
最近换了SLES 11,发现git命令有问题。无法呈现所有 ANSI 颜色。相反,它显示了这样的 ANSI 代码:
*ESC[33m*commit 0a02124a0fd85c1f0094bcdf15bac1645e3c8630ESC[m
* ESC[33m*提交 0a02124a0fd85c1f0094bcdf15bac1645e3c8630ESC[m]
note: the ansi color in 'ls' works very well.
注意:'ls' 中的 ansi 颜色效果很好。
回答by sarnold
Try setting your LESSenvironment variable to include the -Roption:
尝试设置您的LESS环境变量以包含该-R选项:
LESS=-R git grep ...
If this works, add export LESS=-Rto your ~/.bashrcor ~/.profileor similar shell startup file.
如果这个作品,添加export LESS=-R到您~/.bashrc或~/.profile或类似的shell启动文件。
-R or --RAW-CONTROL-CHARS
Like -r, but only ANSI "color" escape sequences are
output in "raw" form. Unlike -r, the screen
appearance is maintained correctly in most cases.
ANSI "color" escape sequences are sequences of the
form:
ESC [ ... m
where the "..." is zero or more color specification
characters
回答by Cascabel
The problem, as others have noted, is that your terminal is fine, but when Git invokes the pager, it's not interpreting the ANSI color codes correctly.
正如其他人所指出的,问题在于您的终端很好,但是当 Git 调用寻呼机时,它没有正确解释 ANSI 颜色代码。
I'd start out by unsettingLESSin your environment; it sounds like you might have previously been setting it to something obscuring what Git needs. If that solves it, there you are. If you really must customize LESS, note that Git starts out with FRSXas default, so be wary of changing those if you don't need to.
我开始时通过取消设置LESS在您的环境; 听起来您之前可能已经将其设置为模糊 Git 需要的东西。如果这解决了它,那就是你。如果您确实必须自定义LESS,请注意 Git 开始时FRSX是默认的,因此如果您不需要,请小心更改它们。
If you dofor whatever reason want LESSin your environment different from what you want for Git, the ideal way to deal with Git and the pager is through the core.pagerconfig variable. To quote the manpage:
如果你这样做无论出于何种原因想LESS从你想要的Git的环境不同,理想的方式来处理Git和寻呼机是通过core.pager配置变量。引用联机帮助页:
The command that git will use to paginate output. Can be overridden with the GIT_PAGER environment variable. Note that git sets the LESS environment variable to FRSX if it is unset when it runs the pager. One can change these settings by setting the LESS variable to some other value. Alternately, these settings can be overridden on a project or global basis by setting the core.pager option. Setting core.pager has no affect on the LESS environment variable behaviour above, so if you want to override git's default settings this way, you need to be explicit. For example, to disable the S option in a backward compatible manner, set core.pager to
less -+$LESS -FRX. This will be passed to the shell by git, which will translate the final command toLESS=FRSX less -+FRSX -FRX.
git 将用于对输出进行分页的命令。可以用 GIT_PAGER 环境变量覆盖。请注意,如果在运行寻呼机时未设置 LESS 环境变量,则 git 会将 LESS 环境变量设置为 FRSX。可以通过将 LESS 变量设置为其他值来更改这些设置。或者,可以通过设置 core.pager 选项在项目或全局基础上覆盖这些设置。设置 core.pager 对上面的 LESS 环境变量行为没有影响,所以如果你想以这种方式覆盖 git 的默认设置,你需要明确。例如,要以向后兼容的方式禁用 S 选项,请将 core.pager 设置为
less -+$LESS -FRX。这将通过 git 传递给 shell,它将最终命令转换为LESS=FRSX less -+FRSX -FRX.
That, combined with some knowledge of the options you want, should get you where you want to be. (The fancy backward-compatible method works by disabling all options currently in LESS, then adding back in the ones you want.)
结合您想要的选项的一些知识,应该可以让您到达您想要的位置。(花哨的向后兼容方法的工作原理是禁用当前中的所有选项LESS,然后重新添加您想要的选项。)
回答by dhavald
In git, you can change your pager to use the -Roption:
在 git 中,您可以更改寻呼机以使用以下-R选项:
git config --global core.pager "less -R"
回答by robster_guy
For me, this did not work:
对我来说,这不起作用:
git config --global core.pager less -R
git config --global core.pager less -R
So instead i appended the following to my ~/.gitconfig file
因此,我将以下内容附加到我的 ~/.gitconfig 文件中
[core]
pager = less -R
To test it i did
为了测试它我做了
git log --graph --pretty=format:"%C(yellow)%h%Creset%C(blue)%d%Creset %C(white bold)%s%Creset %C(white dim)(by %an %ar)%Creset" --all
git log --graph --pretty=format:"%C(yellow)%h%Creset%C(blue)%d%Creset %C(white bold)%s%Creset %C(white dim)(by %an %ar)%Creset" --all
回答by Rocky
OK, I got it. This problem is to do with the variable LESS.
好,我知道了。这个问题与变量 LESS 有关。
following line resolve this problem:
以下行解决了这个问题:
export LESS="-erX"
回答by Pavel Prischepa
I had the same problem. But why I need to configure a git in one machine and no need on another? I want to fix the source of the problem because
我有同样的问题。但是为什么我需要在一台机器上配置一个 git 而不需要在另一台机器上呢?我想解决问题的根源,因为
git config --global core.pager "less -R"
git config --global core.pager "less -R"
looks like a workaround for me.
对我来说似乎是一种解决方法。
回答by Badger
Whoever ever winds up here, in my case it was solved by setting core.pager to an empty string, instead of unsetting it:
无论谁走到这里,就我而言,它是通过将 core.pager 设置为空字符串而不是取消设置来解决的:
git config --global core.pager ''
This was from https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
这是来自https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration
回答by Milan
I had similar problem with ANSI color escape sequences in Git for Windows v2.7.1 in Laravel Artisan and Symfony consoles. Here mentioned LESSsolution didn't solve the problem.
我在 Laravel Artisan 和 Symfony 控制台中的 Git for Windows v2.7.1 中遇到了类似的 ANSI 颜色转义序列问题。这里提到的LESS解决方案没有解决问题。
Since Git for Windows opens terminal with bash --login -i, this line entered after terminal started did work for me:
由于适用于 Windows 的 Git 打开终端bash --login -i,因此在终端启动后输入的这一行对我有用:
bash

