Is there a Compare feature like the Plugin for Notepad++?
- 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 9 '15 at 13:01
- 7@lasse vscode can compare two arbitrary files now. See my answer stackoverflow.com/a/45091703/1081043 – wisbucky Jul 13 '17 at 22:13
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.
- 3Yup, Ive just noticed there is the option of Selecting files to compare but only those in an open folder. – user4863890 May 9 '15 at 17:26
- 43
- 3to be fair, the question was just to know if there is a funcion, not how to trigger it :) – Armando SM Jan 19 at 6:54
- 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 at 12:19
- 12
- 5
- 5@AmrElAdawy I meant compare two buffers (for example, Untitled-1 and Untitled-2, there is no physical file) – ca9163d9 Mar 6 '17 at 18:47
- 7@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 7 '17 at 3:37
- 4In 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 6 '20 at 9:46
I have Visual Studio Code version 1.27.2 and can do this:
Compare two files
- Drag and drop the two files into Visual Studio Code
- Select both files and select Select for Compare from the context menu
- Then you see the diff
- With Alt+F5you can jump to the next diff
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:
- Open two tabs by hitting Ctrl+N twice:
- Paste your first text sample from the clipboard to the first tab and the second text sample from the clipboard to the second tab
- Select the first document Untitled-1 with Select for Compare:
- Select the second document Untitled-2 with Compare with Selected:
- Then you see the diff:
- 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? The first result is Compare Folders by MoshFeu. Might be what you are looking for. – Lernkurve Jun 30 '20 at 14:04
- 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 6 at 7:04
Here is my favorite way, which I think is a little less tedious than the "Select for Compare, then Compare With..." steps.
- Open the left side file (not editable)
F1
Compare Active File With...- Select the right side file (editable) - You can either select a recent file from the dropdown list, or click any file in the Explorer panel.
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.
- 2Do you know, if there exists a way to have both files editable within the compare window? – Sebastian Werk Feb 16 '18 at 12:51
- 2This is nice. Do you know automated way to push differences from left into right? Other than manual copy/paste? – user1055568 Apr 14 '18 at 16:58
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 command line
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.
There is plugin called Partial Diff which helps to compare text selections within a file, across different files, or to the clipboard.
- I love Partial Diff. Select some text, set it as the left, select some more, set it as the right. Voila. – Jeremy Foster Feb 5 '18 at 22:20
- Yes, I also found that Partial Diff is so easy to use and very useful. – Noby Fujioka Apr 29 '18 at 7:29
- there's a native "Compare with clipboard" option from the F1 menu – Facundo Colombier Apr 15 at 13:49
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.
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.
Right click on 1st file click "Select for compare".
Click 2nd file click "Compare with selected"
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:
- 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.
- Open or focus file A.
- Press
Alt+k
, a quick open menu will be shown with file B focused. - 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.