Comparing the contents of two files in Sublime Text
Asked Answered
G

11

503

I have two cloned repositories of two very similar open-source projects, which I have been working on in different instances in Sublime Text 2 to arrive at my desired result. Code from both of these projects was used. I have been using Git as version control for my project, but have not included the original projects. Thus, I would like to be able to quickly compare the contents of two files of the original project and compare the differences between them and my project.

I was hoping that Sublime Text 2 would have a "Compare File" feature, but I can't seem to find anything related to it in the settings or online. A third-party ST2 package to accomplish this task would also work well. Is such a task possible to do within the ST2 text editor?

Ganges answered 16/9, 2014 at 16:31 Comment(0)
A
860

You can actually compare files natively right in Sublime Text.

  1. Navigate to the folder containing them through Open Folder... or in a project
  2. Select the two files (ie, by holding Ctrl on Windows or on macOS) you want to compare in the sidebar
  3. Right click and select the Diff files... option.

Alidia answered 3/5, 2015 at 10:3 Comment(19)
@Derek: Not able to select two files at a time in sidebar in sublime using the Cmd or Ctrl key in mac.Ferrosilicon
@Derek: using select able to select but not able to find the diff file option on right click, only got the close option on right click.Ferrosilicon
@MohamedHussain There are really two different sidebars and you need to be in the right one for this to work. If you go to File>Open Folder..., Select the folder your files are in, then select View>Side Bar>Show Side Bar, you will get the one you need. You get the other by selecting View>Side Bar>Show Open Files, but this one will not let you use the Diff Files... option.Benzoic
@Derek朕會功夫 I just asked the OP to make yours the accepted answer. I now use Sublime's diff for most of my needs, except when I need some extended options, in which case I use an external tool like P4Merge, DiffMerge, or Diffuse (Win, OSX, and Linux, respectively).Barrator
@TJ. just asked for it to be switched (I wrote the currently-accepted answer).Barrator
@jmc no, that's not the case. Both parent folders need to be in the sidebar, but you can diff any two files you want, regardless of where they live.Barrator
This unfortunately does not seem to work anymore in sublime build 3103 on mac. As soon as I right click the second file gets deselected,Diluvium
@Diluvium Works for me in Build 3114 on OSX. They probably have fixed it.Skipbomb
Not working for me in Build 3144 on OSX. I can select two files in the file interface, but when I Ctrl-click only one becomes selected and the diff option isn't there.Phipps
Not work for me too in 3114 - windows. There is no diff option when I right-click on files.Lem
Note to those with the "deselected on Ctrl/Cmd+click" issue: those files must be closed, I mean, none of them is open as a tab (but it doesn't matter if one is open as a preview tab, tab label in italics). It could be a bug but that's the way it works :)Keeling
This cuts off the bottoms of my files, does anyone know how to fix this issue?Knife
I used Compare With and that works well, and is actually a better solution.Knife
In Sublime3 (cant speak for lower versions) this only works for UTF-8 files.Lemuroid
In my version of Sublime, build 2221 on OSX, I have to use the Project menu to "Add Folder to Project...". The header at the top of the sidebar will be "Folders". In that sidebar view, I could select 2 files and diff.Abbasid
The files must be closed first to be able to select multiplesLemuroid
This fails for me with the error message Diff only works with UTF-8 files.Hammer
If the files are becoming deselected then you need to close the open files sidebar by going to View > Sidebar > Hide Open FilesAdieu
Comparing latest code and older code is little bit tricky but your solution works, Thumbs Up :)Hadleigh
C
282

Compare Side-By-Side looks like the most convenient to me though it's not the most popular:

UPD: I need to add that this plugin can freeze ST while comparing big files. It is certainly not the best decision if you are going to compare large texts.

Catalan answered 16/11, 2015 at 12:24 Comment(10)
this is the easiest and most convenient solution IMO. Works well in ST3Bernardinebernardo
Works well and can compare files from different locations which I haven't managed to do with the native compare.Grille
This also works with copy pasted content in new unsaved tabs, which is handy in a pinch.Janeyjangle
Compare Side-By-Side just helped me.Pincushion
seems like a more obvious question to the answerGershon
This one looks beautiful. Hopefully its not as unstable on large files as the answerer stated. Better than the other ones.Toluene
love this one! does anyone know how to extract a separate list of the coloured lines?Runge
This is broken for me in ST4. They don't appear side-by-side, and no text is highlighted.Player
Broken and no issue tracker to report. I think the project is dead.Kimberliekimberlin
It upsets me that this isn't the preferred answer. Telling people "open the open dialogue, find the two files..." how often do we diff 2 files when neither of them are open? Dany's answer is much better than Derek's.Superordinate
B
68

