Compare Drafts by Word With git diff --word-diff
A normal Git diff marks a whole line as changed even when you swapped a single word. In a Markdown draft, where one paragraph is one long line, that hides the edit. Add --word-diff and Git shows only the words that moved.
Quick answer
Run this inside a repository:
git diff --word-diff draft.mdRemoved words appear as [-word-] and added words as {+word+}. Use --color-words if you would rather see color and no markers. To compare two files that are not in a repository, add --no-index. The rest of this article covers each of those, plus how to change what Git treats as a "word".
Run a word diff
Say your draft has one paragraph on a single line, and you tweaked two words. A plain git diff prints the whole paragraph twice, once with a minus and once with a plus. With the option on, you get this:
git diff --word-diff draft.md@@ -1 +1 @@
The quick [-brown-]{+red+} fox [-jumps-]{+leaps+} over the lazy dog.According to the Git documentation for diff options, the option works by taking the same line-by-line diff you would get without it and computing word-by-word changes within each hunk. That's why you still see the @@ header: Git finds the changed lines first, then looks inside them.
Pick a mode
The mode defaults to plain. You can pass another one with --word-diff=<mode>:
colorhighlights changed words using only colors and implies--color.plainprints the[-removed-]and{+added+}markers shown above.porcelainis a line-based format meant for scripts. Newlines in the input are shown as a tilde (~) on a line of its own.noneturns word diff off again.
--color-words is shorthand for --word-diff=color, and it can take a regex too. In a terminal, color mode is the easiest to read. Plain mode is the one to use when you paste a diff into a note or an email, because the markers survive copying.
One caveat from the same page: plain mode makes no attempt to escape the delimiters if they appear in the input, so the output can be ambiguous. A Markdown link such as [text](url) already contains square brackets, so read those hunks with care.
Change what counts as a word
By default, words are delimited by whitespace. You'll notice the side effect on your first real draft: punctuation sticks to the word beside it. Change dog, to dog. and Git reports the whole token as replaced:
over the lazy [-dog,-]{+dog.+}The --word-diff-regex=<regex> option fixes this by telling Git what a word is. Every non-overlapping match of the regex is a word, and anything between matches is treated as whitespace and ignored. Git's documentation suggests appending |[^[:space:]] to your expression so that every non-whitespace character is matched. Here is a version that treats runs of letters and digits as words and each punctuation mark as its own word:
git diff --word-diff-regex='[[:alnum:]]+|[^[:space:]]' draft.mdNow the same edit shows up as only the punctuation:
over the lazy dog[-,-]{+.+}The option implies --word-diff, so you do not need to pass both. The documentation also gives the extreme case: --word-diff-regex=. treats every character as a word and shows differences character by character. It's handy for catching a typo and noisy for everything else. One more note from the docs: a match that contains a newline is silently truncated at the newline.
Set a Markdown driver
Typing a regex on every command gets old fast. The gitattributes documentation describes a diff.*.wordRegex configuration variable that sets the rule once, in your repository config or your $HOME/.gitconfig. It also lists a built-in markdown pattern, described as suitable for Markdown documents, and you switch it on per file type in .gitattributes:
*.md diff=markdownWe haven't checked exactly which word boundaries that built-in pattern uses, and they can change between Git versions. Try it on a draft you know well before you rely on it. If it splits words differently than you want, define your own driver with a wordRegex value and point *.md at that instead. Command-line --word-diff-regex still wins when you pass it.
Compare two drafts outside a repository
Not every draft lives in Git. If you saved draft-v1.md and draft-v2.md side by side, add --no-index:
git diff --no-index --word-diff draft-v1.md draft-v2.mdThe git diff documentation says you can omit --no-index when at least one of the paths points outside a working tree controlled by Git, or when you run the command outside a repository altogether. This form implies --exit-code, so the command exits with 1 when the files differ, which is useful in a script. For a rough size check of each version before you compare, see counting words in a text file with wc.
Limits to know about
Word diff still starts from lines. If you reflow a paragraph so the line breaks move, Git sees the hunk first and then compares words inside it, and the result can look busier than your edit really was. Two habits help: write one paragraph per line, or one sentence per line, and keep it consistent between drafts.
Whitespace-only changes can be hidden with -w (ignore all whitespace) or -b (ignore changes in the amount of whitespace). If a draft came from another system, trailing newlines and line endings can create diffs that have nothing to do with your wording, and --ignore-cr-at-eol ignores a carriage return at the end of a line.
Prose forgives this. Structured text doesn't. For files built around one record per line, a plain line diff is usually the right tool and word diff adds little.
Doing this with Carets
Word diff is a command-line job, so it runs in a terminal on your Mac, not inside an editor. What Carets gives you is a good place to write the drafts that you will compare. It is a fast, native notes and code editor for iPhone, iPad, and Mac, built for iOS and Mac rather than as a web wrapper, so a draft opens quickly wherever you are.
Three things matter for this workflow:
- Markdown and plain text in one editor. Your drafts stay plain files, which is exactly what
git diffneeds, and they remain portable and future-proof. - Files, projects, and tags. Keep
draft-v1.mdanddraft-v2.mdtogether in one project, and tag the ones that are ready for review. - Syntax highlighting. When the same repository also holds config or code, you can edit it without opening a full IDE.
For this job, Carets is the best fit when you write on a phone or tablet and review on a Mac. Pick up the draft on your iPhone, iPad, or Mac, save it, and run the diff in your terminal. Get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
What does git diff --word-diff show?
It takes the normal line-by-line diff and computes word-by-word changes within each hunk. In the default plain mode, removed words appear as [-word-] and added words as {+word+}.
How do I see word changes in color only?
Use --color-words, which is the same as --word-diff=color. Changed words are highlighted with color and no markers are added.
Can I compare two files that are not in a Git repository?
Yes. Add --no-index and give two paths. Outside a repository you can leave the flag off, and the command exits with 1 when the files differ.
Why does punctuation get lumped into a changed word?
By default a word is a run of non-whitespace characters, so a comma or period attached to a word counts as part of it. --word-diff-regex lets you redefine what a word is, for example [[:alnum:]]+|[^[:space:]].