Using IntelliJ to diff two arbitrary files

I'm really liking the IntelliJ diff tool. I'm wondering if it is possible to use this tool to diff two arbitrary files. I.e. files NOT in an IntelliJ project. Just pick two files in the file system and diff them? What about if I want to diff two strings? In my text editor, I'd just create two new documents, paste the text in and diff them, then not save the files when done. Is this possible?

Thanks

32367 次浏览

Yes you can (of course, if we are talking about text files). But probably you will need to create some project (just for opening there files).

Then your steps will be:

1) Add to created project file#1 and file#2.

2) Open file#1 in editor

3) Right click mouse in left menu (project files browser) on file#2 and find menu-item Compare File with Editor.

enter image description here

That's all. After p.3) a comparison windows will be opened.

Found this command line:

on MacOSX: (IDEA EAP 11)

/Applications/IntelliJ\ IDEA\ 11.app/Contents/MacOS/idea diff /Users/ben75/file1.txt /Users/ben75/file2.txt

On MacOSX it can't run when IDEA (11) is open.

on Windows: (IDEA EAP 12)

"C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0\bin\idea.exe" diff C:\Users\ben75\file1.txt C:\Users\ben75\file2.txt

It runs even if IDEA is already open.

I have trouble when trying to compare files with no extensions (or extensions that don't match )

Directly from IDEA (EAP 12)

Not so easy, but it works...

  • File > Open... > select "file1.txt"
  • File > Open... > select "file2.txt"
  • Create a "Favorite list" and add both files to this favorite list. (to do this : ctrl+right-click on file name tab in the editor > add to favorite > select the favorite list just created - see screenshot)
  • Finally, open the "Favorites" view (Alt+2 or click on it on the left or your IDEA windows).
  • Select both files
  • Right click : "compare two files" (or ctrl+D)

add to favorites

Select 2 files in Project panel and press ABC0 + D.

enter image description here

IntelliJ Idea 13 has a feature labeled Compare with Clipboard. This feature compares the current open file with a version that you have copied into your clipboard. It's a great, easy way to compare and merge two files easily. It can also be used to compare two arbitrary blocks of code. Can't figure out why one block works and another does not? Use this feature.

This option is present under View > Compare with Clipboard. I assigned an easy to remember keyboard shortcut to give me quick access to this feature.

View > Compare with Clipboard

In order to compare two files that both aren't in the project, the following works for me in IntelliJ IDEA Ultimate 14.1.7 on Windows:

  1. Open the first file in the IDE (either by dragging from Windows Explorer into the window, or by File / Open).
  2. In the view menu, choose "Compare With…"
  3. In the "Select Path" dialog that appears, select the second file (either directly, or by dragging from Windows Explorer into the dialog).

It then opens up in the traditional file comparison pane. If you try to edit one of the files, it presents the usual "Are you sure you want to edit a file that's not in your project" dialog, which one can accept if that's what one is trying to do.

In IntelliJ IDEA 2018.2, you can open an empty Differences Viewer and paste any text you want to compare in its left and right panels. The new Open Blank Diff Viewer action can be found in the Find Action dialog.

From the terminal, you can use the idea launcher directly like so:

# Windows
idea.exe diff path1 path2


# MacOS/Linux
idea diff path1 path2

At least for MacOS (I'm not certain of Windows), if you've created the Command-line Launcher via "Tools -> Create Command-line Launcher..." it's as simple as the following:

# Create some files to diff
echo "This is line 1" > file1.txt
echo "This is line 2" > file2.txt


# Diff with IntelliJ
idea diff file1.txt file2.txt

Note this works with other JetBrains applications as well; for example, using WebStorm or PyCharm:

# Diff with WebStorm
webstorm diff file1.txt file2.txt
# Diff with PyCharm
charm diff file1.txt file2.txt

Reference

Since 2018.2, IntelliJ has officially supported it:

For windows: Ctrl + Shift + A, then type "Open Blank Diff Window"

enter image description here

enter image description here

For Mac: Cmd + Shift + A then type "Open Blank Diff Window"

enter image description here

enter image description here

Just select both files in project structure and hit:

ctrl+D

Mac: command+D

I have found even better way if you need to compare opened file with any arbitrary file on filesystem:

  • Be in editor of the first file, let say A.java.
  • Ctrl+Shift+A, you will see: Actions search list
  • Select second "Compare With..." option, you will get "Select path" dialog: Select path dialog
  • Select any file on you hard drive and confirm it, you will get diff window: Diff windows with an arbitrary file against originally selected one

In 2022, Help -> Find Action... -> "Open Blank Diff Window", or make a keyboard shortcut for it.

In the Open Blank Diff Window you can paste or open any file, whether it's in the project or not.