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

flageffect
-1, --no-deletedSuppress deleted words (show only common + inserted).
-2, --no-insertedSuppress inserted words.
-3, --no-commonSuppress common words (show only inserted + deleted).
-a, --auto-pagerAuto-pipe output to $PAGER when stdout is a tty.
-d, --diff-inputRead a single unified diff on stdin (re-format into word-level).
-i, --ignore-caseFold case while comparing.
-s, --statisticsPrint word counts (total / common / inserted / deleted / changed) to stderr.
-t, --terminalUse termcap standout mode for emphasis (bold + underline via the controlling terminal's so/se/us/ue capabilities).
-w STR, --start-delete=STROverride the [- marker (e.g. to use ANSI escapes for colour).
-x STR, --end-delete=STROverride the -] marker.
-y STR, --start-insert=STROverride the {+ marker.
-z STR, --end-insert=STROverride 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.