How to ignore whitespace in a diff and see only the real changes
By TechCompare · Updated · How we check
Turn on Ignore whitespace when a file was reindented or reformatted. It works like `git diff -w`: spaces and tabs anywhere in a line stop counting, and the view still shows the text as written.
The example
1 change: 5 lines removed, 5 added
| Original: total.js | Changed: total.js | ||||
|---|---|---|---|---|---|
| 1 | function total(items) { | 1 | function total(items) { | ||
| 2 | Removed | 2 | Added | let sum = 0; | |
| 3 | Removed | 3 | Added | for (const item of items) { | |
| 4 | Removed | 4 | Added | sum += item.price * item.qty; | |
| 5 | Removed | 5 | Added | } | |
| 6 | Removed | 6 | Added | return sum; | |
| 7 | } | 7 | } | ||
Change the indentation of a block of code, or switch a file from tabs to spaces, and a plain diff marks every touched line as changed. The one line you really edited gets lost in the noise. Ignore whitespace fixes that. It compares each line with its spaces and tabs taken out, so only changes to the actual characters count.
How this is calculated
In the example, a JavaScript function went from 2-space to 4-space indentation, and one line also started multiplying by item.qty. As typed, the tool reports 1 change: 5 lines removed, 5 added, since every indented line differs. With Ignore whitespace on, it reports 1 change: 1 line removed, 1 added. That's the change a reviewer needs to see.
The patch copied with Ignore whitespace on holds only the real edit:
--- a/total.js
+++ b/total.js
@@ -1,7 +1,7 @@
function total(items) {
let sum = 0;
for (const item of items) {
- sum += item.price;
+ sum += item.price * item.qty;
}
return sum;
}It's built the way diff -w -u builds one. Its context lines come from the original text, so they keep the old indentation. Apply it and the edited line arrives with its new spacing, while every other line keeps the old one. If you need the new spacing everywhere, copy the patch with Ignore whitespace off.
Git has three levels of this, and it helps to know which one you want:
git diff -w(--ignore-all-space) ignores all whitespace, even where one line has a space and the other has none. This tool's switch works the same way.git diff -b(--ignore-space-change) ignores changes in the amount of whitespace, but a space added where there was none still counts.git diff --ignore-space-at-eolonly ignores whitespace at the end of lines.
Ignoring all whitespace has a cost. total = a+b and total = a + b become the same line, which is usually fine in code. In Python and YAML, though, indentation is part of the meaning. A line that moved into or out of a block counts as unchanged, so check those files with the switch off before you trust the result.
Verdict
Use Ignore whitespace for reindented code, reformatted SQL and text that was rewrapped with different spacing. Leave it off for Python, YAML and Makefiles, where whitespace carries meaning, and for any patch that has to recreate the new file exactly.
More Text Diff scenarios
Related guides
Frequently asked questions
Does Ignore whitespace also ignore blank lines?
--ignore-blank-lines is the option for that.Do tabs versus spaces count as a change?
Why does the copied patch still have the old indentation?
diff -w -u does. Turn the switch off before you copy if the patch has to recreate the new file exactly.Related tools
JSON Formatter
Validate, format, and minify JSON. Browse it as a tree, run JSONPath queries, compare two documents, or validate against a JSON Schema.
Use toolCharacter Counter
Count characters, words, sentences, paragraphs, and lines. Check writing limits and count specific characters or phrases.
Use toolText Encoding Converter
Convert text to Base64, binary, hex, URL encoding and HTML entities, and back again.
Use tool