There are a number of diff plugins available via Package Control. I've used Sublimerge Pro, which worked well enough, but it's a commercial product (with an unlimited trial period) and closed-source, so you can't tweak it if you want to change something, or just look at its internals. FileDiffs is quite popular, judging by the number of installs, so you might want to try that one out.

Barrator answered 16/9, 2014 at 16:53 Comment(5)
I am now using FileDiffs and it works like a charm. Thanks, answer accepted.Ganges
Well I cannot figure it out how to use FileDiffs. Can you shortly describe how I can compare two files with it.Sirenasirenic
@Ganges I wouldn't normally do this, but would you mind changing the accepted answer to Derek 朕會功夫's answer below? Neither of us really need the points, but I didn't know there was a built-in diff at the time. I now use it all of the time when in Sublime. The only time I don't is when I need the extended features of an external tool.Barrator
Which of those plugins is able to detect the movement of whole blocks of text? And not just tell some lines are missing here and appear there but also show with arrows how they moved?Wafer
Thanks to this answer, I installed FileDiffs and added commad to use TortoiseMerge.exe for comparison. It was a huge improvement for my every day use of sublime code comparison. Thanks!Exert
J
62

UPDATE
(Given the upvotes, I feel there is a need for a complete step-by-step explanation...)

  1. In the Menu bar click on File->Open Folder...
  2. Select a folder (the actual folder does not really matter, this step is just to make the FOLDERS sidebar available)
  3. If there is no Side Bar shown yet, make it appear via View -> Side Bar -> Show Side Bar
  4. Use this FOLDERS-titled Side Bar to navigate to the first file you want to compare.
  5. Select it (click on it), hold down ctrl and select the second file.
  6. Having two files selected, right click on one of the two and select Diff Files...

There should be a new Tab now showing the comparison.


Original short answer:
Note that:

The "Diff files" only appears with the "folders" sidebar (to open a folder: File->Open Folder) , not with "open files" sidebar.

Jeanninejeans answered 13/10, 2015 at 9:37 Comment(6)
Is there a way to select those different lines? One appears in red, and the other in greenDragoon
What do you mean by select? Highlight them and e.g. copy them? Or extracting them? Thanks for the clarification!Jeanninejeans
Since the missing lines are prefixed with '- ' I selected one and then matched all of them, selected the full line and copied. A built in way would just be more fluff to forget.Dragoon
I think it is worth explaining that both files can't be hard opened. If you doubled clicked on both so they are both opened, then you can't highlight both files in the sidebar using the ctrl button. You have to make sure one of them is closed before you can select both files. I found this out because I already had both files open and wanted to use diff files...Unwholesome
@Unwholesome I can, whether its due to a plugin or not (sidebar enhancements maybe). I have both files open, then select both again and can diff them which opens in a third file. Maybe - the two selected files need to be in a project folder, not in the "Open Files" section at the top of the sidebar.Catamenia
@NaveenDA I adapted my answer to be able to remove the broken link. Thanks for the hint!Jeanninejeans
F
38

UPDATE JAN 2018 - especially for Sublime/Mac

