sponsor Vim development Vim logo Vim Book Ad

intermediate Tip #1393: Automatically create/set tmp or backup directories

 tip karma   Rating 156/41, Viewed by 1804 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   November 22, 2006 8:51      complexity:   intermediate
author:   Clayton      as of Vim:   5.7

I needed a way to automate creating the backup/tmp directories.  It is especially annoying when I am in a new environment and I have to manually create the directories.  I also prefer to use _vim rather then .vim on a Windows system so it checks for that.  Hopefully I've discovered all the bugs.

function InitBackupDir()

    if has('win32') || has('win32unix') "windows/cygwin
        let separator = "_"
    else
        let separator = "."
    endif

    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()

If you found this tip useful, you may also want to check out vimtip #20

 rate this tip  Life Changing Helpful Unfulfilling 

<< Shell script to use grep with gvim | Make tar of all files in vim buffer >>

Additional Notes

[email protected], November 23, 2006 8:17
Of corse using just one backup/swapfile directory will mean that the following command won't work any more:

vimdiff old/foe.txt new/foe.txt

especialy if you need to merge/change both ways.

that's why in vimscript#1537 I use local directorys. But the "set directory=" might be a worthy addition to my script.

Martin.
Anonymous, November 28, 2006 6:31
actually, that command does work and it won't clobber the file, in :h directory
"        - For Unix and Win32, if a directory ends in two path separators, 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."

example swap files in ~/.vim/tmp:
%home%user%tmp2%test1.txt.swp  %home%user%tmp%test1.txt.swp

maybe you can clarify if I am misunderstanding what you're saying.  in a test I did, even without having the extra slash it will have two different swap files called test.txt.swp and test.txt.swo.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo