Iqbal Ansari

My little corner on the Internet

Automatically Create Parent Directories on Visiting a New File in Emacs

| Comments

In emacs we can open a file in a non-existent directory and then use the command M-xmake-directory to create the parent directory (or directories) if they do not exist. While this is really useful, I find it a bit cumbersome to press M-xmake-directoryRETRET every time I visit such a file. I was looking for ways to automate this when I came across the variable find-file-not-found-functions. According to the documentation (C-hvfind-file-not-found-functionsRET)

List of functions to be called for `find-file' on nonexistent file. These functions are called as soon as the error is detected. Variable ‘buffer-file-name’ is already set up. The functions are called in the order given until one of them returns non-nil.

I wrote a simple function which checks whether the parent directories exist for a given file and offers to create them if they do not exist.

Offer to create parent directories if they do not existlink
(defun my-create-non-existent-directory ()
      (let ((parent-directory (file-name-directory buffer-file-name)))
        (when (and (not (file-exists-p parent-directory))
                   (y-or-n-p (format "Directory `%s' does not exist! Create it?" parent-directory)))
          (make-directory parent-directory t))))

I added this function to find-file-not-found-functions

(add-to-list 'find-file-not-found-functions #'my-create-non-existent-directory)

So now whenever I visit a file a non existent directory emacs offers to create the parent directories.