789

Is there a Compare feature like the Plugin for Notepad++?

Avatar
  • 14,622
  • 9
  • 119
  • 198
  • 1
    Yes, there is, but it is integrated with the version control tools, you can't really ask it to compare two arbitrary files, but you can ask version control to compare two revisions of the same file, or your current file in the working folder with the last checked in / committed revision. – Lasse V. Karlsen May 09 '15 at 13:01
  • 13
    @lasse vscode can compare two arbitrary files now. See my answer https://stackoverflow.com/a/45091703/1081043 – wisbucky Jul 13 '17 at 22:13

16 Answers16

944

right click on first file and select

enter image description here

then right click on second file and select

enter image description here

Screencast:

how to compare files in vscode

Avatar
  • 14,622
  • 9
  • 119
  • 198
Rakka Rage
  • 15,941
  • 8
  • 33
  • 45
  • its good enough it has this option for the files in the open folder at least. –  May 09 '15 at 17:27
  • 60
    how to compare two unsaved newly created files? – ca9163d9 Dec 28 '16 at 23:05
  • 9
    @dc7a9163d9 the same way, right click and select for compare. – Amr Eladawy Mar 06 '17 at 06:49
  • 15
    @AmrElAdawy I meant compare two buffers (for example, Untitled-1 and Untitled-2, there is no physical file) – ca9163d9 Mar 06 '17 at 18:47
  • 12
    @dc7a9163d9, Yes, That still also the same way as if the two untitleds are saved. Flow the same steps. I tried it and it worked. – Amr Eladawy Mar 07 '17 at 03:37
  • @ca9163d9 See [my answer](https://stackoverflow.com/a/52622305/33311) on how to compare two unsaved newly created files. – Lernkurve Aug 26 '19 at 19:37
  • Is there a way (or plugin) to compare all the files in 2 different folders (and their sub-folders)? – Tohid Jun 30 '20 at 12:41
  • 65
    In the Explorer view you might not find the "Open Editors" section at first. Just click on the three dots for the settings of the Explorer view and you can see that you can tick this option. – DarioMangoni Nov 06 '20 at 09:46
  • I'd prefer to have the same for jupyter lab notebook files. Is that also possible? – Hestaron Jul 07 '21 at 12:32
  • That's great! But, I don't see options to merge the differences by copying from left to right or vise versa. In Eclipse, the compare out of the box, allows to merge the code from/let left/right without any plugin. Is there such an option in vscode? – tarekahf Sep 27 '21 at 17:00
  • how to compare files that are downloaded via some FTP and are on temp location? – Umair Ayub Feb 22 '22 at 14:43
408

You can compare files from the explorer either from the working files section or the folder section. You can also trigger the global compare action from the command palette.

  1. Open a folder with the files you need to compare,
  2. Select two using SHIFT
  3. Right click "Compare Selected" enter image description here
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
Benjamin Pasero
  • 113,622
  • 14
  • 75
  • 54
  • 3
    Yup, Ive just noticed there is the option of Selecting files to compare but only those in an open folder. –  May 09 '15 at 17:26
  • 48
    you don't specify how to do either. – java-addict301 Aug 31 '20 at 19:39
  • 5
    to be fair, the question was just to know if there is a funcion, not how to trigger it :) – Armando SM Jan 19 '21 at 06:54
  • 5
    The "must be inside the opened folder" restriction seemed to be very annoying to me, as I usually open a folder and then also work with external files in the meantime. But there is an easy solution provided by vs code itself: in the folder explorer tab, click on the 3 dots, then toggle "open editors" (disabled by default, at least on the Ubuntu version): this way you can pick any opened file for the side-by-side comparison, not just the ones within the folder. Maybe this is an obvious solution, but I took me a while to figure it out: hope this can help someone. – Jetboy Jun 11 '21 at 12:19
  • 14
    You can run `code -d ` these days. – Mark Allen Jun 25 '21 at 21:19
  • 3
    @Jetboy solved it: "open editors" is disabled by default, on the Ubuntu AND Windows 10 versions; you have to click on the spread symbol (...) up and left. – Marcelo Scofano Diniz Mar 20 '22 at 21:18
283

I have Visual Studio Code version 1.27.2 and can do this:

Compare two files

  1. Drag and drop the two files into Visual Studio Code enter image description here
  2. Select both files and select Select for Compare from the context menu enter image description here
  3. Then you see the diff enter image description here
  4. With Alt+F5 you can jump to the next diff enter image description here

Compare two in-memory documents or tabs

Sometimes, you don't have two files but want to copy text from somewhere and do a quick diff without having to save the contents to files first. Then you can do this:

  1. Open two tabs by hitting Ctrl+N twice: enter image description here
  2. Paste your first text sample from the clipboard to the first tab and the second text sample from the clipboard to the second tab
  3. Select the first document Untitled-1 with Select for Compare: enter image description here
  4. Select the second document Untitled-2 with Compare with Selected: enter image description here
  5. Then you see the diff: enter image description here
Community
  • 1
  • 1
