6

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

Tryer
  • 3,580
  • 1
  • 26
  • 49
  • Don't do long unsaved edit sessions – rioV8 Aug 30 '21 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 Aug 30 '21 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 Aug 30 '21 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 Jan 09 '22 at 09:01

3 Answers3

10

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

{
  "key": "ctrl+k d",
  "command": "workbench.files.action.compareWithSaved"
}
Tryer
  • 3,580
  • 1
  • 26
  • 49
  • 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 Jan 09 '22 at 09:04
  • @AnthonyK: It's also available via the Command Palette, and a context menu: https://stackoverflow.com/a/73527331/3442232 – Andrew D. Bond Aug 29 '22 at 10:20
  • On Mac, CMD+K D (press D separately). – CodeSammich Jul 05 '23 at 20:36
5

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

Andrew D. Bond
  • 902
  • 1
  • 11
  • 11
1

That will now also work with VSCode for the Web (https://vscode.dev/), with VSCode 1.78 (Ap. 2023).

The "Edit Sessions" (introduced with VSCode 1.69, June 2022) enable you to bring your uncommitted changes along when you continue working on a repository wherever VS Code is available, whether that is VS Code for the Web, desktop VS Code, or across different machines.

But, as illustrated in issue 157113, you could not show the compare view in between changes with the current head change, like this one:

https://user-images.githubusercontent.com/50770619/182910631-b671726e-bffd-4efa-9fb9-3a4985643728.png

This is fixed with PR 179477, available in VScode Insiders.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250