Skip to content

Instantly share code, notes, and snippets.

@omar2205
Created November 5, 2022 22:46
Show Gist options
  • Save omar2205/aca4aea04f96924ed8bf35a3818eded4 to your computer and use it in GitHub Desktop.
Save omar2205/aca4aea04f96924ed8bf35a3818eded4 to your computer and use it in GitHub Desktop.
Vim create a dir if it's doesn't exist
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