# Text Diff

> Compare two blocks of text and see exactly what changed, line by line, with inline word-level highlighting on edited lines. Optionally ignore whitespace or case. Everything is computed in your browser; your text is never sent anywhere.

- Tool: https://ronutz.com/en/tools/diff
- Family: Text & utilities

---

## What it does

Compare two blocks of text and see exactly what changed: a line-by-line view of additions and deletions, with word-level highlighting inside lines that were edited so you can spot the specific change. You can optionally ignore whitespace or case. Everything is computed in your browser; your text is never sent anywhere.

## How a diff is computed

A good diff is not a naive line-by-line comparison; it finds the smallest set of changes that turns the first text into the second. The tool does this by computing the **longest common subsequence** (LCS) of the two texts: the longest sequence of tokens that appears, in order, in both. Whatever is in that common subsequence is unchanged; everything in the first text but not in the LCS is a **deletion**, and everything in the second text but not in the LCS is an **insertion**. This is the idea at the heart of the classic Myers diff algorithm, and it produces a minimal edit script, the fewest additions and deletions that explain the difference.

## Two levels: lines and words

The same LCS operation runs at two granularities. For the main view, the tokens are whole lines, which gives you the familiar added-and-removed line list. For a line that changed rather than being added or removed outright, the tool runs the diff again with words, spaces, and punctuation as the tokens, and highlights just the parts of the line that actually differ. That inline highlighting is what turns "this line changed" into "this word changed".

## Determinism

When two different edit scripts are equally short, a diff has to pick one, and the tool always picks the same way (it prefers a deletion when the two directions are equally good). That fixed tie-break makes the output deterministic: the same two inputs always produce the same diff, with no dependence on timing or anything external.

## Using it

Paste the original text in one side and the changed text in the other, and read the line-by-line differences with inline word highlights. Turn on the ignore-whitespace or ignore-case options when you care about content rather than formatting.

## Standards and references

- [Myers (1986) - An O(ND) Difference Algorithm and Its Variations (Algorithmica 1:251-266)](http://www.xmailserver.org/diff2.pdf) - the LCS / shortest-edit-script algorithm behind diff
- [POSIX diff utility - The Open Group Base Specifications](https://pubs.opengroup.org/onlinepubs/009695399/utilities/diff.html) - the diff utility and its minimal change-list output

## Related reading

- [How text diff works](https://ronutz.com/en/learn/how-diff-works.md): A diff finds the smallest set of insertions and deletions that turns one text into another. Underneath is the longest common subsequence: the lines both versions share, in order, form the unchanged backbone, and everything else is an add or a remove.
- [Minimal Edits: Why a Diff Can Look Wrong](https://ronutz.com/en/learn/diff-minimal-edits.md): A diff shows the smallest set of insertions and deletions that turns one text into the other. Because the smallest set is not unique and the algorithm has to choose, a diff can align lines in ways that look counterintuitive, blaming the wrong block or splitting a moved section. Knowing this makes odd diffs readable.
- [Reading a diff](https://ronutz.com/en/learn/reading-a-diff.md): How to read a line-by-line diff: unchanged, added, and removed lines, the plus and minus markers, both sides' line numbers, inline word highlighting, and what ignore-whitespace and ignore-case actually change. Plus the things a diff cannot tell you.
- [Three-Way Diffs and Merge Conflicts](https://ronutz.com/en/learn/diff-three-way-and-merge-conflicts.md): A normal diff compares two versions and cannot tell which one changed. A three-way diff adds a common ancestor, which is what makes automatic merging possible and what produces the <<<<<<< ======= >>>>>>> conflict markers. This explains the third input and how to read and resolve a conflict.
- [Word and Character Level Diffs](https://ronutz.com/en/learn/diff-word-and-character-level.md): A line diff marks a whole line as changed even when a single character moved. Word-level and character-level diffs highlight the exact part of the line that changed, which is far easier to read for prose, long lines, and small edits. This covers the difference and when each is the right lens.
