When opening a new file whose parent directory doesn't exist yet, I either open a shell window, or an Emacs shell buffer, and then
mkdir the directory in it. I find this cumbersome. Is there an easier way?
You don't need to do that, if that's your question.
C-x C-f /some/new/directory/newfile.txt
Emacs prints a message to let you know that the directory
/some/new/directory/ does not yet exist:
Use M-x make-directory RET RET to create the directory and its parents.
Insert text into the new buffer for new file
C-x C-s to save the file.
Emacs asks you whether you want to create the missing intermediate directories (e.g.,
new/directory/. You hit
y for "yes".
Not very cumbersome, IMO. The UI needs to ask you for confirmation, because you could easily have mistyped the name of an existing directory.
If you really want to make
find-file to automatically create parent directories when not already exists, you can do the following in your init file.
(defun my-find-file (orig-fun &rest args) (let* ((filename (car args)) (directory (file-name-directory filename))) (if (not (file-directory-p directory)) (make-directory directory t)) (apply orig-fun args))) (advice-add 'find-file :around 'my-find-file)
After all, Emacs was designed to be extensible and customizable.