3

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?

Skurpi
  • 1,000
  • 1
  • 12
  • 22

2 Answers2

1

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.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
1

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

mliebelt
  • 15,345
  • 7
  • 55
  • 92