11

Suppose I am working on a long edit session of a previously existing file and have not saved the edits/changes yet. I would like to compare this changed, yet unsaved version with the saved version on disk.

Is this possible in VSCode natively or else via an extension?

In VIM, such a possibility exists, and here are some references for that:

Can I see changes before I save my file in Vim?

VIM - Show the diff of last saved version and current unsaved version

4
  • Don't do long unsaved edit sessions
    – rioV8
    Commented Aug 30, 2021 at 10:16
  • @rioV8 I actually think that the idea of buffer and saved file that vim uses is quite useful in this context. While I am only beginning to try vim out, this seems a useful feature.
    – Tryer
    Commented Aug 30, 2021 at 12:03
  • I have been programming for years and never had the need for this feature, just like I never had the need for code formatters. You can put the code in SCM and then VSC shows you the diff of the current editor and the saved file in SCM, click on the SCM markers on the left of the editor pane
    – rioV8
    Commented Aug 30, 2021 at 14:59
  • 3
    @rioV8 - It's not just about long unsaved edit sessions - this also comes in handy when you get interrupted and have to step away. Then on returning, you may have forgotten where you left off and not recall what you had changed.
    – AnthonyK
    Commented Jan 9, 2022 at 9:01

2 Answers 2

15

On Windows and Linux, this feature appears in 1.60.0 by default as:

{
  "key": "ctrl+k d",
  "command": "workbench.files.action.compareWithSaved"
}
3
  • 1
    Thanks for sharing. Interestingly, there's no menu option for this though I keep reading on Google results that there's a compare with saved context menu of the file browser - I'm yet to find that.
    – AnthonyK
    Commented Jan 9, 2022 at 9:04
  • @AnthonyK: It's also available via the Command Palette, and a context menu: stackoverflow.com/a/73527331/3442232 Commented Aug 29, 2022 at 10:20
  • On Mac, CMD+K D (press D separately). Commented Jul 5, 2023 at 20:36
7

Available natively in multiple different ways:

  • "Files: Compare Active File with Saved" command from the standard VS Code Command Palette (Ctrl+Shift+P).
  • File context menu "Compare with Saved" within the 'OPEN EDITORS' view of the Explorer.
  • Ctrl+K D keyboard shortcut, as mentioned by Tryer.

The functionality was introduced in v1.15: https://code.visualstudio.com/updates/v1_15#_compare-dirty-file-with-version-on-disk

1
  • "File: Compare Active File with Saved" Commented May 1, 2023 at 11:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.