37

How can I add , as a word separator for git diff --word-diff?

For instance I would like the diff to recognize that

function(A,B,C) -> function(A, B, C)

only has added the spaces instead of replacing the whole line.

1
  • 1
    The answers address commas and spaces. For commas only, we use --word-diff-regex='[^,]+' Aug 1, 2020 at 1:31

3 Answers 3

46

Use --word-diff-regex:

   --word-diff-regex=<regex>
       Use <regex> to decide what a word is, instead of considering runs
       of non-whitespace to be a word. Also implies --word-diff unless it
       was already enabled.

       Every non-overlapping match of the <regex> is considered a word.
       Anything between these matches is considered whitespace and
       ignored(!) for the purposes of finding differences. You may want to
       append |[^[:space:]] to your regular expression to make sure that
       it matches all non-whitespace characters. A match that contains a
       newline is silently truncated(!) at the newline.

       The regex can also be set via a diff driver or configuration
       option, see gitattributes(1) or git-config(1). Giving it explicitly
       overrides any diff driver or configuration setting. Diff drivers
       override configuration settings.

I never used it but i guess that git diff --word-diff-regex="[^[:space:],]+" may work.

4
  • 1
    Any idea how to set it in git config, to use this regex by default, when I specify --word-diff rather than me having to specify the regex each time.
    – sibaz
    Dec 13, 2016 at 14:45
  • 3
    Answered my own question: git config --global diff.wordRegex [^[:space:],]+
    – sibaz
    Dec 13, 2016 at 14:48
  • 15
    Only flaw in this is it won't show commas that were added/removed. To see those too, use --word-diff-regex="[^[:space:],]+|[,]+" Feb 17, 2017 at 15:40
  • @user6506750 It does show the comma for me.
    – Flimm
    Nov 22, 2023 at 16:46
12

In addition to KurzedMetal's answer: without the quotes it works even better since it doesn't break my bashs autocompletion when hitting TAB.

git diff --word-diff-regex=[^[:space:],]+

2

You can also try -w/--ignore-all-space, although that may ignore more whitespace than you like.

1
  • 2
    -b ignores a bit less whitespace Nov 27, 2014 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.