Sté*_*nez 561
用:
git diff --color=always | less -r
Run Code Online (Sandbox Code Playgroud)
--color=alwaysgit即使输出是管道(不是 tty),是否也可以告诉输出颜色代码。并且-r可以告诉less解释这些颜色代码和其他转义序列。使用-R仅ANSI颜色代码。
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)
enz*_*tib 33
使用-r( --raw-control-chars) 选项来减少,或者也使用-R(仅限 ANSI 转义序列)。
我有一个别名 ~/.bashrc
alias rless='less -r'
Run Code Online (Sandbox Code Playgroud)
小智 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与jq和less它可以使用来实现:
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可能会导致显示字符的问题。
从手册:
Run Code Online (Sandbox Code Playgroud)-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
Mor*_*kel 13
只是在“使用less -r”上添加另一个版本:
使用LESS值为 r的环境变量(或将 r 添加到它已经是的任何内容)
例如,当我在我的.bashrc:
export LESS=-Xr
Run Code Online (Sandbox Code Playgroud)