How do I create a directory from within Emacs?

52,281

Solution 1

  • 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

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

Solution 3

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

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

M-x shell-command

then

mkdir -p topdir/subdir

Solution 5

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.

Share:
52,281

Related videos on Youtube

Ray
Author by

Ray

Writes code and likes it. A lot.

Updated on October 09, 2020

Comments

  • Ray
    Ray over 3 years

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

  • Ray
    Ray over 15 years
    I assume the '+' goes at the end of whatever the new file path directory is going to be?
  • why
    why over 12 years
    Hi J.F. Sebastian, When I create a directory in shell , how to update the directory window ?
  • jfs
    jfs over 12 years
    @why: type g. In general M-x describe-mode RET (C-h m) to display documentation of current major mode.
  • why
    why over 12 years
    Thanks very much! But you mean I input M-x g or C-x g ?
  • Vicky Chijwani
    Vicky Chijwani almost 12 years
    @why he means a single g. Since dired-mode is not an editing mode, it uses single-letter commands directly (for the curious: g is bound to self-insert-command in most editing modes).
  • Zeynel
    Zeynel over 10 years
    when I do C-x C-f and enter "+ dirname" emacs creates a file "+ dirname", it does not create a directory "dirname". I am working with OSX, I don't know if that's the problem because in Linux this worked.
  • Drew
    Drew over 10 years
    @Zeynel: You're not reading. There is no C-x C-f here. You use C-x d to get into Dired mode, visiting the directory where you want to create a subdirectory. Then you hit +. That's all.
  • mistige
    mistige almost 4 years
    This works, type 'g' afterwards to update the screen contents.
  • x-yuri
    x-yuri over 2 years
    C-x d *.py RET is supposed to show python files? It says: "Reading directory: No such file or directory, /some/path/*.py".
  • jfs
    jfs over 2 years
    @x-yuri: have you typed C-x d *.py RET or C-x d /some/path/*.py RET? The former works in my environment.
  • x-yuri
    x-yuri over 2 years
    Oh, sorry, by the look of it I probably thought it would find python files in subdirectories as well (recursively). To answer your question, it autosuggested the /some/path/ part. And I believe I tried both back then.