Vim comparison text

Source: Internet
Author: User

SourceProgramFiles (usually plain text files) Comparison and merging tools have always been an important part of the software development process. Many powerful dedicated comparison and merge tools on the market, such as beyondcompare. Many ide or software configuration management systems, such as Eclipse, rational clearcase provides built-in functions to support file comparison and merging.

When working remotely on Unix/Linux platforms, I am afraid that the simplest and ubiquitous is the command line tool, such as diff. Unfortunately, diff has limited functions and is not very convenient to use. As a command line comparison tool, we still hope to have a simple and clear interface that allows us to see the comparison results at a glance. We also hope to quickly locate the differences among multiple comparisons, it is hoped that files can be easily merged ....... The diff mode provided by VIM, usually called vimdiff, is such a powerful tool that can meet all these needs and even provide more powerful tools. In recent work, we have made a simple summary of the use of vimdiff because we need to compare and merge a lot of files. Let's take a look at the basic use of vimdiff.

Start Method

First, ensure that the diff command in the system is available. The diff mode of VIM depends on the diff command. The basic usage of vimdiff is:

 

# Vimdiff file_left file_right

 

Or

 

# Vim-D file_left file_right

 

Figure 1 shows the execution result of the vimdiff command.


 

We can see a clear comparison result. The screen is vertically split, and the two files to be compared are displayed on both sides of the screen. The same row in the two files is folded consecutively so that the user can focus on the differences between the two files. Only the background color of the row in a file is set to blue, and the corresponding position in the other file is displayed as green. The two files exist, but the lines that contain the difference are displayed as pink backgrounds. The text that causes the difference is highlighted in red background.

In addition to using this method to start the diff mode of VIM, we can also use the split window command to start the diff mode:

 

# Vim file_left

 

Then input:

 

: Vertical diffsplit file_right

 

The same effect can be achieved. If you want to switch the positions of two windows, or you want to change the window separation method, 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 leftmost)
3. CTRL-w j (Move the current window to the bottom)
4. CTRL-w l (Move the current window to the rightmost)

The Operations 1 and 3 change the window to the horizontal split mode.

Back to Top

Move cursor

Next, move the cursor between lines to see that the screen scrolling between the left and right sides is synchronized. This is because the "scrollbind" option is set. Vim tries its best to ensure file alignment on both sides. If you do not want this feature, you can set it:

 

: Set noscrollbind

 

You can use shortcuts to quickly move between different points. Jump to the next difference:

 

] C

 

The reverse jump is:

 

[C

 

If you add a number before the command, you can skip one or more differences to jump farther. For example, if "2] C" is entered in the row at the first variance point, the next variance point is crossed and the third variance point is jumped.

Back to Top

File Merging

One of the final purposes of file comparison is to merge to eliminate the differences. If you want to copy the content of the current file from one differential point to another, you can run the following command:

 

DP (diff "put ")

 

To copy the content of another file to the current line, run the following command:

 

Do (diff "get", because DG is occupied by another command)

 

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

 

CTRL-W, W

 

After modifying one or two files, vimdiff tries to automatically recompare the files to reflect the comparison results in real time. However, there will also be situations where the processing fails. In this case, you need to manually refresh the comparison results:

 

: Diffupdate

 

If you want to cancel the modification, you can directly

 

<ESC>, u

 

However, be sure to move the cursor to the file window where you want to undo the modification.

Back to Top

Operate two files simultaneously

After the comparison and merge come to an end, you can use the following command to operate the two files simultaneously. For example, exit simultaneously:

 

: QA (quit all)

 

If you want to save all files:

 

: Wa (write all)

 

Or the merge command of the two, save all the files, and then exit:

 

: Wqa (write, then quit all)

 

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

 

: QA! (Force to quit all)

 

Back to Top

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 modify the default number of contextual lines, 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, the reason why it uses the Z letter is because it looks like a folded paper)

 

Then, you can use the following command to fold again:

 

ZC (folding close)

 

Is to set the context to 3 rows and expand the vimdiff screen with some of the same text:


 

Back to Top

Conclusion

Vimdiff is the best choice when you cannot use graphical comparison tools or when you need to quickly compare and merge a small number of files.

 

References

      • Vim homepage: http://www.vim.org

      • VIM: http://man.chinaunix.net/newsoft/vi/doc/help.html
Related Article

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.