Introduction to Dired Mode
The legendary Directory Editor / File Manager of Emacs
DIRED
(short for Directory Editor) is the native file manager of Emacs. It was the first file manager, or visual editor of file system information ever created. The first version of Dired was written as a stand-alone program circa 1974 by Stan Kugell at the Stanford Artificial Intelligence Laboratory (SAIL). It was incorporated into GNU Emacs from the earliest versions and re-implemented in C and C++ on other operating systems. For more information, check out its manual.
In this article, we cover the basics of using Dired within Emacs. Also check out this awesome cheat sheet by Sacha Chua, which covers most of the below. For a nice comprehensive summary of all the features of Dired, simply enter h
when within Dired Mode.
Starting & Exiting Dired
You can start Dired Mode with C-x d
, which will then prompt you to enter the directory you wish to view. You can also enter Dired with C-x C-f
and then typing the directory you wish to view (as opposed to entering a filename).
Dired Mode is a read-only buffer, so many of the commands are single letter keystrokes. You can exit Dired mode with q
.
As a final note, you can use wildcards *
to filter the files you want to view in Dired. The first command below opens all .el
files within ~/foo/
whilst the second command below opens all .el
files in all subdirectories of ~/foo/
.
C-x d ~/foo/*.el RETC-x d ~/foo/*/*.el RET
Navigating the Dired Buffer
You can use n
to move to the next line, p
to move to the previous line. You can use M-<
and M->
to move to the start and end of the buffer respectively.
You can jump to a file within the directory listing with j
(which will then prompt you to the file you want to jump to). You can toggle between sorting alphabetically and by last modified with s
.
Opening Files
You can open files with f
or with RET
(enter). You can open files in read-only view mode with v
.
Navigating the File System
We can move up a directory with ^
. You can also press f
or RET
on the ..
listing, but ^
works better as it will keep your point on the directory you came from (try both and see).
Deleting Files
There are two approaches to deleting files:
- Use
d
to flag files and then delete them withx
(you can unflag files withu
) - Use
m
to mark files and then delete them withD
(that’s a capital letter) (again, you can unmark files withu
)
To send the files to your trash and not permanently delete them, add the following to you .emacs
(with the second command for MacOS users, and in general I only tested the below on my Mac so your mileage may vary):
(setq delete-by-moving-to-trash t)(if (eq system-type 'darwin)
(setq trash-directory "~/.Trash"))
Marking Files
The marking done above in the second approach for deleting files (m
to mark and u
to unmark) is also used when trying to copy and move multiple files, per below. We can use t
to toggle all files currently marked to unmarked and all unmarked files to marked. You can also mark all files with * s
.
Copying Files
You can copy files with C
(you will be prompted for where to copy the file to). When more than one file is marked (i.e. selected), you will be copying these files to a new directory.
Renaming & Moving Files
You can rename files with R
(you will be prompted for the new name — you can use this to also move the file as you can edit the file path of the renamed file). When more than one file is marked, you will be rather moving these files files to a new directory.
Touching Files
Sometimes you want to update the timestamp of a file (e.g. to trigger some process like a compiler to pick up the updated file). This can be done with T
.
Creating New Directories & Files
You can create new directories with +
. You can create new files with C-x C-f
(this is just the normal create file command and not actually a dired feature, but noting here for completeness).
Refreshing the Dired Buffer
You can refresh the contents of the dired buffer with g
.
Appendix: Beyond Dired
One of our readers has kindly pointed out that a lot has happened in the decades since dired was first created, with many people contributing additional features. Some features have been absorbed into Emacs itself, but many still exist only as add-on packages. There are roughly 50 extensions to Dired on Melpa, so something that is worth checking out if its an area of interest to you.
One such package is diredc, written by Boruch Baum. diredc adds a whole lot of additional functionality to Dired, including for example, the ability to view, manage, restore, or empty your trash. I am impressed by its quality of documentation, and recommend it to you if you are interested in such packages. Check it out!