Change Vim swap/backup/undo file name

VimSwapfile

Vim Problem Overview


Is it possible to change the way Vim names its swap/backup/undo files?

To avoid clutter, I've set options in my ~/.vimrc to dump these files in ~/.vim/tmp/{swap,backup,undo}; however, as I routinely edit files in different directories with the same name, I often end up with lots of otherwise indistinguishable files and Vim sometimes has trouble recovering.

Ideally, I'd like to use the naming scheme that the persistent undo has (%path%to%file.undo) for all these auxiliary files; there's no obvious way to set it, but can it be done with Buf{Read,Write} macros?

Vim Solutions


Solution 1 - Vim

I have this in my .vimrc and it names the swap files with full path names and percent signs just as you describe:

" Store swap files in fixed location, not current directory.
set dir=~/.vimswap//,/var/tmp//,/tmp//,.

The key is the // at the end of the directories. See this note from :help dir:

> - For Unix and Win32, if a directory ends in two path separators "//" or "\\", the swap file name will be built from the complete path to the file with all path separators substituted to percent '%' signs. This will ensure file name uniqueness in the preserve directory.

Solution 2 - Vim

Here's part of my .vimrc from github.

This sets the undodir (and turns it on), sets the backupdir, and directory (used for .swp files). Note that it creates the directories if they don't already exist.

" Save your backup files to a less annoying place than the current directory.
" If you have .vim-backup in the current directory, it'll use that.
" Otherwise it saves it to ~/.vim/backup or .
if isdirectory($HOME . '/.vim/backup') == 0
  :silent !mkdir -p ~/.vim/backup >/dev/null 2>&1
endif
set backupdir-=.
set backupdir+=.
set backupdir-=~/
set backupdir^=~/.vim/backup/
set backupdir^=./.vim-backup/
set backup

" Save your swap files to a less annoying place than the current directory.
" If you have .vim-swap in the current directory, it'll use that.
" Otherwise it saves it to ~/.vim/swap, ~/tmp or .
if isdirectory($HOME . '/.vim/swap') == 0
  :silent !mkdir -p ~/.vim/swap >/dev/null 2>&1
endif
set directory=./.vim-swap//
set directory+=~/.vim/swap//
set directory+=~/tmp//
set directory+=.

" viminfo stores the the state of your previous editing session
set viminfo+=n~/.vim/viminfo

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

Hopefully, it's commented well enough to understand what's going on. If not, add a comment and I'll fix it.

Ciao!

Update [07/16/2012]

I got an email from Rob Kine asking these questions about the backupdir section that I wanted to answer for everyone:

  1. It looks like you are removing the current directory, and then re-adding it. what does that do?
  2. What does the ^= operator do?
  3. How does the order of precedence in the use of folders checked work in Vim? (Like is the last folder added the first one it checks for?)

The first thing is to describe the different operators. These operators have different meanings for non-string-list options, so be warned!

  • -= removes the value from a string list;
  • += appends the value to a string list;
  • ^= prepends the value to a string list.

So the backupdir has the following operations applied:

  1. Remove the current directory from the list.
  2. Append the current directory to the list (this ensures it is the last thing checked).
  3. Remove the home directory from the list (I don't like stuff being saved there).
  4. Prepend ~/.vim/backup/.
  5. Prepend ~/.vim-backup/.

When Vim looks for where to save the backups, it checks from first to last; so it'll check for ~/.vim-backup/, then check for ~/.vim/backup, then check the default list (except for . and ~/ which were removed), and finally check .

You can get help for all these in Vim by using (for example) :help ^= or :help backupdir.

Solution 3 - Vim

Create the directory undo

 $ mkdir ~/.vimundo

Set up your .vimrc file

 set undodir=~/.vimundo
 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobbieView Question on Stackoverflow
Solution 1 - VimJohn KugelmanView Answer on Stackoverflow
Solution 2 - VimdocwhatView Answer on Stackoverflow
Solution 3 - VimjuqueView Answer on Stackoverflow