Ignore whitespace in Visual Studio Code git diff view
Asked Answered
E

5

146

Is it possible to ignore white-space when viewing code differences of a file (generated by GIT) in Visual Studio Code? That will be really helpful in checking for actual code additions/deletions before committing.

Estrada answered 24/11, 2016 at 6:15 Comment(0)
O
67

I believe OP is asking to ignore all whitespaces (including between words) not just trailing whitespaces.
Unfortunately, it's still not implemented and you can track the process status here: https://github.com/Microsoft/vscode/issues/43026

Orthognathous answered 9/9, 2018 at 19:42 Comment(4)
"hediet self-assigned this 3 weeks ago" please hediet, make it happenSupine
Do you know if there is also a feature request for diffEditor.ignoreLinebreaks? My search for diffEditor line break seems to have no relevant result. There is a comment that suggests to add ignore line breaksEncephalon
I created a feature request to ignore line breaksEncephalon
5 years later...Fall
C
169

Add on your settings.json:

"diffEditor.ignoreTrimWhitespace": true,
Cicisbeo answered 4/2, 2017 at 9:48 Comment(7)
EDIT: I found that this setting only controls if the diff editor shows changes in leading or trailing whitespace as diffs. It does not consider whitespaces in-between the same line. I want the diff editor to ignore all the whitespace in the code, not just leading and ending whitespace of a line.Estrada
It's also available in Preferences,Settings.Kierakieran
doesn't affect leading whitespaceOverzealous
File => Preferences => Settings => Diff Editor => Ignore Trim Whitespace.Animosity
Not sure when it was added, but there's an icon to toggle this at the top of the diff document pane now.Fullmouthed
whitespace toggle button looks like this: ¶Brigitta
Use scm.diffDecorationsIgnoreTrimWhitespace if you want to ignore whitespace on gutter indicator. Valid values for it are inherit|true|false. If you set inherit, it will inherit from diffEditor.ignoreTrimWhitespace.Industry
F
98

On the top right corner in diff view, there is a "Show Leading/Trailing Whitespace Differences" button (¶, called pilcrow or paragraph mark) to trim whitespace:

The "Show Leading/Trailing Whitespace Differences" button in VSCode Git Diff

Franklynfrankness answered 14/6, 2022 at 4:25 Comment(3)
fwiw, this feature was initially added in VS Code 1.20.0 by the commit 8c91f01fda90388138414d414ace2b230d3331c8 .Impudicity
Perhaps be the accepted best answer nowadays, though the OP asked the question years ago. Thank you!Muldoon
This button is only available when you are checking a revision, not during a mergeKeikokeil
O
67

I believe OP is asking to ignore all whitespaces (including between words) not just trailing whitespaces.
Unfortunately, it's still not implemented and you can track the process status here: https://github.com/Microsoft/vscode/issues/43026

Orthognathous answered 9/9, 2018 at 19:42 Comment(4)
"hediet self-assigned this 3 weeks ago" please hediet, make it happenSupine
Do you know if there is also a feature request for diffEditor.ignoreLinebreaks? My search for diffEditor line break seems to have no relevant result. There is a comment that suggests to add ignore line breaksEncephalon
I created a feature request to ignore line breaksEncephalon
5 years later...Fall
A
4

One idea is to format the documents using the same formatting standard and then compare the files.

Arsenide answered 30/7, 2022 at 15:10 Comment(2)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewGrantham
This does actually answer the question, and pretty well IMO.Memento
R
4

If you are searching for a solution that doesn't bluntly ignore all whitespace changes, but only those that don't have an effect on your code, you should look into semantic/structural diffs. They use the grammar rules of the programming language to distinguish whether a whitespace is relevant (e.g. within a string) or optional (e.g. between function arguments).

I had a similar issue and ended up developing my own extension (SemanticDiff) that adds a structural diff mode to VS Code. Here is an example how it ignores whitespace changes:

Ignored whitespace changes

You can get the extension from the marketplace. I am not aware of any other VS Code extension that implements such a feature.

Outside of VS Code, there are a few more options available, such as difftastic or diffsitter.

Retrospect answered 16/2, 2023 at 11:17 Comment(1)
Correct, there are plenty of online tools as well and even offline like WinMergeHypothesis

© 2022 - 2024 — McMap. All rights reserved.