The popular Emacs text editor can be used as a drop-in replacement for other programs in your system. For many users, this helps to reduce operating system bloat. For others, it can be waste of time. But if you’re in the former camp, this article will show you how to use your text editor as your file manager with the built-in Emacs Dired package.
Table of Contents
A Note On Emacs Documentation
Note: Emacs novices may be confused by instructions to use
RET. Emacs has its own sort of parlance when it comes to giving instruction. But it’s important to learn what these keys mean, because just about everything you’ll do in Emacs will be engaged with a key command or series of commands triggered by key combinations. Just know that these keys, used below, translate like so:
- Maps to your Ctrl key. So an instruction in Emacs written as
C-xmeans hold the Ctrl down and press the x key on your keyboard.
- This key represents your keyboard’s “Meta” key, or Alt as it is mapped on contemporary keyboards. So the command
M-xmeans hold down your Alt key and press the x key.
- In Emacs,
RETrepresents your return or “Enter” key.
Those are the most often invoked key commands you’ll see in Emacs-related instructions.
Directory Listings and Files in Emacs Dired
First and foremost, Emacs Dired provides you with a list of directories and files. But unlike a directory listing you get on the command line with
ls -al, this directory listing is editable. Hence the name Dired, which is derived from “directory editor.”
In order to invoke Dired in Emacs, all you need to do is type
In the “minibuffer” at the bottom of your Emacs frame, you’ll see “Find file:” and a prompt to enter a directory path.
RET after entering your directory and you will see a listing for that directory. This is where the fun begins.
Editing Directories and Files
Now that you know how to invoke Dired, it’s time to learn about how you can use it as a complete file manager.
Note that much of what is possible with Dired lies outside the scope of this article, which is meant to introduce you to the basics. Further resources to continue your learning will be provided at the end of the article.
How to Copy Files With Dired
In order to copy a file with Dired, use your arrow keys (or n [down one line] and p [up one line]) to drop your cursor over a certain file. Type the C key and you will be prompted, in the minibuffer, to rename the file. Note: you can use tab auto-completion for assistance.
How to Delete Files With Dired
When you delete files with Dired they are unrecoverable. Items deleted with Dired skip your trash directory and are immediately wiped from memory. So be extra cautious when deleting files this way.
With Dired, you can mark files for deletion using the d key. Once you have selected the files you want to delete, you can type x to initiate the deletion process. You will be prompted with a warning and must type
no to complete or abort the deletion.
Note that Emacs can be customized to skip the deletion warning prompt. For more advanced users, these kinds of customizations can speed up workflow. But for beginners it’s probably best to keep customizations to a minimum while learning the basics.
If you have selected a set of files for deletion but changed your mind, you can deselect the files for deletion by using your Backspace key or pressing U.
How To Mark Files
Just as you would Shift-select a series of files in your current file manager, Dired allows you to “mark” a set of files in order to isolate them and complete other operations upon them. You can mark files by pressing your m key. And you can unmark individual files by pressing u, or unmark all you’ve selected by typing U. You can also Backspace over files to unmark them.
How to Regex Search Files
Just like the popular
awk programs, Dired lets you use regular expressions to search for text patterns within files.
Select an individual or multiple files with m key and press A to initiate a prompt in the minibuffer. Fill in your regular expression and hit
RET. Emacs will commence with your search and display the output in a separate buffer. From that buffer, you can select an individual line and hit
RET to visit that file and line in another buffer.
Expand Your Dired Practice
No matter what line of work you’re in, managing files on your system with speed and accuracy is a critical skill. Emacs can handle just about any file management task you put before it. To learn the advanced use cases for Dired, check out the full Emacs documentation. Depending on your installation, the Emacs documentation can be found in
C-h r at any time while working in Emacs to enter the official documentation. Type m to search for “Dired” and click
RET. Then you will be shuttled to the complete documentation for Dired.