Change Vim file name swap / backup / undo

Is it possible to change the way Vim calls its paging / backup / undo files?

To avoid confusion, I set the parameters in my ~/.vimrc to dump these files in ~/.vim/tmp/{swap,backup,undo} ; however, since I regularly edit files in different directories with the same name, I often get many other indistinguishable files, and Vim sometimes has problems with recovery.

Ideally, I would like to use a naming scheme that is constant (for %path%to%file.undo ) for all of these auxiliary files; There is no obvious way to install it, but is it possible to do this using the Buf{Read,Write} macros?

+49
vim
Dec 02 2018-10-12T00:
source share
3 answers

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

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

The key // at the end of the directories. See This Note From :help dir :

  • For Unix and Win32, if the directory ends with two path separators "//" or "\\" , the paging file name will be created from the full path to the file with all path separators replaced by% '%' characters. This ensures that the file name in the save directory is unique.
+66
Dec 02 '10 at 4:56
source share

Here is part of my .vimrc from github .

This installs undodir (and includes it), installs backupdir and directory (used for .swp files). Note that it creates directories if they do not 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 he commented well enough to understand what was going on. If not, add a comment and I will fix it.

Ciao!

Update [07/16/2012]

I received an email from Rob Keane asking these questions about the backupdir section, which I wanted to answer everyone:

  • It looks like you are deleting the current directory and then re-adding it. what does it do?
  • What does the ^= operator do?
  • How does priority order in using folders work in Vim? (Like the last folder, the first one that it checks is added?)

The first thing to describe for different operators. These operators have different meanings for parameters other than the string list, so be warned!

  • -= removes the value from the list of strings;
  • += adds value to the list of lines;
  • ^= adds the value to the list of strings.

So backupdir has the following operations:

  • Delete the current directory from the list.
  • Add the current directory to the list (this ensures that it is checked last).
  • Remove the home directory from the list (I don't like it when stuff is saved there).
  • Prepare ~/.vim/backup/ .
  • Prepend ~/.vim-backup/ .

When Vim searches for where to save backups, it checks from the first to the last; therefore, it checks for the presence of ~/.vim-backup/ , then checks for ~/.vim/backup , then checks for the default list (with the exception of . and ~/ , which were deleted), and finally check .

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

+60
Mar 02 '12 at 5:12
source share

Create directory cancel

  $ mkdir ~/.vimundo 

Configure the .vimrc file

  set undodir=~/.vimundo 
+2
Dec 19 '10 at 19:25
source share



All Articles