I'm trying to improve the code from this answer to ask for confirmation before creating the missing directory, this is what I write:
function! CreateDirectoryAskConfirmation(path) if !isdirectory(a:path) echohl Question echo "Create directory " . a:path . "?" echohl None let response = nr2char(getchar()) if response ==? "y" call mkdir(a:path, "p") endif endif endfunction autocmd BufWritePre * call CreateDirectoryAskConfirmation(expand("<afile>:p:h"))
One thing I'm missing: when I don't press
y at the prompt I'd like Vim to abort the write so I don't get the error
E212: Can't open file for writing and I don't need to hit another key to make the error message disappear. There is a way to achieve this?