Skip to content

Instantly share code, notes, and snippets.

@majutsushi
Created April 4, 2011 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majutsushi/901860 to your computer and use it in GitHub Desktop.
Save majutsushi/901860 to your computer and use it in GitHub Desktop.
Automatically create dir to write file to if it doesn't exist
" Automatically create dir to write file to if it doesn't exist
function! AutoMkDir()
let required_dir = expand("<afile>:p:h")
if !isdirectory(required_dir)
if confirm("Directory '" . required_dir . "' doesn't exist.", "&Abort\n&Create it") != 2
bdelete
return
endif
try
call mkdir(required_dir, 'p')
catch
if confirm("Can't create '" . required_dir . "'", "&Abort\n&Continue anyway") != 2
bdelete
return
endif
endtry
endif
endfunction
autocmd BufNewFile,BufWritePre * call AutoMkDir()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment