HOWTO: have vim create backup and tmp directories

vimThis may only apply to those of us geeks that use vim to admin servers daily, but today I needed a way to backup, and automate the creation of backup and tmp directories to house those ever annoying ~ and .swp files from showing up in my working directory ($PWD). I didn’t want to lose them, just move them somewhere so they don’t clutter up the directory I’m working in. The solution was a function I found on the vim forums. Basically it uses directories it creates in your home directory, so you’ll have something like ~/.vim/backup and ~/.vim/tmp which is perfect; files are moved out of the way, but still backed up in a place you can rely on. I slightly reworked this, you can try it out by opening your ~/.vimrc file, and find the line:

set backup            " keep a backup file

Then after that cut/paste the following (if you don’t have the set backup line, add it first):

function InitBackupDir()
let separator = "."
let parent = $HOME .'/' . separator . 'vim/'
let backup = parent . 'backup/'
let tmp    = parent . 'tmp/'
if exists("*mkdir")
if !isdirectory(parent)
call mkdir(parent)
endif
if !isdirectory(backup)
call mkdir(backup)
endif
if !isdirectory(tmp)
call mkdir(tmp)
endif
endif
let missing_dir = 0
if isdirectory(tmp)
execute 'set backupdir=' . escape(backup, " ") . "/,."
else
let missing_dir = 1
endif
if isdirectory(backup)
execute 'set directory=' . escape(tmp, " ") . "/,."
else
let missing_dir = 1
endif
if missing_dir
echo "Warning: Unable to create backup directories: " 
. backup ." and " . tmp
echo "Try: mkdir -p " . backup
echo "and: mkdir -p " . tmp
set backupdir=.                 
set directory=.
endif
endfunction          
call InitBackupDir()




  • cas

    Excellent tip. Just recently several of us had to use vim regularly to access some remote machines we were developing on. (Not to mention the speed of quick local changes it affords.)

    The .swp’s sure do get annoying after a bit, but I’d never thought to see about moving them.

    Thanks much and, as much as it may annoy me, I couldn’t see anyway to improve the script.

    I’ll pass it around today.

  • cas

    Excellent tip. Just recently several of us had to use vim regularly to access some remote machines we were developing on. (Not to mention the speed of quick local changes it affords.)

    The .swp’s sure do get annoying after a bit, but I’d never thought to see about moving them.

    Thanks much and, as much as it may annoy me, I couldn’t see anyway to improve the script.

    I’ll pass it around today.

  • http://fak3r.com/ fak3r

    [quote comment="2648"]Thanks much and, as much as it may annoy me, I couldn’t see anyway to improve the script.[/quote]
    Thanks Charles, coming from you that’s quite the complement. Always open for any/all new ideas that you or your coworkers may have. This is why I love *nix; there’s constantly more to learn, and then share via forums, mailing lists, or posts like this. I use this space not only to disseminate what I’ve come across, but to see if others have other tips, as well as to simply document it for my future use. After all, I didn’t learn this stuff while in college for my fine arts degree! (yes, I’m still toying with the idea of getting a masters in some 10-15 years)

  • http://fak3r.com fak3r

    [quote comment="2648"]Thanks much and, as much as it may annoy me, I couldn’t see anyway to improve the script.[/quote]
    Thanks Charles, coming from you that’s quite the complement. Always open for any/all new ideas that you or your coworkers may have. This is why I love *nix; there’s constantly more to learn, and then share via forums, mailing lists, or posts like this. I use this space not only to disseminate what I’ve come across, but to see if others have other tips, as well as to simply document it for my future use. After all, I didn’t learn this stuff while in college for my fine arts degree! (yes, I’m still toying with the idea of getting a masters in some 10-15 years)

  • cas

    Me again. I tried this out today with mixed results. I’m running on an iMac running 10.4.8, with a vim version of 6.2.

    After adding the function to my .vimrc, vim complained about failing to create the needed directories. After a bit of debugging(where I added an “else” to the “if exists()” for mkdir — you might want to do that to make the function better. Heh) and digging (wherein I learned more about vim functions than I did before), I found that the Mac OS X 6.2 version of vim doesn’t have the “mkdir” function.

    So, I grabbed the latest vim from darwinports (a site I recommend to all Mac users, btw). This gave me 7.0 plus numerous patches. mkdir is present in this release, so all is good. The function works great.

    Life’s good. Thanks again.

  • cas

    Me again. I tried this out today with mixed results. I’m running on an iMac running 10.4.8, with a vim version of 6.2.

    After adding the function to my .vimrc, vim complained about failing to create the needed directories. After a bit of debugging(where I added an “else” to the “if exists()” for mkdir — you might want to do that to make the function better. Heh) and digging (wherein I learned more about vim functions than I did before), I found that the Mac OS X 6.2 version of vim doesn’t have the “mkdir” function.

    So, I grabbed the latest vim from darwinports (a site I recommend to all Mac users, btw). This gave me 7.0 plus numerous patches. mkdir is present in this release, so all is good. The function works great.

    Life’s good. Thanks again.

  • http://fak3r.com/ fak3r

    That’s funny, you think of vim as some old, stodgy, slightly updated version of the ancient (relatively speaking) vi, but it’s constantly gaining new features; thanks for the update. And yes, darwinports is fantastic (and always makes me wonder why Apple doesn’t keep it’s cli utilities on OS X up to date), plus it’s better than cygwin that I’m forced to use on my Windows “workstations” (but that’s still better than nothing).

  • http://fak3r.com fak3r

    That’s funny, you think of vim as some old, stodgy, slightly updated version of the ancient (relatively speaking) vi, but it’s constantly gaining new features; thanks for the update. And yes, darwinports is fantastic (and always makes me wonder why Apple doesn’t keep it’s cli utilities on OS X up to date), plus it’s better than cygwin that I’m forced to use on my Windows “workstations” (but that’s still better than nothing).

  • Aa

    http://valmikam.blogspot.com/2010/09/vim-auto-backup-configuration.html

    See that link for a complete copy_pastable solution.

  • Aa

    http://valmikam.blogspot.com/2010/09/vim-auto-backup-configuration.html

    See that link for a complete copy_pastable solution.

  • http://natanyellin.com Natan Yellin

    Why not just “set backupdir=~/tmp//” and “set dir=~/swap//”

Read previous post:
The Wii Sports Experiment

For anyone trying to find a fun way to get/stay in shape, this is encouraging. "Six weeks ago, I [...]

Close