Compare files in Visual Studio Code

Table of contents

1. Compare two files

1.1The files in VS code are roughly divided into two categories:

 1.2 How to compare two files in VS code?

 2. Side-by-side difference mode: a difference mode in VS code

3. Inline difference mode: another difference mode in VS code

4. VS code ignores spaces added or deleted at the beginning or end of the line.

5. Scenario of using VS code to compare two files

reference:


1. Compare two files

1.1The files in VS code are roughly divided into two categories:

  1. Files in workspace:
    1. Click File-->Open Folder-->Select a folder, and then the files in this folder will be displayed in the workspace;
  2. File in editor:
    1. Click any file in the workspace, and the file will appear in "OPEN EDITORS";
    2. Click File-->New File, a new and unsaved file will appear in OPEN EDITORS;
    3. Instead of opening the folder through VS code, open a file directly. This file will appear under the OPEN EDITORS column;

 1.2 How to compare two files in VS code?

  1. First, left-click to select a file, and the file will turn blue to indicate that it is selected;
  2. Hold down the Shift key, then left-click to select another file, the selected file will turn blue;
  3. Release the Shift key;
  4. Place the mouse on the selected file, right click and select "compare selected"
Step2 The selected file turns blue
Step4: Click Compare Selected

 2. Side-by-side difference mode: a difference mode in VS code

After following the above method, the following difference viewer will be opened. The default difference mode in VScode is to display the old and new codes side by side, which is the following form.

 It is worth explaining that:

  • When comparing two files, the first selected file will be displayed on the left side of the above difference viewer, and the second selected file will be displayed on the right side of the above difference viewer. (At the same time, we know what files correspond to the left and right sides of the difference viewer through the file name of the difference viewer. For example, as shown in the above figure, the file name of the difference viewer is old2.py< -- >new2. py, which also means that the file on the left side of the diff viewer is old2.py, and the file on the right side of the diff viewer is new2.py);
  • Differences between two files are usually highlighted using red and green, but the colors may vary depending on the theme used.
  • Lines of code that exist only in the left page are displayed with a red background, and the corresponding right is displayed with a gray diagonal line;
  • Lines of code that exist only on the right side of the page are shown with a green background and their corresponding left side with a gray diagonal line'
  • Both the right and left sides have content, but there are some small differences. At this time, the left side uses a light red background, the right side uses a light green background, and the difference part uses dark red or dark green.

3. Inline difference mode: another difference mode in VS code

The default difference mode in VS code is to display the old and new codes side by side, that is, the old and new codes are displayed in two parallel pages in one window.

If the code line is very long, or there is not enough horizontal display screen space, you need to switch to the inline diff mode, that is, the two code files to be compared are displayed in the same window in the same page. This mode works by first displaying all rows that have been deleted at the current position, followed by all rows that have been inserted. It might take some getting used to this mode, but it does reduce the screen real estate required.

inline diff mode

To switch between side-by-side and inline diff modes, just open the Actions menu and select Inline View

4. VS code ignores spaces added or deleted at the beginning or end of the line.

By default, VS Code ignores whitespace added or removed at the beginning or end of a line, they have no meaning in most programming languages ​​and just add visual noise to the diff. There are some notable exceptions to inappropriateness. For example, Python uses the indentation of a line to determine which control flow structure the current line belongs to, and Markdown uses two trailing spaces to indicate a newline.

To toggle whether VS Code should ignore leading/ending whitespace, click this icon:

5. Scenario of using VS code to compare two files

  • Compare two code files to see if there are differences; if there are differences, sometimes, some differences need to be removed.
  • Compare two tex files to see if there are any differences. For example, if there are two versions of tex files in two papers, since there is no marking which ones are newly marked and which ones are marked later, you can compare them through VS code at this time;

Remark:

During use, I usually open the template file first, and then open the file to be modified;

reference:

Comparing Files in Visual Studio Code

Guess you like

Origin blog.csdn.net/u011375991/article/details/132791523