Skurpi
Skurpi

Reputation: 1020

How to automatically create whole path when creating new file using vim?

To open new files while in vim I usually do the following:

:tabe my_newfile

However, say I want to open a new file in a NEW directory, like so:

:tabe newdir/my_newfile

leads to the error

"newdir/my_newfile" E212: Can't open file for writing

Is there a smooth way to create the newdir automatically?

Upvotes: 3

Views: 421

Answers (2)

mliebelt
mliebelt

Reputation: 15525

Try the plugin auto_mkdir. It sounds exactly what you are searching for.

PS: I don't have used it, and I will not (due to the comment of @Aaron_Digulla

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328594

I'm using this workaround outside of vim: I've created a shell script vim in $HOME/bin which makes backup of every file that I edit in folder $HOME/.vimback/...path.../file.

In a similar way, you could create a similar script that runs

for f in "$@" ; do
    mkdir -p $(dirname "$f")
done

/usr/bin/$(basename "$0") "$@"

But I'd suggest to ask whether the directories should be created to avoid accidentally creating directories because of typos.

Upvotes: 1

Related Questions