Tip: Vimdiff Use

Source: Internet
Author: User
Tags diff

Tip: Vimdiff Use

While the various Ides are large, the traditional command-line tool has a lot of room to live with its short, handy features, which describes a text comparison and merging gadget: Vimdiff. Hope to be able to develop on the unix/linux system of friends to help.

source program files (usually plain text files) The Compare and merge tools have always been an important part of the software development process. There are many powerful special comparison and merging tools on the market, such as Beyondcompare; many Ides or software configuration management systems such as Eclipse, Rational ClearCase, provide built-in capabilities to support file comparisons and consolidations.

When working remotely on the Unix/linux platform, it is probably the simplest and ubiquitous command-line tool, such as diff. Unfortunately, the function of diff is limited, it is not very convenient to use. As a command-line comparison tool, we still want to have a simple and straightforward interface that allows us to get a clear view of the comparison results, and we also want to be able to quickly locate the differences between the comparisons, hoping to easily merge the files .... The diff pattern provided by vim, often referred to as Vimdiff, is one that satisfies all of these needs and even provides more powerful tools. In recent work, because of the need to do a lot of file comparison and merging work, so the use of Vimdiff a simple summary. Let's first look at the basic use of Vimdiff.

Start method

First, make sure that the diff command in the system is available. Vim's diff mode is dependent on the diff command. The basic usage of Vimdiff is:

# Vimdiff  file_left  file_right

Or

# vim-d  file_left  file_right

Figure one is the screen of the execution result of the Vimdiff command.

From we can see a clear comparison result. The screen is split vertically, with the left and right sides showing the two files being compared. The same rows in the two files are folded up so that the user can focus on the differences between the two files. The background color of a row that exists only in one file is set to blue, and the corresponding position in another file is shown in green. Two files are present, but the lines that contain the differences appear as pink backgrounds, and the text that causes the differences is highlighted with a red background.

In addition to using this method to start the Vim diff mode, we can also use the split Window command to start the diff mode:

# Vim File_left

Then type in the Vim's ex mode (also known as "colon" mode):

: Vertical Diffsplit File_right

can also achieve the same effect. If you want to swap the position of two windows, or if you want to change the way the window is split, you can use the following command:

1. Ctrl-w K (move the current window to the top)
2.ctrl-w H (move the current window to the far left)
3.ctrl-w J (move the current window to the bottom)
4.ctrl-w L (move the current window to the far right)

Of these, 1 and 32 will change the window to a horizontal split mode.

Cursor movement

Next try moving the cursor between the lines, you can see the left and right side of the screen scrolling is synchronized. This is because the "scrollbind" option is set to the result that vim will try to ensure the alignment of the files on both sides. If you do not want this feature, you can set:

: Set Noscrollbind

You can use shortcut keys to quickly move between different points. Jump to the next difference point:

]c

The reverse jump is:

[C

If you precede the command with a number, you can skip one or more of the difference points to achieve a further leap. For example, if you enter "2]c" at the line at the first difference point, you will go over the next difference point and jump to the third difference point.

File merge

One of the ultimate goals of file comparisons is to merge to eliminate differences. If you want to copy the contents of the current file in one diff point to another file, you can use the command

DP (diff "put")

If you want to copy the contents of another file to the current line, you can use the command

Do (diff "get", the reason is not DG, because the DG is already occupied by another command)

If you want to modify a row manually, you can use the usual vim operation. If you want to jump back and forth between two files, you can use the following sequence of commands:

Ctrl-w, W

After modifying one or two files, Vimdiff will attempt to automatically re-compare the files to reflect the comparison results in real time. However, there will be cases where processing fails, and this time it is necessary to manually refresh the results of the comparison:

:d iffupdate

If you want to undo the changes, you can use the Vim editor in peace, directly

<esc>, u

Note, however, that you must move the cursor to the file window where you want to undo the modification.

Operation of two files simultaneously

After the compare and merge is over, you can use the following command to manipulate two files simultaneously. For example, exit at the same time:

: QA (Quit all)

If you want to save all files:

: WA (Write All)

or a merge command for both, save all the files, and then exit:

: WQA (Write, then quit all)

If you do not want to save the results of any operation when exiting:

: qa! (Force-to-quit all)

Context expansion and viewing

When comparing and merging files, it is often necessary to combine the context to determine the final action to take. Vimdiff default is the difference between the 6 lines of text will be displayed for reference. The other lines of the same text are automatically collapsed. If you want to modify the default number of context rows, you can set this:

: Set Diffopt=context:3

You can use the simple collapse command to temporarily expand the same line of text that is collapsed:

Zo (folding open, which uses the z-letter because it looks more like folded paper)

You can then use the following command to re-collapse:

ZC (folding close)

is a vimdiff screen that sets the context to 3 lines, and expands some of the same text:

Tip: Vimdiff Use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.