How do I create a directory from within Emacs?

EmacsDirectoryCommand

Emacs Problem Overview


How exactly can I create a new directory using Emacs? What commands do I use? (If possible, please provide an example)

Emacs Solutions


Solution 1 - Emacs

  • to create the directory dir/to/create, type:

      M-x make-directory RET dir/to/create RET
    
  • to create directories dir/parent1/node and dir/parent2/node, type:

      M-! mkdir -p dir/parent{1,2}/node RET
    

    It assumes that Emacs's inferior shell is bash/zsh or other compatible shell.

  • or in a Dired mode

      +
    

    It doesn't create nonexistent parent directories.

    Example:

      C-x d *.py RET ; shows python source files in the CWD in `Dired` mode
      + test RET     ; create `test` directory in the CWD
    

    CWD stands for Current Working Directory.

  • or just create a new file with non-existing parent directories using C-x C-f and type:

      M-x make-directory RET RET
    

Emacs asks to create the parent directories automatically while saving a new file in recent Emacs versions. For older version, see How to make Emacs create intermediate dirs - when saving a file?

Solution 2 - Emacs

Ctrl+X D (C-x d) to open a directory in "dired" mode, then + to create a directory.

Solution 3 - Emacs

You can also run single shell commands using M-!

You're basically sending a string to the command line so you don't get any nice auto-completion but it's useful if you know how to perform an action through the command line but don't know an Emacs equivalent way.

M-! mkdir /path/to/new_dir

Solution 4 - Emacs

I guess I did it the hard way earlier today. I did:

M-x shell-command

then

mkdir -p topdir/subdir

Solution 5 - Emacs

You can use M-x make-directory inside of any buffer, not necessarily a dired buffer. It is a lisp function you can use as well.

Solution 6 - Emacs

I came across this question while searching for how to automatically create directories in Emacs. The best answer I found was in another thread from a few years later. The answer from Victor Deryagin was exactly what I was looking for. Adding that code to your .emacs will make Emacs prompt you to create the directory when you go to save the file.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRayView Question on Stackoverflow
Solution 1 - EmacsjfsView Answer on Stackoverflow
Solution 2 - EmacsKirk StrauserView Answer on Stackoverflow
Solution 3 - EmacsCristianView Answer on Stackoverflow
Solution 4 - EmacsetankView Answer on Stackoverflow
Solution 5 - EmacsJonathan ArkellView Answer on Stackoverflow
Solution 6 - EmacsBrian TaylorView Answer on Stackoverflow