Vim diff two files

Source: Internet
Author: User
1. The directory tree is the same
The source code 6.0 and 6.2 usually have the same directory tree structure. For example, all files exist.
Sys/i386/i386/machdep. c, and all of them have the init386 function.
If we want to compare the differences between the init386 functions, we can directly compare the differences between the two files.
The scenario we want to implement is as follows:
First, browse the init386 function in the source code of 6.2 (or 6.0). Then, we suddenly want to see what is different from 6.0.
, D (comma and letter d). Then vim compares the two functions in the vertical window diff mode.

Assume that the source code of 6.2 and 6.0 is located in the directory/usr/src/6.2/,/usr/src/6.0/, respectively/
Add the following code to your ~ /. Vimrc:
Function Diff ()
Let s: file_name = expand ("%: p ")
Let s: slash_count = match (s: file_name, "/", 3)
Let s: slash_count = match (s: file_name, "/", s: slash_count + 1)
Let s: slash_count = match (s: file_name, "/", s: slash_count + 1)
Let s: file_name = strpart (s: file_name, s: slash_count + 1)
Let s: file_name = "/usr/src/6.0/". s: file_name
Execute ": vertical diffsplit". s: file_name
Endfunction
Nmap, d: call Diff () <CR>
Map <C-h> <C-W> h
Map <C-l> <C-W> l

Let's explain it one by one:
Function Diff () "declares a function. The first letter of the function name must be larger.
Let s: file_name = expand ("%: p") "to get the current file path (for example,/usr/src/6.2/sys/i386/i386/machdep. c)
Let s: slash_count = match (s: file_name, "/", 3) "starts matching 3rd characters"/", returns the location where 2nd"/"is located.
Let s: slash_count = match (s: file_name, "/", s: slash_count + 1) "returns the location where 3rd "/"
Let s: slash_count = match (S: file_name, "/", S: slash_count + 1) "returns the location where 4th "/"
Let s: file_name = strpart (S: file_name, S: slash_count + 1) "split path, get sys/i386/i386/machdep. c
Let s: file_name = "/usr/src/6.0/". S: file_name "to generate a new path/usr/src/6.0/sys/i386/i386/machdep. c
Execute ": vertical diffsplit". S: file_name "open a new path file in the vertical window diff.
Endfunction
NMAP, D: Call diff () <CR> "ing, d into calling diff Function
Map <c-h> <C-W> H "Maps Ctrl + H to a window on the left
Map <c-l> <C-W> L "Maps Ctrl + L to switch to the right window

2. different situations in the directory tree
If the file structure of the two projects is significantly different, you cannot use the 1st methods.
We still want to use the left and right windows to browse the code, but we want to switch between windows, the corresponding tags can also be switched.

The procedure is as follows (we still use the source code of 6.2 and 6.0 as an example ):
1. Create a tags directory
Mkdir/tmp/tags
CD/tmp/tags
2 cs1_rb-s/usr/src/6.2/
3 mv cscope. out 62.out
4 cs1_rb-s/usr/src/6.0/
5 mv cscope. out 60.out
Add the following code to your ~ /. Vimrc:
Map <C-H> <C-W> h: cs add/tmp/tags/62.out< cr>: cs kill 0 <cr>
Map <C-L> <C-W> l: cs add/tmp/tags/60.out< cr>: cs kill 1 <cr>
6. Use
Vim/usr/src/6.2/sys/i386/i386/machdep. c
: Cs add/tmp/tags/62.out
: Vsplit/usr/src/6.0/sys/i386/i386/machdep. c

In this way, you can use Ctrl + h/l to switch the window and tags at the same time.

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.