tags:

views:

220

answers:

2

I don't like how vim clutters up my folders with backup files, so I have the following line in my .vimrc file:

set backupdir=~/.vim_backup

However, sometimes this folder doesn't exist because of new machines where I am copying my user files over.

How can I create this folder automatically, if it doesn't exist, from within .vimrc? Or is there some better way to deal with this situation?

+6  A: 

You can put this into your .vimrc:

silent !mkdir ~/.vim_backup > /dev/null 2>&1

This will attempt to create the ~/.vim_backup each time you start vim. If it already exists, mkdir will signal an error, but you'll never see it.

ABentSpoon
+1: Good answer. Assuming Linux, you could also do `!mkdir -p ~/.vim_backup` as the `-p` option will stop mkdir reporting an error in the first place (as well as making parent directories if required).
Al
+1  A: 

I wanted a solution to this that works on both Linux and on Windows (with Cygwin binaries on the path.) - my Vim config is shared between the two.

One problem is that 'mkdir' is, I think, built-in to the cmd shell on windows, so it can't be overridden by putting the Cygwin mkdir executable earlier in the path.

I solved this by invoking a new bash shell to perform the action: From there, 'mkdir' always means whatever mkdir executable is on the path:

!bash -c "mkdir -p ~/.vim-tmp"

However, this has the problem that. on Windows at least, it pops up a new window for the cmd shell that '!' invokes. Don't want that every time I start vim. Then I discovered the vim function 'system', which invokes a new cmd shell (the icon appears in the Windows taskbar) but the window is minimised. Hence:

call system("bash -c \"mkdir -p ~/.vim-tmp\"")

Ugly but works.

Tartley