Use the Python and vim plug-ins to allow Vim to support multi-Folder comparison,

Source: Internet
Author: User

Use the Python and vim plug-ins to allow Vim to support multi-Folder comparison,

On Vim's official website, there is a plug-in DirDiff that supports 2 folder comparisons. Link: http://www.vim.org/scripts/script.php? Script_id = 102. However, only two folders are supported. I aligned the research and improved IT TO MAKE IT support comparison of files in Multiple folders.

The basic principle of the DirDiff plug-in is to generate a list of all files in the folders to be compared. Each line of the file corresponds to a file and Its folder. when the folder comparison mode is enabled, the list file will be loaded. When a row is selected, the file path will be parsed, and the corresponding files in different folders will be opened in diff mode for comparison.

For convenience, I will use Python to generate a list of files in this specific format, then start vim and start folder plug-in comparison.

The Python script is as follows:

1 #! /Usr/bin/env python
2 # coding: UTF-8
3
4 import OS, sys, shutil, subprocess
5 from OS import path
6 # Put the files in the directory list in dirlist in a file dictionary filesdict,
7 # The dictionary key is the file name under a directory in dirlist (excluding this directory name ),
8 # value indicates all directories with corresponding keys in dirlist.
9 def getfilesfromdirs (dirlist ):
10 filesdict = {}
11 for onedir in dirlist:
12 onedir = path. realpath (onedir) # canonicalized directory name
13 # process all objects in the target path (including subdirectories)
14 for root, dirs, files in OS. walk (onedir ):
15 # record the directory to which each file name belongs. files with the same name will belong to multiple different directories.
16 # in this way, an inverted index is generated for the file name to the directory where the file is located.
17 for onefile in files:
18 # If the key value of the file name has not been created, create
19 onefile = OS. path. join (root, onefile) [len (onedir):]
20 if onefile not in filesdict:
21 filesdict [onefile] = []
22 filesdict [onefile] + = [onedir]
23 return filesdict
24
25 def vimDirDiffN (diffdirs ):
26 diffbuffer = './vimdirdiffntmpbuffer'
27 filesdict = getfilesfromdirs (diffdirs)
28 fileslines = []
29 for key, values in filesdict. iteritems ():
30 fileslines + = ['file: '+ key +' @ '+', '. join (values)]
31 write2fileline = str (len (diffdirs ))
32 fileslines = '\ n'. join (fileslines)
33 dirsymbol = 'A'
34 for onedir in diffdirs:
35 dirline = '<' + dirsymbol + '>' + '=' + onedir
36 write2fileline + = '\ n' + dirline
37 fileslines = fileslines. replace (onedir, '<' + dirsymbol + '> ')
38 dirsymbol = chr (ord (dirsymbol) + 1)
39 write2fileline + = '\ n \ n' + fileslines
40 fp = open (diffbuffer, 'w ')
41 fp. write (write2fileline)
42 fp. close ()
43 subprocess. Popen ('gvim-c ": dirdiffn' + path. join (OS. getcwd (), diffbuffer) + '"', shell = True)
44
45 if _ name _ = "_ main __":
46 if len (sys. argv)> 2:
47 diffdirs = sys. argv [1:]
48 for index in range (0, len (diffdirs )):
49 diffdirs [index] = path. realpath (diffdirs [index])
50 vimDirDiffN (diffdirs)
51 else:
52 print "Interactive Mode ."
53 diffdirs = raw_input ('input the dirs you want to diff (in List ["A", "B"]): ')
54 if diffdirs = '':
55 diffdirs = ['f:/PYOutput/old', 'f:/PYOutput/new', 'f:/PYOutput/third_merge ']
56 else:
57 diffdirs = eval (diffdirs)
58 for index in range (0, len (diffdirs )):
59 diffdirs [index] = path. realpath (diffdirs [index])
60 vimDirDiffN (diffdirs)
61 raw_input ('Press to exit .')

This script is used to read the list of folders that the user inputs need to compare, scan the file to generate a Comparison List, start vim, and execute the modified multi-Folder comparison plug-in DirDiffN.

There are many modified DirDiffN plug-ins. You can download the. DirDiffN plug-in from the attachment.

Put DirDiffN in the Vim plug-in directory.

Effect preview:



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.