能少保留彩色输出吗?

rip*_*234 471 less colors

我能得到更少的不单色输出吗?

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

Sté*_*nez 561

用:

git diff --color=always | less -r
Run Code Online (Sandbox Code Playgroud)

--color=alwaysgit即使输出是管道(不是 tty),是否也可以告诉输出颜色代码。并且-r可以告诉less解释这些颜色代码和其他转义序列。使用-R仅ANSI颜色代码。

  • 使用`less -R` 不是更好(或`/etc/profile` 中的`export LESS=R`)?为什么要允许它显示除 ANSI“颜色”转义序列以外的任何内容?此外,手册页说`警告:使用 -r 选项时,less 无法跟踪屏幕的实际外观(因为这取决于屏幕如何响应每种类型的控制字符)。因此,可能会导致各种显示问题,例如长行在错误的位置被拆分。` (46认同)
  • 如果你忘记指定 `-r` 选项,你可以在 `less` 提示符下输入“-r”。这当然适用于大多数或全部较少的选项(即,`-i` 打开忽略大小写)。 (16认同)
  • `grep --color=always` 的工作方式相同。这不是这个问题或答案的一部分,但我是通过谷歌搜索这个问题来到这里的。 (10认同)
  • 与此问答并行,`watch --color 'git diff --cached --color=always'` 和它的朋友们可以给你带来一些额外的惊喜。 (6认同)
  • @ripper234。使用最近的 gits,`git config color.ui true` 应该足以获得彩色输出,*和* 自动运行分页器以获得长输出。 (4认同)
  • 我更喜欢“-R”,因为它只影响颜色控制字符。使用“-r”,即使换行符也总是按字面打印,因此“-S”不再起作用 (4认同)

Ger*_*ncy 72

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

git config --global color.ui true
git config --global core.pager 'less -r'
Run Code Online (Sandbox Code Playgroud)

这导致

[color]
    ui = true
[core]
    pager = less -r
Run Code Online (Sandbox Code Playgroud)

在你的 ~/.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.
Run Code Online (Sandbox Code Playgroud)

  • 使用 Git 2.11,我发现 `color.ui true` 不适用于寻呼机,但 `color.ui always` 可以。自从发布答案以来,这可能已经改变。 (12认同)
  • 这个选项(启用颜色并默认使用 'less -r' 作为你的寻呼机)和 `color.ui true` 确实适用于像 `git -p diff` 和 `git -p status`(其中 `-p`意味着将所有输出通过管道传送到 $PAGER,默认情况下它是 `less`),即使对于相当旧的 git 版本(例如,Git 1.7.1)也是如此。但是当你直接指定 `git status | 时,你仍然需要 `color.ui always`(它告诉 git 输出颜色代码“即使输出是管道(不是 tty)”)来获得彩色输出。less -r` 或 `git diff | 少 -r`。当您明确指定输出重定向时,从寻呼机退出后您将看不到输出。 (3认同)

enz*_*tib 33

使用-r( --raw-control-chars) 选项来减少,或者也使用-R(仅限 ANSI 转义序列)。

我有一个别名 ~/.bashrc

alias rless='less -r'
Run Code Online (Sandbox Code Playgroud)

  • 如果可视化混乱,您可以使用 `\less`。 (9认同)
  • 实际上我只是做了`alias less='less -r'`。有什么理由不? (8认同)
  • 呃,你不必将 `less` 别名为任何东西,只需将 `$LESS` 环境变量设置为你总是想提供给 `less` 的标志集,例如 `export LESS='-MRq -z- 2 -j2'` (8认同)
  • @ripper234:我依稀记得在不需要时使用`-r`可能会导致可视化问题。 (3认同)
  • 例如,当您使用“less -r”管道“ack”时,您会丢失回车符 (2认同)

小智 22

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

tree -C | less -r
Run Code Online (Sandbox Code Playgroud)

依此类推ls

ls -lR --color | less -r
Run Code Online (Sandbox Code Playgroud)


dim*_*mid 15

如果有人有兴趣寻呼JSON与jqless它可以使用来实现:

jq -C <jq args> file.json | less -R
Run Code Online (Sandbox Code Playgroud)

例如

jq -C . file.json | less -R
Run Code Online (Sandbox Code Playgroud)

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


小智 14

我知道这是旧的,许多人已经提供了正确的答案,但我想补充一点,它始终是更好地使用less -R,而不是less -r如果你只需要ANSI颜色-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
Run Code Online (Sandbox Code Playgroud)


Mor*_*kel 13

只是在“使用less -r”上添加另一个版本:

使用LESS值为 r的环境变量(或将 r 添加到它已经是的任何内容)

例如,当我在我的.bashrc

export LESS=-Xr
Run Code Online (Sandbox Code Playgroud)

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