Posts Tagged ‘git’

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.