Lernkurve
  • 20,203
  • 28
  • 86
  • 118
  • Thanks, Compare with Default White theme is better than Dark theme. – Shaiju T Jan 29 '20 at 11:15
  • Is there a way (or plugin) to compare all the files in 2 different folders (and their sub-folders)? – Tohid Jun 30 '20 at 12:41
  • @Tohid, have you googled it? [vscode compare two folders](https://www.google.com/search?client=firefox-b-d&q=vscode+compare+two+folders)? The first result is [Compare Folders](https://marketplace.visualstudio.com/items?itemName=moshfeu.compare-folders) by MoshFeu. Might be what you are looking for. – Lernkurve Jun 30 '20 at 14:04
  • 4
    This answer is so much better because it does not assume that the files to compare are in the same directory tree. Thanks – abyshukla Apr 06 '21 at 07:04
  • exactly what i have been needing for a long time! – TravRob Nov 04 '21 at 17:39
  • 3
    This answer is amazing because it doesn't even assume that the two files are actual files, i.e., *saved on disk*. This was precisely what I was looking for, for ages. Imagine you want to compare the outputs of two commands on your screen, say two separate `pip list`, in Windows, without all the bash goodies. It's either this or... nothing. Thank you #Lernkurve! – Ricardo Nov 09 '21 at 18:16
  • Note that in order to compare untitled files, you need to go under the "Open editors" tab which may not appear by default. Right clicking on the "Explorer" title, or clicking on "..." allows you to change that. – Axel Samyn Sep 27 '22 at 15:37
131

Here is my favorite way, which I think is a little less tedious than the "Select for Compare, then Compare With..." steps.

  1. Open the starting file

  2. Open the Command Palette (F1 or Ctrl + Shift + P)

  3. Type Compare Active File and select Compare Active File With...

    Compare Active File With

  4. Select the new file to compare with. You can either select a recent file from the dropdown list, or click any file in the Explorer panel.

    File Picker

  5. View the Result!

    Diff Viewer

Note: This works with any arbitrary files, even ones that are not in the project dir. You can even just create 2 new Untitled files and copy/paste text in there too.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
wisbucky
  • 33,218
  • 10
  • 150
  • 101
71

Another option is using command line:

code -d left.txt right.txt

Note: You may need to add code to your path first. See: How to call VS Code Editor from terminal / command line

Visual Studio Code compare file left.txt with right.txt

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
wisbucky
  • 33,218
  • 10
  • 150
  • 101
54

In your terminal type:

code --diff file1.txt file2.txt

A tab will open up in VS Code showing the differences in the two files.

David Refoua
  • 3,476
  • 3
  • 31
  • 55
Erika Dsouza
  • 1,015
  • 12
  • 8
22

There is plugin called Partial Diff which helps to compare text selections within a file, across different files, or to the clipboard.

SANN3
  • 9,459
  • 6
  • 61
  • 97
7

other option is ctrl-shift + p and type file compare vsc capture

4

If you want to compare file in your project/directory with an external file (which is by the way the most common way I used to compare files) you can easily drag and drop the external file into the editor's tab and just use the command: "Compare Active File With..." on one of them selecting the other one in the newly popped up choice window. That seems to be the fastest way.

Jacek Dziurdzikowski
  • 2,015
  • 2
  • 13
  • 20
4

Recently I created a VSCode extension for even faster way of file comparison. It's called Fast Compare and you can download it here.

https://marketplace.visualstudio.com/items?itemName=DavidKol.fastcompare

Gif animation of functionality

4

Quickly open - and close - 2 new empty editors for a compare

As of Insiders Build v1.75, there is a new command to quickly open 2 new (i.e., empty) editors for a diff/compare. Into each of these you can paste whatever you want to compare between them. The command is:

File: Compare New Untitled Text Files
workbench.files.action.compareNewUntitledTextFiles

There is no default keybinding. Demo:

compare two new files demo

Mark
  • 143,421
  • 24
  • 428
  • 436
3

I found a flow which is fastest for me, by first associating a keyboard shortcut Alt+k to "Compare Active File With..." (#a). (Similar to wisbucky's answer but further improved and more step-wise.)

Then, to compare two files:

  1. Open or focus file B (will be editable in compare view by default). E.g. by drag-drop from File Explorer to VS Code's center.
  2. Open or focus file A.
  3. Press Alt+k, a quick open menu will be shown with file B focused.
  4. Press Enter.

Result: file A on left and file B on right. (Tested on VS Code 1.27.1)

Remarks

#a - to do so, press Ctrl-k Ctrl-s to show Keyboard Shortcuts, type compare on the top search box, and double click the "Keybinding" column for "Compare Active File With...", press Alt+k then Enter to assign it.

Johnny Wong
  • 945
  • 9
  • 16
  • Here is another way to find available compare commands. Press "CMD + Shift + P" (on Mac) or "Ctrl + Shift + P" then type "compare" and you can pick which ever option you want. – Heitara Mar 29 '21 at 09:36
2

Here's a link to marketplace for extension. Extension "compareit" helps to compare two files wich you can choose from your current project and other directory on your computer or clipboard.

Margaret
  • 114
  • 3
2

Right click on 1st file click "Select for compare".

Click 2nd file click "Compare with selected"

Ali Karaca
  • 3,365
  • 1
  • 35
  • 41
1

If we are talking about about "features" per se, there are lots of features that can "replicate" the funcionalities that we are familiar with in Jetbrains.

Like so, there is one that I have found very interesting:

enter image description here

Once you installed it, you double-click and you have all sorts of functionalities to compare, for instance "with clipboard" without having to rely on "copy paste" that part of code in a new file.

enter image description here

Then when comparing with clipboard you should have sth like this:

enter image description here

Hope this helps

Nick Cuevas
  • 1,474
  • 15
  • 10
0

In my case, I was trying to use compare option for files in open editor of VS code, however open editor wasn't visible. In such a case, to access open editor, click on 3 dots next to explorer and check the open editors option.

Shaggy
  • 484
  • 1
  • 6
  • 16