A better less, playing nice with git

On the command line, I used to look at files with less or cat, depending on how long I expected the file to be. Of course I would constantly guess wrong, either blocking my screen with less showing only a handful of lines and making me press “q” needlessly, or having cat’s output scrolling past.

But recently I discovered how to make less do everything I want. -F or --quit-if-one-screen is self-explanatory and avoids needless “q”-presses, and -X or --no-init makes less not clear the screen when invoked. With these two options, less behaves like cat when it makes sense.

I enjoyed my new enhanced less-ing… until I invoked git diff. It looked like this, the line endings seemingly garbled:

ESC[1mdeleted file mode 100644ESC[m
ESC[1mindex bc7f954..0000000ESC[m
ESC[1m--- a/src/test/org/expasy/cvrelational/keywords/KeywordRoundtripTest.javaESC[m
ESC[1m+++ /dev/nullESC[m

Oops! The solution, thanks to those guys, is to add -R or --RAW-CONTROL-CHARS. I had git color its output automatically (git config --global color.ui auto), and less was choking on the control characters git added to its output.

Now, export LESS="-F -X -R" works like a charm.

About these ads

Tags: , ,

4 Responses to “A better less, playing nice with git”

  1. Andreas Demmer Says:

    Nice! Another snippet to enhance my beloved bash. Now ranks shortly before the “column -t” command. Every tried “mount | column -t” instead of “column”? All hail to the pretty output…

  2. thomas11 Says:

    Wow, column -t rocks! Thanks for this tip!

  3. edam Says:

    A better solution, so that the way you set up less for use with git is not set globally, is to set git’s pager to use less as you require. For example:

    git config –global core.pager ‘less $LESS -FRXS’

    That way, you can set your LESS environment variable how you like it and still get git to use less the way you describe.

  4. tiziano88 Says:

    Great, I added -F and -X to my LESS variable, and I could not figure out why it was not displaying the escape codes correctly, now it’s fixed! Thanks a lot!

Comments are closed.


Get every new post delivered to your Inbox.

%d bloggers like this: