REW

How Do I Compare Two Files In Visual Studio Code?

Published Aug 29, 2025 4 min read
On this page

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.

  1. Open the Explorer view by clicking the file icon in the Activity Bar on the side or by pressing Ctrl+Shift+E.
  2. In the Explorer, right-click the first file you want to compare and select Select for Compare.
  3. Right-click the second file and select Compare with Selected.
  4. 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.

  1. Open the file you want to use as the baseline for comparison.
  2. Open the Command Palette by pressing Ctrl+Shift+P.
  3. Type "compare" and select the command File: Compare Active File With....
  4. This will open a file selection menu. You can either select a recent file or browse your file system to choose the second file.
  5. 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.

  1. Open your system's terminal or command prompt.
  2. Run the code command with the --diff flag, 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
  3. 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.

  1. Copy the content you want to compare.
  2. Open the target file in VS Code.
  3. Open the Command Palette (Ctrl+Shift+P).
  4. 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.

  1. Select a file in the Explorer view.
  2. Open the Timeline view at the bottom of the Explorer panel.
  3. 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.

  1. Open two new, untitled files (Ctrl+N twice) and paste the content you want to compare into each.
  2. In the Explorer view, click the "three dots" (...) menu and ensure Open Editors is selected. This will display your two untitled files.
  3. Select both files (Ctrl+Click or Shift+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.

  1. Open the Settings editor (Ctrl+,).
  2. Search for "diff editor" to find relevant options.
  3. 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.
  4. Synchronize Views: You can lock the scrolling of the two files together by enabling the "Synchronize Views" setting.
Enjoyed this article? Share it with a friend.