(This is very similar to Marty F's reply, but addresses some issues from previous responses, combines several different suggestions and discusses the critical distinction that gave me problems at first.)

I'm using Sublime Text 3 (build 3143) on Mac and have been trying for about 30 minutes to find this File Compare feature. I had used it before on Sublime/Mac without any problems, but this time, it was trickier. But, I finally figured it out.

  1. The file format does not need to be UTF-8. I have successfully compared files that are UTF-8, ISO-8559-1, and Windows-1252.

  2. There is no File > Open Folders on Sublime/Mac. Many instructions above start with "Select File > Open Folders," but that doesn't exist on Sublime/Mac.

  3. File compare works on a Project basis. If you want to compare two files, they must be saved to disk and part of the current project.

  4. Ways to open a project

  • If Sublime/Mac is not running or if it's running but no windows are open, drag a folder onto the Sublime app.
  • If Sublime/Mac is running, select "File > Open", navigate to the desired folder, don't select a file or folder and click "Open".
  1. Add a folder to a project. If the files you want to compare are not part of the same hierarchy, first open the folder containing one of the files. Then, select "Project > Add Folder to Project", navigate to the folder you want and click "Open". You will now see two root-level folders in your sidebar.

  2. The Sidebar must be visible. You can either "View > Side Bar > Show Side Bar" or use the shortcut, Command-K, Command-B.

  3. Files must be closed (ie, saved) to compare. Single-clicking a file in the Side Bar does not open the file, but it does display it. You can tell if a file is open if it's listed in the "Open Files" section at the top of the Side Bar. Double-clicking a file or making a modification to a file will automatically change a file's status to "Open". In this case, be sure to close it before trying to compare.

  4. Select files from the folder hierarchy. Standard Mac shorcut here, (single) click the first file, then Command-click the second file. When you select the first file, you'll see its contents, but it's not open. Then, when you Command-click the second file, you'll see its contents, but again, neither are open. You'll notice only one tab in the editing panel.

  5. Control-click is not the same as right-click. This was the one that got me. I use my trackpad and often resort to Control-click as a right-click or secondary-click. This does not work for me. However, since I configured my trackpad in System Preferences to use the bottom-right corner of my trackpad as a right-click, that worked, displaying the contextual menu, with "Delete", "Reveal in Finder", and.... "Diff Files..."

Voilà!

Fango answered 21/1, 2018 at 10:15 Comment(4)
Is it possible to do a folder compare instead of file compare?Toilet
jeez ! you just saved the mac users !Nonresistant
After all these years, how did I miss "Control-click is not the same as right-click"? Thanks for that. And, FWIW, at least on a MacBook Pro running MacOS Mojave, the default for secondary-click is simply a two-finger tap on the trackpad.Raffia
normally Control-Click is the same as secondary-click. Just not in Sublime Text, apparently. not sure why.Fango
T
12

All other answers either state that files can only be compared if they are opened in a project in the folders sidebar, or rely on a third-party plugin.

Actually it is possible to directly compare any two tabs by selecting both tabs (ctrl+click), then right click on either tab and choose Diff Selected Tabs...

(tested in ST build 4143)

Tetrabranchiate answered 26/5, 2023 at 11:28 Comment(1)
This! This worked for me just now on Mac OS.Dockyard
W
11

View - Layout and View - Groups will do in latest Sublime 3

eg:

Shift+Alt+2 --> creates 2 columns

Ctrl+2 --> move selected file to column 2

This is for side by side comparison. For actual diff, there is the diff function other already mentioned. Unfortunately, I can't find a way to make columns scroll at the same time, which would be a nice feature.

Weinstock answered 15/10, 2018 at 12:49 Comment(1)
And to close a group you can use Ctrl + K Ctrl+downFrazzled
D
10

UPDATE OCTOBER 2017 I never knew this feature existed in Sublime Text, but the interface appears to have changed slightly from the previous answer - at least on OS X. Here are the detailed steps I followed:

  1. In the Menu Bar click File -> Open...
  2. Navigate to the FOLDER that contains the files to be compared and with the FOLDER selected, click the Open button, this makes the FOLDERS sidebar appear
  3. In the FOLDERS sidebar, click on the first file to be compared
  4. Hold the Ctrl on Windows or ⌘ on OS X, and click the second file
  5. With both files selected, right click on one and select Diff Files...

This opens a new tab showing the comparison. The first file in red, the second in green.

Desinence answered 11/10, 2017 at 13:28 Comment(0)
A
4

The Diff Option only appears if the files are in a folder that is part of a Project.

Than you can actually compare files natively right in Sublime Text.

Navigate to the folder containing them through Open Folder... or in a project Select the two files (ie, by holding Ctrl on Windows or ⌘ on macOS) you want to compare in the sidebar Right click and select the Diff files... option.

Anastaciaanastas answered 14/6, 2019 at 22:13 Comment(0)
L
3

No one is talking about Linux but all above answers will work. Just use Ctrl to select more than one file. If you are looking to compare side by side, Meld is lovely.

Larrup answered 31/3, 2018 at 17:15 Comment(0)
A
2

There's a BeyondCompare plugin as well. It opens the 2 files in a BeyondCompare window. Pretty convenient to open files from the sublime window.

You will need BC3 installation present in the system. After installing the plugin, you will have to provide the path to the installation.

Example:

{
    //Define a custom path to beyond compare
    "beyond_compare_path": "G:/Softwares/Beyond Compare 3/BCompare.exe"
}
Antonio answered 2/2, 2019 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.