Windows – Can’t run Vimdiff (7.4) on Windows 7

gvimvimvimdiffwindows 7

I recently installed the 7.4 version of gVim on a Windows 7 machine. Trying to diff two files from the context-sensitive menu on Windows Explorer, I keep getting the error messages:

E810: Cannot read or write temp files
E97: Cannot create diffs

The two selected files are opened in the two-pane gVim view, and the editor seems to be in diff mode, but the actual differences are not highlighted.

The %TEMP% and %TMP% environment variables are populated with a valid directory name.

I get the same response when opening the files in a regular gVim session and typing :diffthis in the command prompt.

This used to work properly with Vim 7.3, but it looked like something was changed in v7.4.

How can I diff the two files?

Best Answer

This issue can be caused by the default _vimrc file created by the installer on Windows. If you're still using that default file, or if you copied it at some point, then check the function you've assigned to the diffexpr option. One of the patches between Vim 7.3 and 7.4 introduced new default quoting rules for the cmd.exe shell on Windows. This patch broke the workaround in the MyDiff() function designed to fix the same issue solved by the patch.

The MyDiff() function was fixed by version 7.4.103 by fixing the installer. Here is the MyDiff() function which the latest installer will create for you if you just want to copy it to your _vimrc:

 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
   if $VIMRUNTIME =~ ' '
     if &sh =~ '\<cmd'
       if empty(&shellxquote)
         let l:shxq_sav = ''
         set shellxquote&
       let cmd = '"' . $VIMRUNTIME . '\diff"'
       let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
     let cmd = $VIMRUNTIME . '\diff'
   silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
   if exists('l:shxq_sav')
     let &shellxquote=l:shxq_sav

You can see your full version in Vim using the :version or :intro commands, or at the splash screen at startup.

Unfortunately if you want an official installer, you will either need to wait until 8.0, or install a nightly build. Nevertheless, you can install Vim from other places or build your own Vim.

Duplicated on Stack Overflow (unusually, on-topic on both sites), if this answer is updated so should the other.