Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Emacs create intermediate dirs - when saving a file?

Tags:

emacs

Is there a way to create folder tree in emacs - similar to

mkdir -p

in bash?

Basically - I want emacs to create all the intemediate dirs - if they were not existing - when I save a file.

like image 785
Adobe Avatar asked Jul 26 '11 13:07

Adobe


People also ask

How do I create a dired folder?

The command + ( dired-create-directory ) reads a directory's name, and creates that directory. It signals an error if the directory already exists.

How do you save a buffer in Emacs?

To save the file you are editing, type C-x C-s or select Save Buffer from the Files menu. Emacs writes the file. To let you know that the file was saved correctly, it puts the message Wrote filename in the minibuffer.

How do I write to a file in Emacs?

To insert text into a buffer, place the cursor where you want to start inserting text, and start typing away. If you want to insert the contents of another file into the current buffer, place the cursor at the desired insertion point, and type Control-X-I. Emacs will ask you for the name of the file you wish to insert.


1 Answers

Function make-directory does that. Your particular problem may be solved like this:

(add-hook 'before-save-hook
          (lambda ()
            (when buffer-file-name
              (let ((dir (file-name-directory buffer-file-name)))
                (when (and (not (file-exists-p dir))
                           (y-or-n-p (format "Directory %s does not exist. Create it?" dir)))
                  (make-directory dir t))))))
like image 173
Victor Deryagin Avatar answered Dec 26 '22 02:12

Victor Deryagin