1

I sometimes have long line of code in git repo (e.g. a json config file), and git diff will generate output like below.

long line

On an EC2 instance, I git diff doesn't wrap the content to the next line, generating following output (I can use left/right keys to navigate), which I personally prefer.

short line

Does any one know how I can config the git-diff to change from one behavior to another?

Ying Xiong
  • 4,578
  • 8
  • 33
  • 69
  • 2
    If you are using `less` as your pager, set the `-S` option. (I like to think of S as standing for `Sideways Scroll`.) The defaults in Git are complex: see http://stackoverflow.com/a/18781512/1256452. Note that setting `S` can interfere a bit with `F`: http://unix.stackexchange.com/q/231427/162084 – torek Oct 13 '16 at 02:31
  • If the pager is `vi` or `vim`, then you can get same result of the `less` by doing `git diff | vi -` or `git diff | vim -` too. – Masashi Dec 23 '20 at 21:25

2 Answers2

2

It's your terminal or pager program that wraps the lines, not git diff. Try redirecting the output of git diff into a file and open it with an editor that allows to control wrapping - you will see that the lines are not actually wrapped.

You can try this:

git diff|cut -c -$COLUMNS

Note however that it will disable colors and paging.

Leon
  • 31,443
  • 4
  • 72
  • 97
  • Passing `--color` to git diff will force colors again, but then the lines are visibly cut short before actually reaching '$COLUMNS', one char for each ANSI color char in the line. – Jonathan Hartley Nov 09 '17 at 18:10
1

Thank to torek's inline response above. Based on that, I realized I can do git diff | less -S to achieve what I want. It does better than the cut -c -$COLUMNS solution because it doesn't lose any context (I can still use left and right button to view all differences).

Ying Xiong
  • 4,578
  • 8
  • 33
  • 69