Mercurial. Colour output piped to less

35

6

Operating system: Mac OS 10.6.2

I'd like to be able to see colour output when piping certain commands through less.

Two examples:

I've got ls aliased to ls --color=auto, so I'd like to be able to see colour when I do this:

ls -l | less

I've also got the color extension turned on in Mercurial, so I'd like to see colour output from:

hg diff | less

and

hg st | less

After some googling, it seems like some versions of less support either -r or -R to make this work, but no dice for me. I can't see anything in the man page that looks like what I need. (-r or -R SEEM to be the right options, but again, they don't seem to work)

mmacaulay

Posted 2010-03-27T20:37:51.773

Reputation: 663

1It looks like the color extension is always disabled when piped. Try these commands to see what I mean: ls --color=always | ls -R and hg diff --config color.mode=ansi | less -R – Harvey – 2010-08-10T17:34:32.033

Answers

30

For mercurial, you should use the pager extension rather than piping explicitly to less. This will play nicely with the colored output options of other hg commands.

richq

Posted 2010-03-27T20:37:51.773

Reputation: 1 130

13

I believe you have to use --color=always for ls if you want it to do colors even when not going to stdout. Then use -r on the less command

ls --color=always -l | less -r

Brandon Bodnar

Posted 2010-03-27T20:37:51.773

Reputation: 261

13

In your ~/.hgrc put:

[extensions]
color =
pager =

[pager]
pager = LESS='FRSXQ' less
quiet = True
attend = outgoing,incoming,diff,status,log,qdiff,blame,annotate,pdiff,glog

Joe HG

Posted 2010-03-27T20:37:51.773

Reputation: 131

Why the quiet = True part? – einpoklum – 2016-11-09T19:01:14.563

Nice! I recommend LESS='eiFRSXQ' to also quit after reaching EOF and ignore case during search – sebastian – 2017-01-18T18:22:35.747

12

Do:

$ hg diff --color always | less -R

I would alias "less -R" to less. I'm not sure if there's a way to provide default options to mercurial's commands so that you don't have to remember to type --color always for hg diff all the time.

stantonk

Posted 2010-03-27T20:37:51.773

Reputation: 270

1Or use $ hg diff --color always | less -FRSXQ for better less options. It will silently quit if diff fits the screen, for example. – jpbochi – 2012-07-28T15:08:06.440

1You can setup LESS="-R" alternatively to aliasing "less" itself. – blueyed – 2012-09-27T10:45:51.447

-F is to keep reading file as it's being produced, -S is to not line-wrap, -X something obscure, -Q avoids ringing terminal bell. – Evgeni Sergeev – 2014-01-23T00:17:23.060

2

If you don't want to use the Pager Extension for mercurial you can use the following command:

hg diff | cdiff

Good Person

Posted 2010-03-27T20:37:51.773

Reputation: 155

0

https://www.mercurial-scm.org/wiki/Defaults

[defaults] diff = --color always

user612789

Posted 2010-03-27T20:37:51.773

Reputation: 11

0

ls --color=auto means you get colour output when it's not being piped to something. Try ls --colour=auto | cat and you'll see what I mean.

I expect hg makes similar assumptions.

keturn

Posted 2010-03-27T20:37:51.773

Reputation: 131