└── plugin └── auto_mkdir.vim /plugin/auto_mkdir.vim: -------------------------------------------------------------------------------- 1 | " ---------------------------------------------------------------------- 2 | " 3 | " File: auto_mkdir.vim 4 | " Author: Johannes Holzfuß 5 | " Version: 1.0.0 6 | " 7 | " Description: 8 | " When saving a file, automatically create the file's parent 9 | " directories if they do not exist yet. 10 | " 11 | " For example, 12 | " 13 | " :w foo/bar/baz.txt 14 | " 15 | " will create the directories foo/ and foo/bar/ if they do not exist 16 | " already, then save the current buffer as baz.txt 17 | " 18 | " License: 19 | " This file is placed in the public domain. 20 | " 21 | " ---------------------------------------------------------------------- 22 | 23 | if exists("g:loaded_auto_mkdir") 24 | finish 25 | endif 26 | let g:loaded_auto_mkdir = 1 27 | 28 | if !exists("*mkdir") 29 | echomsg "auto_mkdir: mkdir() is not available, plugin disabled." 30 | finish 31 | endif 32 | 33 | if !has("autocmd") 34 | echomsg "auto_mkdir: autocommands not available, plugin disabled." 35 | finish 36 | endif 37 | 38 | augroup auto_mkdir 39 | au! 40 | au BufWritePre,FileWritePre * call auto_mkdir() 41 | augroup END 42 | 43 | function auto_mkdir() 44 | " Get directory the file is supposed to be saved in 45 | let s:dir = expand(":p:h") 46 | 47 | " Create that directory (and its parents) if it doesn't exist yet 48 | if !isdirectory(s:dir) 49 | call mkdir(s:dir, "p") 50 | endif 51 | endfunction 52 | --------------------------------------------------------------------------------