Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Vim swap/backup/undo file name

Tags:

vim

swapfile

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?

like image 344
Robbie Avatar asked Dec 02 '10 04:12

Robbie


People also ask

Can I delete SWP files?

Use the swap -d command to remove swap space. The swap file name is removed from the list so that it is no longer available for swapping. The file itself is not deleted. Edit the /etc/vfstab file and delete the entry for the swap file.

Where are Vim swap files saved?

While editing a file, you can see which swap file is being used by entering :sw . The location of this file is set with directory option. The default value is .,~/tmp,/var/tmp,/tmp . This means Vim will try to save this file in the order of . , and then ~/tmp , and then /var/tmp , and finally /tmp .

What is SWP file in Vim?

An SWP file is a swap file created by the Vi text editor or one of its variants, such as Vim (Vi iMproved) and gVim. It stores the recovery version of a file being edited in the program. SWP files also serve as lock files, so no other Vi editing session can concurrently write to the currently-open file.


2 Answers

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.
like image 129
John Kugelman Avatar answered Sep 19 '22 22:09

John Kugelman


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.

like image 34
docwhat Avatar answered Sep 17 '22 22:09

docwhat