Can I get less not to monochrome its output?
E.g., the output from
git diff is colored, but
git diff | less is not.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Another option would be to enable colors and use 'less -r' as your pager.
git config --global color.ui true git config --global core.pager 'less -r'
This results in
[color] ui = true [core] pager = less -r
in your ~/.gitconfig
For more information see the Pro Git book.
Possible values for
color.ui can be found in the man page of git-config. The output of
man git-config | grep "color.ui$" -A8 is
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.
In case anyone is interested in paging a json with
less it can be achieved using:
jq -C <jq args> file.json | less -R
jq -C . file.json | less -R
I know this is old and many have already provided the right answer but I would like to add that it is always better to use
less -R and not
less -r if you only need ANSI colors as
-r may cause problems in displaying the characters.
From the manual:
-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
Just to add another version on the "use
Use the environment variable
LESS with the value r (or add r to whatever it already is)
E.g., as I use it in my
The X stops the screen from clearing when exiting less.
I believe the ultimate solution is to use
unbuffer which can be found in the
In essence, you retain
git diff color by running the command as follows:
unbuffer git --no-pager diff [some_file] | less -r
You have to use
--no-pager as otherwise the command hangs.
I'd have liked to take credit for this but jcubic got there long before me.
I was looking for a way to achieve this for
yq to parse YAML files because I have to work with large YAML files on a daily basis. A quick look at the
man yq confirmed that
-C flag is also available for
yq. The following worked for me.
kustomize build k8s/overlays/dev | yq eval -C | less -r
I added and alias in my
~/.zshrc file for convenience
alias yqlc='yq eval -C | less -r'
kustomize build k8s/overlays/dev | yqlc
works like a charm.