Usage
Word-level diff in practice — flags, examples, and a note on CJK.
Basic word diff
The simplest invocation: two files, output to stdout.
$ wdiff a.txt b.txt
Inserted words are wrapped in {+...+}; deleted
words in [-...-]. Common words pass through
untouched.
Common flags
| flag | effect |
|---|---|
-1, --no-deleted | Suppress deleted words (show only common + inserted). |
-2, --no-inserted | Suppress inserted words. |
-3, --no-common | Suppress common words (show only inserted + deleted). |
-a, --auto-pager | Auto-pipe output to $PAGER when stdout is a tty. |
-d, --diff-input | Read a single unified diff on stdin (re-format into word-level). |
-i, --ignore-case | Fold case while comparing. |
-s, --statistics | Print word counts (total / common / inserted / deleted / changed) to stderr. |
-t, --terminal | Use termcap standout mode for emphasis (bold + underline via the controlling terminal's so/se/us/ue capabilities). |
-w STR, --start-delete=STR | Override the [- marker (e.g. to use ANSI escapes for colour). |
-x STR, --end-delete=STR | Override the -] marker. |
-y STR, --start-insert=STR | Override the {+ marker. |
-z STR, --end-insert=STR | Override the +} marker. |
CJK and UTF-8
wdiff uses isspace() to detect word
boundaries, so on CJK text without spaces (Chinese / Japanese
/ Korean) the entire line is treated as one "word". The diff
is still useful — you see line-level changes —
but you do not get intra-line character-level visibility.
$ printf '今天天气很好\n' > a.txt
$ printf '今天天气不错\n' > b.txt
$ wdiff a.txt b.txt
[-今天天气很好-]{+今天天气不错+}
For true character- or word-level CJK diff, use
ljh-sh/dwdiff
(vendored dwdiff 2.1.4 + libicu 78,
ICU-aware tokenization). wdiff is the
whitespace-splitting tool; dwdiff is the
Unicode-segmenting tool. Both are ljh-sh distributions.
Re-formatting a unified diff
Pass an existing diff -u output to wdiff
-d to re-format it at the word level. Useful for
rendering a code review in a terminal-friendly way.
$ git diff | wdiff -d
Statistics
$ wdiff -s a.txt b.txt
a.txt: 7 words
b.txt: 7 words 6 86% common 1 14% deleted 0 0% changed
Statistics go to stderr; the marked output goes to
stdout. So wdiff -s a.txt b.txt >/dev/null
is a common pattern in shell scripts.