Casebash Casebash - 4 months ago 12
Git Question

Git - Color words excluding {}

I am using git with --color-words to view my diff. In my diff, it shows that I removed

<b>{{ljcount}}</b>&nbsp;&nbsp;&nbsp;Changes


And that I added:

<b>{{skills_limits}}</b>&nbsp;&nbsp;&nbsp;Changes


This is larger than what I would like it to be (I want the word boundary to be at the {}). I tried playing around with
--word-diff-regex
, but I couldn't find a regex to make it work. How can I achieve this result?

Answer

From git help diff:

   --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.

The following expression will make a word be any string of characters and underscore, or any non-whitespace character.

$ git diff --color-words --word-diff-regex='\\w+|[^[:space:]]'