auto-mkdir on write

Thu, 10 Nov 2016 01:35:08 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Thu, 10 Nov 2016 01:35:08 -0500
changeset 431
4cb4cd6a2570
parent 430
cac0a6e87836
child 432
11b78e03f052

auto-mkdir on write

.vimrc file | annotate | diff | comparison | revisions
--- a/.vimrc
+++ b/.vimrc
@@ -360,6 +360,20 @@ augroup vimrc
     \ endif
 augroup END
 
+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 AutoMkdir
+  autocmd!
+  autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
+augroup END
+
 " https://mjj.io/2015/01/27/encrypting-files-with-gpg-and-vim/
 augroup encrypted
   autocmd!

mercurial