diff --git a/README.md b/README.md index 6af7bde..4ecbb24 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Buffer Time Travel](vim/buffer-time-travel.md) - [Close the Current Buffer](vim/close-the-current-buffer.md) - [Count the Number of Matches](vim/count-the-number-of-matches.md) +- [Create A New File In A New Directory](vim/create-a-new-file-in-a-new-directory.md) - [Generate and Edit Rails Migration](vim/generate-and-edit-rails-migration.md) - [Head of File Name](vim/head-of-file-name.md) - [Help For Non-Normal Mode Features](vim/help-for-non-normal-mode-features.md) diff --git a/vim/create-a-new-file-in-a-new-directory.md b/vim/create-a-new-file-in-a-new-directory.md new file mode 100644 index 0000000..d3b1cfa --- /dev/null +++ b/vim/create-a-new-file-in-a-new-directory.md @@ -0,0 +1,38 @@ +# Create A New File In A New Directory + +From within a vim session, if you create a buffer for a new file in a +directory that doesn't exist. For instance, let's say that `/features` +doesn't exist and the new file is `my_latest_feature_spec.rb`: + +``` +:e spec/features/my_latest_feature_spec.rb +``` + +Vim's command line will inform you that this is a buffer for a `[New +DIRECTORY]`. If you then make some changes and subsequently try to save the +file, Vim will present you with: + +``` +"spec/features/my_latest_feature_spec.rb" E212: Can't open file for writing +``` + +This is because the containing directory doesn't exist. You can quickly +create that directory with a combination of Vim filename shorthands and +shelling out to the `mkdir` command. + +``` +:!mkdir -p %:h +``` + +The `%` is shorthand for the qualified path of the current file. The `:h` is +a filename modifier that returns the *head of the filename*, that is, it +resolves to the path with everything except the name of the file. + +Thus, this command is essentially resolving to: + +``` +:!mkdir -p spec/features/ +``` + +Vim will shell out with this command making directories for all non-existent +directories in the given path. Now you can happily save your new file.