Managing Directories with Emacs

Esperanto | English
Last updated: March 17, 2022

Supposing is good, but finding out is better.
—Samuel Clemens

lucas-benjamin-V-mEcfI8fsI-unsplash

Table of contents

Introduction

In this post, I’ll be focusing on one of the smart ways Emacs handles directory management. The directory editor, or Dired (pronounced dir-ed, not dye-rd), is the Emacs equivalent of a file manager. Whatever you can do with regular buffers, you can also do it with dired ones.

To run Dired, run Emacs on the command line, supplying a directory as its argument:

$ emacs ~/Desktop

Or, alternatively, you can press C-x d inside Emacs. If you are currently editing a file, the directory of that file will be presented as the default value in the minibuffer area. Either way, when you hit Enter, a buffer of the directory will load, that looks like the output of ls -l:

/home/vakelo/Desktop/foo:
total used in directory 84 available 540767396
-rw-r--r-- 1 vakelo users 5935 Sep 27 18:17 index.html
drwxr-xr-x 3 vakelo users 4096 Sep 26 17:42 pics
drwxr-xr-x 3 vakelo users 4096 Sep 26 05:39 vids

Okay, now that you have it, what can you do with it? Well, here is a short list of what, you can do with it. Take note, that the keyboard shortcuts in this article are case-sensitive, unless explicitly stated otherwise.

Common commands

These are the usual commands that you would use, in a dired buffer. In addition to that, they can operate on single, or multiple items. Using them for multiple items will be explained, next.

KeyWhat it does
RRename item
CCopy item
DDelete item
OChange owner
GChange group
MChange permissions
SCreate symlink
TTouch item
! or XRun shell command on item

Mark commands

These commands perform mark-related operations on items. Creating marks simply means putting a tag on items, so that you can perform the operations in the previous section, on them.

KeyWhat it does
mMark an item
dMark an item for deletion
xExecute operation
uUnmark a single item
UUnmark all items
tToggle marks between marked and unmarked items
cCompress items

Other commands

These commands act on their own. They operate on single items, and they don’t make use of marks. The w command, however, is an exception.

KeyWhat it does
+Create directory
^Go up one level, like cd ..
e or fEdit an item
vView an item, like less
gReload the current directory
jJump to an item
sChange sort order
yShow file type of item, like file
wCopy item name to clipboard

WDired mode

But, one of the coolest, and often-overlooked feature of dired is the WDired mode. What it does is that it gives you a powerful ability to edit the item names in a dired buffer, just as you would on a typical buffer. To enter wdired mode, hit:

M-x wdired-change-to-wdired-mode RET

The major mode changes from Dired to Editable Dired. You can then rename the files, and directories, with ease. You can even use rectangle and replace functions on them, to make things easier. The changes you have made at this point are not yet saved. To save the changes, press C-c C-c.

Closing remarks

We have only touched the tip of the iceberg. Feel free to explore. For more information visit the Dired manual here.