Search code examples
regexgitdiffgit-diffposix-ere

How do I split words on punctuation in git diff?


I've have some luck with the following command:

git diff --color-words='[^][<>()\{},.;:?/|\\=+*&^%$#@!~`"'\''[:space:]]+|[][<>(){},.;:?/|\\=+*&^%$#@!~`"'\'']'

but it doesn't seem to negate the square brackets properly in the first character class.

I've tried this:

git diff --color-words='[^\]\[<>()\{},.;:?/|\\=+*&^%#@!~`"'\''[:space:]]+|[\]\[<>(){},.;:?/|\\=+*&^%#@!~`"'\'']'

in order to make the square brackets literal, but it fails with the message fatal: Invalid regular expression.

Edit:

The output I get is like so:

foobarfo]ob[ar

But what I'm after is:

fo]oob[ar


Solution

  • With word diff you will always see the word you removed next to the new one.

    This is why you have the word before your new change.

    Read this question, very detailed answer:

    Filtering a diff with a regular expression