Win7中的vimdiff不能用?

一、vimdiff启动方式:

  1. 鼠标选中两个要进行比较的文件,右键->Diff with Vim
  2. (管理员/一般用户)打开cmd,cd到文件目录下,运行命令gvimdiff file1 file2
  3. (管理员/一般用户)用gvim打开file1, 在以命令方式运行diffs file2

二、上面的所有方式都会出现错误信息:

Drawing

回车继续后,两个文件打开了,可是两个文件有差异的地方却没有高亮出来:
Drawing

三、解决方法

在网页http://vimdoc.sourceforge.net/htmldoc/diff.html
中可以查看到两个错误信息E810和E97的说明:


Vim will do a test if the diff output looks alright. If it doesn’t, you will
get an error message. Possible causes:
- The “diff” program cannot be executed.
- The “diff” program doesn’t produce normal “ed” style diffs (see above).
- The ‘shell’ and associated options are not set correctly. Try if filtering
works with a command like “:!sort”.
- You are using ‘diffexpr’ and it doesn’t work.
If it’s not clear what the problem is set the ‘verbose’ option to one or more
to see more messages.

The self-installing Vim for MS-Windows includes a diff program. If you don’t
have it you might want to download a diff.exe. For example from
http://gnuwin32.sourceforge.net/packages/diffutils.htm.

逐一排查这些问题

  1. 是否是系统缺少diff?cmd确实不能识别这个命令,于是从http://sourceforge.net/projects/gnuwin32/?source=typ_redirect 下载安装,并将路径添加到Path中。
    第一步完成后问题依然存在。
  2. 运行在vim中运行:se查看对默认设置做了些什么更改,可以发现diffexpr被定义成了Mydiff,这是在_vimrc中设置的,打开_vimrc可以发现如下设置代码:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    set diffexpr=MyDiff()
    function MyDiff()
    let opt = '-a --binary '
    if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
    if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
    let arg1 = v:fname_in
    if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
    let arg2 = v:fname_new
    if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
    let arg3 = v:fname_out
    if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
    let eq = ''
    if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
    let cmd = '""' . $VIMRUNTIME . '\diff"'
    let eq = '"'
    else
    let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
    else
    let cmd = $VIMRUNTIME . '\diff'
    endif
    silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
    endfunction

注释掉之后,vimdiff就可以正常工作了!
Drawing

热评文章