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 :!mkdir foo/bar
from Vim but I'm lazy 🙂
Is there a way to make Vim do that automatically when it saves the buffer?
Best Answer
Note the conditions:
expand("<afile>")!~#'^\w\+:/'
will prevent vim from creating directories for files likeftp://*
and!isdirectory
will prevent expensive mkdir call.Update: sligtly better solution that also checks for non-empty buftype and uses
mkdir()
: