We all have our favourite diff tools for comparing files and it’s probably not Eclipse itself, unless you’re comparing version controlled files.
But sometimes you’ll want to compare two files from within Eclipse (whether they are in your workspace or external files) either because it’s quicker or you don’t have your own diff tool to hand.
Eclipse offers two ways of comparing files with each other. One of them is appropriately called Compare with Each Other. Although this is often the suggested way to compare files, it’s not easy to use and doesn’t work with external files. This post will focus on another Eclipse feature called Compare with Other Resource that makes things go a bit smoother and handles external files but it will also cover using Compare with Each Other.
Compare with Other Resource – The Easy Way
Although Compare with Other Resource is easier to use, it’s slightly hidden and requires a once-off setup of a keyboard shortcut to work. The reason is that the feature hasn’t been officially released. It does, however, come standard with any Eclipse installation.
To assign a keyboard shortcut to the feature, go to Window > Preferences > General > Keys, look for the command Compare with Other Resource and assign it to a keyboard shortcut of your liking. See How to manage keyboard shortcuts in Eclipse for more details on configuring shortcuts.
Tip: Alternatively, you could press Ctrl+3 (for Quick Access) and search for Compare with Other Resource. This way you don’t have to configure a shortcut but you’ll have to search for the command every time in Quick Access.
Once the shortcut is assigned, you need to select the source file from the Package Explorer, Project Explorer or Navigator. The easiest way to do this from an open editor is to assign a shortcut to instantly show the file in the Package Explorer or, if it’s not open, to open it quickly using the keyboard.
Now press the keyboard shortcut you assigned to Compare with Other Resource and the following (non-modal) dialog should appear:
Troubleshooting: If the dialog doesn’t appear, it’s probably because you haven’t selected a file in either the Package Explorer or another view that allows file selection. Make sure a file is selected and make sure you haven’t selected the class in the Package Explorer rather than the actual Java file.
Now you can select the second file in the Package Explorer and drag and drop it onto the dialog. Click OK and the comparison editor should display, comparing the two selected files.
Tip: See the post on using shortcuts for common SVN commands for tips on how to navigate the comparison editor with the keyboard.
Comparing External Files
Using the Compare with Other Resource dialog, you can compare a workspace file with an external file or even compare two external files with each other.
Comparing a workspace file with an external file is easy: Once you’ve opened the dialog from a selected file, instead of dragging and dropping another workspace file, tick External File in the Right pane and then Browse to a file on your filesystem.
To compare two external files, you have to start the dialog again from a selected workspace file (choose an arbitrary file). Once open, select External File in the Left pane and Browse to your file then do the same for the Right pane.
Compare with Each Other – The Hard Way
The other way to compare two files is to use Compare with Each Other. Unlike the previous feature, Compare with Each Other doesn’t use a dialog but relies on selecting the two files in the Package Explorer. However, it also requires that both files be in your workspace.
To use Compare with Each Other, open the Package Explorer (or similar view) and select the source file. Then hold down Ctrl while you navigate to the other file in the same view. Then select the other file and release Ctrl. Both files should now be selected.
Right-click to bring up the context menu and choose Compare With > Each Other. Eclipse will open a comparison editor, comparing the files that you selected.
The reasons why I don’t use this method as much is that you can’t easily compare external files. If you have external files then you need to import (or drag and drop) them into Eclipse’s workspace. Also, when you have many projects (eg. Eclipse RCP/OSGi development) then selecting two files in different projects becomes a nightmare.
I’d recommend using Compare with Other Resource instead because of the flexibility and because it’s easier to use. However, it’s not officially released so you may hit a bump now and then but overall it’s a lot better than Compare with Each Other.
- Quick ways to navigate Eclipse editors using the keyboard
- The fastest ways to open editors in Eclipse using the keyboard
- Eclipse shortcuts for common SVN commands
- How to manage keyboard shortcuts in Eclipse and why you should
- Instantly show a class/file in the Package/Project Explorer in Eclipse