Line-wrapping `less` in OS X (specifically for use with 'git diff')

3

Using less +-S does not work (although it does ask Chop long lines (press RETURN)), and entering -S once inside less also does not work. Is there a solution?

I am usig OS X Mavericks (10.9.3).

less 418 (default) or less 458 (via MacPorts)

DilithiumMatrix

Posted 2014-07-06T16:04:06.970

Reputation: 477

What do "less 418" and "less 458" mean? Are they version numbers? Access rights? Formatting specifications? – Peter Mortensen – 2014-07-06T19:33:44.030

1@PeterMortensen: They're the less version number as output by less --version. OS X still ships version 418 in Yosemite, by the way, and I see the same buggy behavior with less -S on Yosemite, so this question is not dead, even if it is "answered". I've seen this behavior for years and years on multiple OSes, though so my belief is that less -S is just plain buggy. – Warren Young – 2015-06-17T21:39:51.373

How about icdiff: https://github.com/jeffkaufman/icdiff

– zhanxw – 2019-12-15T03:58:44.080

Answers

3

You can use fold to first wrap the text, then pipe to less:

fold -w 78 | less

Of course, -w sets the number of characters. By default it's 80. fold is POSIX and bundled with OS X.


Apparently, using less -r as the default Git pager will also achieve folding of long lines.

Otherwise, while the diff is being displayed, you can type -S, then Enter to toggle chopping/folding long lines.

slhck

Posted 2014-07-06T16:04:06.970

Reputation: 182 472

+1 for a cool work-around, but I'm actually hoping to use this for the (git) diff command --- so I don't think this will work – DilithiumMatrix – 2014-07-06T16:08:53.230

1Oh, you didn't mention. Does setting less -r as Git pager work in that case? – slhck – 2014-07-06T16:10:52.500

Yes! That works! (I saw that on this question but didn't try it because my man page said less -r did something completely different. Any idea why this works in git, but not terminal?

– DilithiumMatrix – 2014-07-06T16:13:08.297

I have no idea why exactly. I just remembered this trick. I'm curious why less -+S doesn't work in your case. In fact, in my setup (also under OS X), the diff shown in less is automatically line-wrapped, but I haven't set anything explicitly to enable it. – slhck – 2014-07-06T16:18:13.753

That's very strange! Maybe this is unique to me then. – DilithiumMatrix – 2014-07-06T16:19:08.350