/Ryan Faulhaber/posts/cool emacs things: dired

Cool Emacs Things: Dired

Fri, May 28, 2021 - 3 min

Today we continue my ongoing series on Emacs with a feature of Emacs that feels genuinely magical: Dired.

One of the first things I did when I became a Linux user was to learn how to use the terminal. The terminal is an indispensable tool in the Unix world broadly because of its versatility. I got to a point where I used the terminal so heavily that eventually on Linux I stopped installing file explorers, and on macOS I had entire directories that never had those annoying .DS_Store files. Personally, managing files in the terminal is often much simpler and more direct than a graphical file explorer.

Navigating your file system without a graphical file explorer requires you to know two standard Unix programs: cd (change directory) and ls (list directory contents). ls in particular can print out very detailed information about the files in your system:

# navigate to a project directory
cd ~/Projects/dial
# print contents
ls -lh

From left-to-right, these columns represent: file permissions (modifiable by chmod), links to this file, file owner (modifiable by chown), group owner (modifiable by chgrp), file size, last modified timestamp, and file name.

Dired defies explanation for those who haven’t used it, and while writing this I struggled with how to explain it exactly. I’m tempted to describe it as “interactive ls”.

Imagine if, when you typed ls and got a list of files, you could click on those names. If the name is a directory, your terminal would automatically cd to that directory and invoke ls again. If that name is a file, your terminal opens that file in your favorite text editor. If your favorite text editor is Emacs, I have good news for you: this is what Dired is.

Dired mode. Mine looks a little weird because I have exa aliased to ls.

Dired mode. Mine looks a little weird because I have exa aliased to ls.

But Dired does a little more than just navigation: Dired is a file explorer in its own right. From Dired you can move, copy, rename, and delete files, make links, run rsync (with an extra package), diff files, and more.

But that’s not all! Dired has a sibling called wdired-mode, or “writable Dired.” wdired lets you edit a Dired buffer, thereby allowing you to rename files with all the editing powers of Emacs. So, for example, replacing all of your .js to .ts files is just a matter of finding and replacing js with ts. wdired will also let you change file permissions, so you could change rw-r--r-- to, say, rw-rw-rw- by simply changing the text without having to break out chmod.

This is why I navigate my file explorer in Emacs rather than my terminal now.