Question

My setup is to use vim (specifically MacVim in graphical mode) to edit source files and then I do (mercurial) repository diffs using vimdiff (in graphical mode again). Sometimes, when I do a diff, vim gives me the error:

Swap file "~/nn/src/imm/model/injector_node/.values_table.py.swp" already exists!

How do I fix this? Can I command vim to stop making swp files, or the vimdiff to ignore them, or write its swp files to a different place?

Was it helpful?

Solution

Yes, (maybe), and yes.

To turn off swap files:

set nobackup
set nowritebackup
set noswapfile

To change location of swap files (my preferred choice):

" Set the directory of the swap file
" The // indicates that the swap name should be globally unique
set directory=~/.vim/tmp//,/tmp

Here I set it up in a tmp directory under .vim. See :h 'directory' to find out what each part of the option represents.

As for getting Vimdiff to ignore the swap files, I'm not quite sure. But the error is probably occurring because when you're checking the diffs with Mercurial, you have the same files already open in MacVim. If you just close the files before performing the diff, then you should be fine.

OTHER TIPS

I would suggest a different approach: instead of using mercurial command-line interface to view differences between files, use one of vim plugins that adds VCS support, in this case you will neither need to close anything nor do something with swap file.

For example, with my aurum you may use <leader>aD (mapped to :AuVimDiff) to view differences between currently opened buffer and its version in repository and then <Leader>X to switch back to normal view (if you have not edited anything it will even restore opened/closed folds). If you do let g:aurum_vimdiffusewin=1 and use vertical or horizontal (depending on 'diffopt') equally sized splits then my script will reuse one of adjacent windows.

With <leader>agD you can view all changes to all files in repository in a multiple tabs in a vimdiff split (excluding added and removed files): it is mapped to :AuVimDiff full. As far as I know this functionality is almost unique: there is some script for mercurial which does the same job, but it is not integrated with other VCS plugins. Again, <leader>X to close all tabs. Buffers which were already open won’t get closed, others will.

Another plugin which I may recommend is VCSCommand.

I know this question is more than a year old now, but might I recommend lawrencium, a plugin integrating mercurial into vim, inspired by the fantastic fugitive plugin for git. They make what you're doing (and a lot of other CVS things) much easier with a few simple commands. They didn't pay me to say this; the plugins are just really good!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top