The standard Linux diff utility is an essential tool for programmers comparing source code line by line. However, when comparing documentation, prose drafts, legal agreements, or configuration scripts where long paragraphs reside on single lines, line-oriented diff tools flag entire paragraphs as changed even when only a single word or punctuation mark was altered. The GNU wdiff command solves this problem by comparing files on a word-by-word basis, highlighting exact inline textual additions and deletions.
Why Use wdiff Over Standard diff?
While traditional diff treats lines as the fundamental unit of comparison, wdiff parses files as sequences of whitespace-delimited words. Understanding the differences between these approaches clarifies when to employ word-based auditing:
- Inline Prose Editing: When reviewing Markdown, LaTeX, or HTML drafts where line breaks do not mirror semantic sentences,
wdiffisolates precise phrasing modifications without displaying giant blocks of identical text. - Terminal Colour Highlights: With colour flags enabled, deletions appear in red strike-through brackets while additions appear in bold green, mimicking modern pull-request prose review interfaces directly in your shell.
- Statistical Summaries: The tool can output word counts, percentage similarity scores, and modification ratios between two distinct versions of a manuscript.
- Pipeline Integration: Because
wdiffoutputs clean bracketed delimiters, it pairs effectively withsed,awk, or terminal pagers likelessfor rapid automated filtering.
Installing wdiff on Linux Distributions
On many modern Linux installations, wdiff is not installed by default but is readily available in standard package repositories across all major distributions.
On Ubuntu, Debian, and Linux Mint:
sudo apt update && sudo apt install wdiff
On Fedora, RHEL, and CentOS Stream:
sudo dnf install wdiff
On Arch Linux and Manjaro:
sudo pacman -S wdiff
Verify installation and version details by running:
wdiff --version
Basic Word-by-Word Comparison Syntax
The simplest way to use wdiff is to pass two files as arguments. By default, deleted words are enclosed within [-deleted words-] brackets, and inserted words are enclosed within {+added words+} braces.
wdiff draft1.txt draft2.txt
Consider two files where draft1.txt contains “The quick brown fox jumps over the lazy dog.” and draft2.txt contains “The fast red fox leaps over the lazy dog.”
Running wdiff produces:
The [-quick brown-] {+fast red+} fox [-jumps-] {+leaps+} over the lazy dog.
The output keeps the unchanged context entirely intact and flags only the modified words inline.
Enabling Terminal Colour Highlights
Reading bracketed delimiters across long text blocks can strain readability. Using terminal colour codes makes changes immediately obvious to the naked eye.
To enable ANSI escape colours:
wdiff -n draft1.txt draft2.txt
Alternatively, if you want full terminal colour capability piped cleanly into less with colour support:
wdiff --terminal draft1.txt draft2.txt | less -R
In terminal mode, deletions appear in striking red bold text and insertions appear in bright green bold text, eliminating visual clutter from brackets entirely.
Useful wdiff Command Flags and Filtering Options
The wdiff command includes practical switches to customise how comparisons are processed and displayed:
- Ignore Case Differences (
-i): Prevents capitalisation changes from being marked as differences:wdiff -i file1.txt file2.txt - Show Only Inserted Words (
-2or-3): Suppress unchanged words and show only edits. For example, to view only deleted text:
To view only added words:wdiff -1 file1.txt file2.txt
To suppress common words and output strictly the differences:wdiff -2 file1.txt file2.txtwdiff -3 file1.txt file2.txt - Display Statistical Edit Summary (
-s): Displays word counts for both files, number of words preserved, words deleted, words added, and the overall percentage ratio of changes:wdiff -s manuscript_v1.md manuscript_v2.md - Customise Delimiter Strings (
-w,-x,-y,-z): Define custom start and end tokens for deletions and insertions to format output for markdown or HTML parsers:wdiff -w "<del>" -x "</del>" -y "<ins>" -z "</ins>" draft1.txt draft2.txt
Practical Workflow: Reviewing Git Commits with wdiff
You can easily connect wdiff into your version control routines when reviewing markdown documentation or technical specifications managed in Git repositories:
git diff HEAD~1 README.md | wdiff -d -
Passing -d - tells wdiff to read the standard unified diff output from stdin and render it as word-level changes, providing immediate clarity on paragraph-level adjustments.