例如,git diff的输出是彩色的,而git diff | less的输出不是彩色的。

评论

在超级用户上可能是相同的:superuser.com/questions/117841/…

相关但不完全相同的问题。链接大约更少。

#1 楼

使用:

git diff --color=always | less -r


--color=always可以告诉git输出颜色代码,即使输出是管道(不是tty)。 -r在那里告诉less解释那些颜色代码和其他转义序列。请仅将-R用于ANSI颜色代码。

评论


@ ripper234。对于最新的git,git config color.ui true应该足以获得彩色输出,并自动为长输出运行寻呼机。

–StéphaneGimenez
2011年8月24日12:48



与此问题同时,观看--color'git diff --cached --color = always'及其朋友可以为您带来更多的惊喜。

–阿洛瓦·马哈德(Alois Mahdal)
13年7月26日在9:19

使用更少的-R是否更好(或在/ etc / profile中导出LESS = R)?为什么要允许它显示除ANSI“颜色”转义序列以外的任何内容?另外,手册页上还显示警告:使用-r选项时,很少有人无法跟踪屏幕的实际外观(因为这取决于屏幕对每种控制字符的响应方式)。因此,可能导致各种显示问题,例如长线在错误的位置被分割。

–x-yuri
2014年6月10日12:57



而且,如果您忘记指定-r选项,则只需在较少的提示下键入“ -r”。这当然适用于大多数或所有较少的选项(即,-i可以打开ignorecase)。

–haridsv
16年1月7日,9:32

grep --color = always的工作方式相同。这不是此问题或答案的一部分,但我是通过谷歌搜索该问题而来到这里的。

–弗兰克·布莱斯(Frank Bryce)
16 Dec 6'在14:37

#2 楼

另一种选择是启用颜色并使用'less -r'作为您的传呼机。

git config --global color.ui true
git config --global core.pager 'less -r'


这将导致

[color]
    ui = true
[core]
    pager = less -r


〜/ .gitconfig中的

有关更多信息,请参阅Pro Git书。

color.ui的可能值可以在git-的手册页中找到配置。 man git-config | grep "color.ui$" -A8的输出是

color.ui
    This variable determines the default value for variables such as color.diff and
    color.grep that control the use of color per command family. Its scope will expand as
    more commands learn configuration to set a default for the --color option. Set it to
    false or never if you prefer Git commands not to use color unless enabled explicitly
    with some other configuration or the --color option. Set it to always if you want all
    output not intended for machine consumption to use color, to true or auto (this is the
    default since Git 1.8.4) if you want such output to use color when written to the
    terminal.


评论


使用Git 2.11,我发现color.ui true不能与寻呼机一起使用,但是color.ui总是可以。自从发布答案以来,情况可能已经改变。

–汤姆·齐奇(Tom Zych)
17年1月2日在14:18

此选项(启用颜色并默认使用'less -r'作为您的传呼机)与color.ui true确实适用于git -p diff和git -p status之类的命令(其中-p表示将所有输出通过管道传递到$ PAGER,默认情况下,它会更少),即使对于相当旧的git版本(例如,Git 1.7.1)也是如此。但是当您直接指定git status |时,仍然仍然需要color.ui(告诉git输出颜色代码“即使输出是管道(不是tty)”)也要获得彩色输出。 less -r或git diff |少-r。当您明确指定输出重定向时,退出分页器后将看不到输出。

– kenichi
18年1月19日在17:06

git状态|始终设置color.ui时,less和git -p状态对于我来说似乎完全相同-但由于前者都需要不鼓励使用的配置选项并且更长,因此我将简单地选择后者^^

– Xerus
3月28日13:01

#3 楼

请使用更少的-r--raw-control-chars)选项,或者也可以使用-R(仅ANSI转义序列)。

评论


实际上,我只是做了别名less ='less -r'。有没有理由吗?

–ripper234
2011年8月24日12:25



@ ripper234:我隐约记得在某些情况下使用-r可能会导致可视化问题。

– Enzotib
11年8月24日在12:29

例如,当您用较少的-r传递ack时,您将丢失回车符

–新亚历山大
2013年1月14日15:58

如果可视化混乱,则只能使用\ less。

–布赖恩·彼得森(Brian Peterson)
13-10-26在0:41

呃,您不必为任何东西都少用别名,只需将$ LESS环境变量设置为您一直希望提供给少的标志集,例如export LESS ='-MRq -z-2 -j2'

– wjv
17年8月30日在19:28

#4 楼

tree还可以选择强制启用以下颜色:

tree -C | less -r


ls等等:

ls -lR --color | less -r


#5 楼

只需在“ use less -r”上添加另一个版本:

使用值为r的环境变量LESS(或将r添加到已经存在的值中)

我在我的.bashrc中使用它:

export LESS=-Xr


X退出较少时会阻止屏幕清除。

评论


不幸的是,-X也为我中断了鼠标滚动:c

– Xerus
18年4月17日在11:27

#6 楼

我知道这已经很老了,许多人已经提供了正确的答案,但是我想补充一点,如果只需要ANSI颜色,最好使用less -R而不是less -r,因为-r可能会导致显示字符时出现问题。
来自手册:

  -r or --raw-control-chars
         Causes "raw" control characters to be displayed.   The  default
         is  to display control characters using the caret notation; for
         example, a control-A (octal 001) is displayed as  "^A".   Warn‐
         ing:  when the -r option is used, less cannot keep track of the
         actual appearance of the screen (since this depends on how  the
         screen responds to each type of control character).  Thus, var‐
         ious display problems may result,  such  as  long  lines  being
         split in the wrong place.

  -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



#7 楼

如果有人有兴趣使用jqless分页json,可以使用以下方法实现:

jq -C <jq args> file.json | less -R


例如

jq -C . file.json | less -R


来源:https://github.com/stedolan/jq/issues/764#issuecomment-95355331