Managing Directories with Emacs
Supposing is good, but finding out is better.
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
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.
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.
|What it does
|! or X
|Run shell command on item
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.
|What it does
|Mark an item
|Mark an item for deletion
|Unmark a single item
|Unmark all items
|Toggle marks between marked and unmarked items
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.
|What it does
|Go up one level, like
|e or f
|Edit an item
|View an item, like
|Reload the current directory
|Jump to an item
|Change sort order
|Show file type of item, like
|Copy item name to clipboard
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
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.
We have only touched the tip of the iceberg. Feel free to explore. For more information visit the Dired manual here.