In emacs we can open a file in a non-existent directory and then use the command M-x
make-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-x
make-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-hv
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.
1 2 3 4 5
I added this function to
So now whenever I visit a file a non existent directory emacs offers to create the parent directories.