1. Use Vim's comparison mode to open two files:
Vim-D file1 file2
Or
Vimdiff file1 file2
2. If the file file1 has been opened, open another file file2 for comparison:
: Vert diffsplit file2
If the vert command is not used, diffsplit will split up and down two windows.
3. If you have opened two files, file1 and file2, in Split mode, and want to compare the differences between the two files.
Enter the following commands in two windows:
: Diffthis
4. If the content of a window is changed and VIM does not automatically update the diff check, run the following command to update the window:
: Diffupdate
5. Locate the differences:
[C jump to the previous difference
] C jump to the next difference
6. jump between Windows:
CTRL-w jump to the next window
CTRL-w h jump to the Left window
CTRL-w l jump to the right window
CTRL-w j jump to the window below
CTRL-w k jump to the window above
7. Merge documents:
DP applies the current document content of the difference point to another document (diff put)
Do copies the content of another document of the difference to the current document (diff get)
8. Expand and view the context
When comparing and merging files, you often need to combine context to determine the final action to be taken. By default, vimdiff displays the texts of 6 rows in the upper and lower directions for your reference. Other text lines are automatically folded. If you want to change the default number of contextual lines to three, you can set it as follows:
: Set diffopt = context: 3
You can use a simple collapse command to temporarily expand the same collapsed text line:
Zo (folding open, Z looks like folding paper)
Then, you can use the following command to fold again:
ZC (folding close)