# Can I create directories that don't exist while creating a new file in emacs?

In emacs, I create a file by visiting it with C-x C-f. Let's say I'd like to create /home/myself/new_directory/file.txt.

If new_directory does not exist yet, is there a way to have it get created during the creation of file.txt without any extra steps? (I'm thinking of something like using the -p flag to mkdir in Linux.)

I feel like there's a different keystroke instead of C-x C-f that can do this, but I can't remember what it is.

-
There's no equivalent in emacs to lanch a command while editing?? in vi you could :!mkdir -p ~/new_dir/to/some/crazy/path –  DaveParillo Apr 16 '10 at 4:28
@DaveParillo: Of course there is, M-! for instance. –  Nikana Reklawyks Oct 29 '12 at 15:09

You can also advise function find-file to transparently create necessary directories.

(defadvice find-file (before make-directory-maybe (filename &optional wildcards) activate)
"Create parent directory if not exists while visiting file."
(unless (file-exists-p filename)
(let ((dir (file-name-directory filename)))
(unless (file-exists-p dir)
(make-directory dir)))))


Simply put this in your .emacs somewhere and use C-x C-f as usual.

-
Wonderful solution. I love how improving small things can open a whole new world of hints to give emacs to do stuff better (yeah, I didn't know about defadvice before). –  Nikana Reklawyks Oct 29 '12 at 15:15

When I supply a pathname with a nonexistent component, find-file (i.e. C-x C-f), gives me an extra message that says

Use M-x make-directory RET RET to create the directory and its parents

Since the file is not created until you first save the buffer, you can either run make-directory right after your new buffer comes up or you can do it any other time before you need to save the file. Then from the buffer that needs a directory, type M-x make-directory RET RET (it will prompt for the directory to create (the default is derived from the buffer's pathname); the second RET is to accept the default).

-

The Ido mode provides ido-find-file that is a replacement of find-file and gives you much more features. For instance, it allows you to create new directory meanwhile you open the file.

• Type C-x C-f as usual (which is remapped to ido-find-file),

• provide the non-existent path,

• press M-m which will prompt for the new directory to create,

• and then specify the file name to visit in the newly created directory.

-
I don't get it : why press M-m at some point, and C-x C-f at all if that doesn't create everything automagically ? If to be prompted for the directory to create, M-! mkdir or dired will do a fair job too… –  Nikana Reklawyks Oct 29 '12 at 15:13

In addition to @Chris' suggestion of M-x make-directory, you can write a short elisp function that will do find-file and then make-directory... You can try this:

(defun bp-find-file(filename &optional wildcards)
"finds a file, and then creates the folder if it doesn't exist"

(interactive (find-file-read-args "Find file: " nil))
(let ((value (find-file-noselect filename nil nil wildcards)))
(if (listp value)
(mapcar 'switch-to-buffer (nreverse value))
(switch-to-buffer value)))
(when (not (file-exists-p default-directory))
(message (format "Creating  %s" default-directory))
(make-directory default-directory t))
)


It's not pretty, and it flashes up the "it M-X make-directory...." before saying "Creating directory..." but if nothing else, it should give you a start.

-
In case of this approach, it's better to advice the original find-file function instead of defining a new one so that other functions calling find-file directly can even benefit of the modified behavior. –  Török Gábor Apr 16 '10 at 14:44
but find-file doesn't seem to take any arguments that can tell it to do this... Unless there's something useful that you can do in find-file-hooks... –  Brian Postow Apr 16 '10 at 15:29
I meant this: superuser.com/questions/131538/… –  Török Gábor Apr 20 '10 at 8:27