Skip to content

Instantly share code, notes, and snippets.

@PeterRincker
Created February 3, 2022 16:21
Show Gist options
  • Save PeterRincker/80b80ff5e55f0489fc484d6fe201d4fa to your computer and use it in GitHub Desktop.
Save PeterRincker/80b80ff5e55f0489fc484d6fe201d4fa to your computer and use it in GitHub Desktop.
" create parent directories
" http://stackoverflow.com/questions/4292733/vim-creating-parent-directories-on-save
function! s:MkNonExDir(file, buf)
if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/'
let dir=fnamemodify(a:file, ':h')
if !isdirectory(dir)
call mkdir(dir, 'p')
endif
endif
endfunction
augroup BWCCreateDir
autocmd!
autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment