Edit a new file in a directory that does not exist yet, such as
Try to save the file:
An error will display:
"app/views/application/index.html.erb" E212: Can't open file for writing Press ENTER or type command to continue
The error means is it can't save the file because
app/views/application doesn't exist. So, we have to create it.
Making the directory in the shell
I used to suspend Vim:
Then, create the directory from the shell:
Then, bring Vim back to the foreground:
Making the directory in Vim
What Sean does in Hands-On Backbone.js on Rails is create the directory right from Vim:
:!mkdir -p %:h
How it works
% is the current file in Vim.
%:h is the directory in which the current file is located. So, when editing
app/views/application/index.html.erb, the command expands to
mkdir -p app/views/application.
For the fastest approach yet, use vim-mkdir to just automatically make the directory when it doesn't exist, with no manual intervention necessary.