Search code examples
gitgit-diff

Git: remove leading plus/minus from lines in diff


My question is rather simple, though I have had no luck finding an answer.

I'd like to remove the leading plus/minus symbols from each line in git diff. Before you ask why I wish to do this, allow me to outline my reasons:

  1. Lines that are exactly 80 chars will overflow by a single character, which just looks plain awkward
  2. The coloring is enough for me to distinguish between additions/deletions
  3. I'd prefer to keep my Terminal's window width at 80 chars (as opposed to an arbitrary 81 chars) to maintain consistency with everything else I do in my Terminal (outside of git)

Is there some config option for doing this? If not, how can I do this in a way that still allows me to page through my diff less-style?

Any insight would be greatly appreciated.


Solution

  • One option is to use sed to remove the undesired character from diff, while preserving the color:

    git diff --color | sed -r "s/^([^-+ ]*)[-+ ]/\\1/" | less -r
    

    (Note that you need to remove the leading space as well, as it is emitted by diff.)