Search code examples
vim

How can I create a folder if it doesn't exist, from .vimrc?


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?


Solution

  • 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.