There are multiple ways to compare two files in Visual Studio Code, depending on where the files are located and your workflow.
The most common method involves using the Explorer view.
Method 1: Using the Explorer view
This method is the most straightforward for comparing any two files that are visible in the VS Code file explorer.
- Open the Explorer view by clicking the file icon in the Activity Bar on the side or by pressing
Ctrl+Shift+E. - In the Explorer, right-click the first file you want to compare and select Select for Compare.
- Right-click the second file and select Compare with Selected.
- A new diff editor will open, displaying the two files side-by-side.
Key features of the diff editor:
- Color coding: Differences are highlighted with distinct colors. A green background indicates added content, a red background shows removed content, and a blue background highlights modified lines.
- Inline and side-by-side views: You can toggle between a side-by-side view for a complete overview and an inline view that shows changes within a single column.
- Navigation: Use the arrows in the top-right of the diff editor to jump between changes.
- Live editing: You can make edits directly in the right-hand file to merge changes. The left file is read-only.
Method 2: Using the Command Palette
This approach is ideal if the two files are not in the same folder or if you need to compare an open file with another file on your disk.
- Open the file you want to use as the baseline for comparison.
- Open the Command Palette by pressing
Ctrl+Shift+P. - Type "compare" and select the command File: Compare Active File With....
- This will open a file selection menu. You can either select a recent file or browse your file system to choose the second file.
- After selecting the second file, the diff editor will open with the active file on the left and the newly selected file on the right.
Method 3: Using the command line
For those who prefer a command-line workflow, you can initiate a file comparison directly from your terminal, which is useful for scripting or quick, one-off comparisons.
- Open your system's terminal or command prompt.
- Run the
codecommand with the--diffflag, followed by the paths to the two files you want to compare.- Syntax:
code --diff <file_1> <file_2> - Example:
code --diff main.js new-version.js
- Syntax:
- VS Code will launch and display the diff view for the two specified files.
Advanced comparisons and additional tips
Compare with Clipboard
You can compare the currently active file with the contents of your clipboard. This is useful for quickly comparing a local file against code copied from a web page or another application.
- Copy the content you want to compare.
- Open the target file in VS Code.
- Open the Command Palette (
Ctrl+Shift+P). - Type "compare" and select File: Compare Active File with Clipboard.
Git integration and timeline view
For projects under version control, VS Code’s built-in Git integration offers powerful comparison features.
- Select a file in the Explorer view.
- Open the Timeline view at the bottom of the Explorer panel.
- This view shows the file's history from Git. Clicking on any commit entry will show a diff of the file against its previous version, allowing you to track changes over time.
Comparing unsaved "Untitled" files
Even new, unsaved files can be compared in VS Code.
- Open two new, untitled files (
Ctrl+Ntwice) and paste the content you want to compare into each. - In the Explorer view, click the "three dots" (
...) menu and ensure Open Editors is selected. This will display your two untitled files. - Select both files (
Ctrl+ClickorShift+Click), right-click, and choose Compare Selected.
Customizing the diff editor
You can adjust the diff editor's behavior to suit your preferences by modifying settings.
- Open the Settings editor (
Ctrl+,). - Search for "diff editor" to find relevant options.
- Ignore Trim Whitespace: This setting, also accessible with a button in the diff view's toolbar, prevents changes in trailing whitespace from appearing as a difference.
- Synchronize Views: You can lock the scrolling of the two files together by enabling the "Synchronize Views" setting.