14

By default, vim leaves swap files in my project directory, and on crashes it leaves backup files.

Can I prevent this? Or do I need to update all my projects' .VCSignore file?

3 Answers 3

16

Backup files

By default, Vim doesn't leave backup files behind. They are merely transient files that exist while Vim is saving the buffer to disk. As described in :help backup-table this behavior can be controlled by the 'backup' and 'writebackup' options:

'backup' 'writebackup'  action  ~
   off       off    no backup made
   off       on     backup current file, deleted afterwards (default)
   on        off    delete old backup, backup current file
   on        on     delete old backup, backup current file

Swap files

As for swap files, it can be useful to collect them in a dedicated directory. This can be done with the 'directory' option. For example

:set directory^=$HOME/.vim/tmp//

will add $HOME/.vim/tmp to the head of the list of directories that Vim will try to use when creating a swap file. The trailing // makes Vim encode the entire path name to the file so there aren't collisions with similarly named files from different directories.

Caveat: This effectively disables the ability for Vim to notice if multiple users are trying to edit the same file since the swap files are stored in different locations.

The snippet below (adapted from tpope's vimrc) will also disable the swap file for a buffer when it isn't modified, that way if Vim/your computer crashes when you have a bunch of files open, the swap files left behind will only be for modified files.

autocmd CursorHold,BufWritePost,BufReadPost,BufLeave *
  \ if isdirectory(expand("<amatch>:h")) | let &swapfile = &modified | endif

Caveat: Since the swap file only exists when the buffer is modified, this has a similar effect as setting 'directory'. If the buffer is unmodified, then another Vim can start editing it without notice. If the second Vim saves before the first one makes more changes, then they won't be noticed until the user tries to save or something triggers Vim to check if the file is modified.

5

I use my .vimrc on multiple machines, including shared servers. I don't necessarily want people being able to see the files that I'm working on and I like keeping my home directory relatively clean. So this is my solution which will work on Linux and Mac OS X just fine.

" Sets central temp file location, to prevent local default behavior.
if isdirectory($HOME . '/.vim/.tmp') == 0
  :silent !mkdir -m 700 -p ~/.vim/.tmp > /dev/null 2>&1
endif

set backupdir=~/.vim/.tmp ",~/.local/tmp/vim,/var/tmp,/tmp,
set directory=~/.vim/.tmp ",~/.local/tmp/vim,/var/tmp,/tmp,

Note that this will create a .tmp directory under ~/.vim if it doesn't already exist. This would be the new home of your swap files. For undo files, I also have the following in my .vimrc (which is pretty similar):

if exists("+undofile")
  " undofile -- This allows you to use undos after exiting and 
  "             restarting. NOTE: only present in 7.3+
  "             :help undo-persistence
  if isdirectory( $HOME . '/.vim/.undo' ) == 0
    :silent !mkdir -m 700 -p ~/.vim/.undo > /dev/null 2>&1
  endif
  set undodir=~/.vim/.undo
  set undofile
endif
1

This is what I do in my vimrc:

fun! MkdirIfNeeded(dir, flags, permissions)
    if !isdirectory(a:dir)
        call mkdir(a:dir, a:flags, a:permissions)
    endif
endfun

" Set (& create if needed) a temp directory to keep backup & swap files
if has('win32')
    let whoami = substitute(system("whoami /LOGONID"), '\n', '', '')
    let tmpdir = 'C:/tmp/vim_' . whoami
else
    let whoami = substitute(system("whoami"), '\n', '', '')
    let tmpdir = '/var/tmp/vim_' . whoami
endif
call MkdirIfNeeded(tmpdir, 'p', 0700)

let &backupdir = tmpdir
let &dir = tmpdir
let &undodir = tmpdir

It will create /var/tmp/vim_<username>, and use this to store all vim-related files, keeping your project directory clean.

Using whoami makes sure you can use the vimrc globally (ie. for the user & root) while still using separate directories; if you don't need this, you could replace it with a hard coded directory for faster startup. You could also use ~/.vim/ directory, but I like using /var/tmp because it's usually a separate filesystem.

I use /var/tmp because /tmp is often cleared on reboot. You probably don't want this directory world-writable, since people could then place files there to do potentially harmful things when you open a file.

4
  • 1
    /var/tmp is always world writable—it's a system temp directory. (Well, you can change it to not be, but that's one of those things where Unix-like systems are happy to let you keep the pieces after you break it). If you have another user on the system, he/she could create /var/tmp/vim_«your_username» with permissions of his/her choice. And then fill it with symlinks to your important files, which will probably be destroyed by vim tempfiles.
    – derobert
    Feb 3, 2015 at 17:51
  • @derobert Only if it doesn't exist. If you use this system-wide and havea lot of users, you probably want to add the creation of this directory to adduser or whatever you're using to make users. In any case, most people use Vim on trusted machines (but your comment is a good one, as such). Feb 3, 2015 at 17:56
  • Correct, only if it doesn't exist. Which, being in a system temporary directory, happens unpredictably based on when those tempfiles are cleaned up. Not sure why you don't just put it in $HOME somewhere.
    – derobert
    Feb 3, 2015 at 17:59
  • @derobert Well, /var/ is a different filesystem. The theory is, that it will prevent a lot of writes on /, /usr, or /home, leading to less fragmentation & risk of file corruption in case of a crash... This is also why mailboxes and stuff are on /var by default, and not in the homedir... Feb 3, 2015 at 18:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.