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?
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.
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 .
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.
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.
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!
I got an email from Rob Kine asking these questions about the backupdir
section that I wanted to answer for everyone:
^=
operator do?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:
~/.vim/backup/
.~/.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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With