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 ~/.v
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.
Create the directory undo
$ mkdir ~/.vimundo
Set up your .vimrc file
set undodir=~/.vimundo
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
.