If I invoke vim foo/bar/somefile
but foo/bar
don\'t already exist, Vim refuses to save.
I know I could switch to a shell or do :!mkdi
I think I managed to do this in three lines, combining what others are saying on this answer.
This seems to do the trick:
if has("autocmd")
autocmd BufWritePre * :silent !mkdir -p %:p:h
end
It attempts to create the folder automatically when saving a buffer. If anything bad happens (i.e. permission issues) it will just shut up and let the file write fail.
If anyone sees any obvious flaws, please post a comment. I'm not very versed in vimscript.
EDIT: Notes thanks to ZyX