it-swarm.cn

不能保留彩色输出?

我可以减少单色输出吗?

例如,git diff的输出是彩色的,但git diff | less没有。

364
ripper234

采用:

git diff --color=always | less -r

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

435
Stéphane Gimenez

另一种选择是启用颜色并使用“ 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-config的手册页中找到。 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.
61

使用-r--raw-control-chars)选项少一些,也可以使用-R(仅ANSI转义序列)。

我在~/.bashrc中有一个别名

alias rless='less -r'
29
enzotib

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

tree -C | less -r

以此类推,以ls为例:

ls -lR --color | less -r
17
Antonio Bardazzi

只是在“使用less -r”:

将环境变量LESS与值r一起使用(或将r加到已经存在的值上)

例如,当我在.bashrc

export LESS=-Xr

X停止 少退出时清除屏幕。

11
MortenSickel

如果有人有兴趣使用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

9
dimid

我知道这已经很老了,很多人已经提供了正确的答案,但是我想补充一点,如果只需要ANSI颜色作为less -Rmay,最好使用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
6
Naheel