mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Create A New File In A New Directory as a vim til.
This commit is contained in:
@@ -69,6 +69,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
- [Buffer Time Travel](vim/buffer-time-travel.md)
|
- [Buffer Time Travel](vim/buffer-time-travel.md)
|
||||||
- [Close the Current Buffer](vim/close-the-current-buffer.md)
|
- [Close the Current Buffer](vim/close-the-current-buffer.md)
|
||||||
- [Count the Number of Matches](vim/count-the-number-of-matches.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)
|
- [Generate and Edit Rails Migration](vim/generate-and-edit-rails-migration.md)
|
||||||
- [Head of File Name](vim/head-of-file-name.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)
|
- [Help For Non-Normal Mode Features](vim/help-for-non-normal-mode-features.md)
|
||||||
|
|||||||
38
vim/create-a-new-file-in-a-new-directory.md
Normal file
38
vim/create-a-new-file-in-a-new-directory.md
Normal file
@@ -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.
|
||||||
Reference in New Issue
Block a user