This article shows how the manage files with Emacs. The dired package enables you to open, copy, move and delete files on your computer or network.

Manage Files with Emacs: Organise your drive with Dired

Peter Prevos

Peter Prevos |

1926 words | 10 minutes

Share this content

Working with Emacs means that you will need to access, create files and manage files on your drives. Emacs comes shipped with dired (pronounced dir-ed), a potent file manager. This article explains the basic principles of using dired and shows how to manage files with Emacs to organise your hard drive.

Emacs Writing Studio

Emacs Writing Studio

A comprehensive guide for writers seeking to streamline their workflow using Emacs. The book covers everything from organising ideas and writing distraction-free to publishing in multiple formats. It’s perfect for both beginners and experienced Emacs users, offering practical tips and a tailored configuration to enhance your writing process.

The source files of the book and EWS configuration are also freely available on GitHub.

Accessing directories and files with dired

Dired is short for “directory editor”, but it can do much more than just that. This software has an illustrious history. The first version of dired was a stand-alone program written circa 1974, so its origins are even further back in time than Emacs. The software comes packaged with Emacs and it provides a front end to various GNU core utilities.

You activate the file manager with the dired function or the C-x C-d shortcut. When you hit enter, Emacs will create a buffer with the content of the directory of your open buffer. You can also enter another directory in the minibuffer before you hit the enter key. Another useful function is dired-jump (C-x C-j) which opens dired and jumps to the file linked to the current buffer. The q button closes the dired window but does not kill (remove) it.

You can navigate the content with the arrow keys and press j to jump to a specific file by entering part of its name.

The enter key opens the respective file or directory. To open a file or directory in another window, press o (Using C-o open the file in another window, but the cursor stays in Dired). The carat key ^ takes you to the parent folder.

Note that every time you open a new directory, Emacs will open a new dired buffer. After a while, you can litter your Emacs session with unused dired buffers. Pressing the a key opens a directory in the same buffer. This functionality is disabled by default because the Emacs developers strangely believe that new users find it confusing. Emacs Writing Studio configures this behaviour by default.

Copying and Moving Files

To copy a file, press the C button. Dired will ask for a new name and location in the minibuffer. To move a file, you press R because moving a file is the same as renaming it with a new directory.

There is no need to close the buffer before you rename an open file. Emacs will link the open buffer to the new filename.

If you have two open dired buffers, Emacs Writing Studio copies and moves from the folder in the active window to the other dired buffer.

Renaming files

Renaming the file is the same as moving it. So press R and type a new filename.

It is sometimes useful to copy the name of a file to the kill ring with the w key, so you can use it to rename the file. So to rename a file, copy the name with w, rename the file with R and paste the existing name with C-y and edit the name to your new version.

Removing files

You can select and deselect files for deletion (killed) with the d and u buttons. After you selected the files you like to delete, press x to execute the deletion. Press capital D if you like to remove a single file.

By default, Emacs permanently removes files. The Emacs Writing Studio is configured so that files are moved to the recycle bin.

When you delete or trash a currently open file, Emacs will also ask you to close the appropriate buffer.

Working on Multiple Files

You can select multiple files to work on at the same time by marking them. The m button marks a file, and the u removes the mark. The capital U removes all marks in the buffer. The t key reverses your markings, which is helpful when you want to select everything but one or two files.

This method requires you to manually select each file. You can also use regular expressions to select files. Regular expressions are a powerful set of logical rules to select sections of text. Simply put, regex is wildcards on steroids—press % m to open the regular expression selection prompt.

For example, ^2023.*_journal* selects all Denote files that start with the 2023 and that have the journal file tag. Now press t to invert the selection and k to remove the selected files from view. This sequence is a useful method to find related files.

After you selected multiple files in this manner, you can use all file commands to act on the selected targets, for example moving all 2023 files with the _journal tag to another folder.

Opening a file with another program

Emacs is a Swiss-Army chainsaw, but it cannot do everything. Sometimes you might like to open a file in other software, such as your image editor or video player.

You can open files with external software by pressing & after which dired will ask for the appropriate software. You need to type the name of the executable file of the software you like to use, e.g. gimp.

Manage files with Emacs dired

If your head is buzzing with all the different key bindings, the table lists the functionality described in this article. These are only a small snapshot of the functions of the directory editor in Emacs. You can press the h key while in a dired buffer to view all functionality and related keybindings.

Key Function Action
a dired-find-alternate-file Open folder in same buffer
C dired-do-copy Copy a file
j dired-goto-file Jump to a file
g revert-buffer Refresh the dired buffer
m dired-mark Mark file under cursor
% m dired-mark-files=regexp Mark by regular expression
o dired-find-file-other-window Open file in other window
q quit-window Close the buffer
R dired-do-rename Rename (move) a file
t dired-toggle-marks Inverse marked files
u dired-unmark Unmark file under cursor
U dired-unmark-all-marks Unmark all files
& dired-do-async-shell-command Open file with other program
enter dired-find-file Open file
Dired key bindings.

The dired package is a convenient and powerful tool to keep your drives organised and access your information. Developers have published an extensive collection of extensions to dired to add functionality, which you can find in the package manager.

Dired lists files and directories in alphabetical order. I prefer a different view, which shows directories on top and files below them. The parameters determine the order of the entries in the folder.

Removing hidden files from view

Your Dired view might be littered with hidden files and folders that start with a dot. You can remove these by marking them with a regular expression and removing these lines from view, but that is a bit awkward to do every time you open a Dired buffer. The Dired-X package provides functionality to hide files that meet a certain condition. The Emacs Writing Studio configuration for Dired omits all files staring with a dot.

  ;; Hide hidden files
  (use-package dired-hide-dotfiles
    :hook
    (dired-mode . dired-hide-dotfiles-mode)
    :bind
    (:map dired-mode-map ("." . dired-hide-dotfiles-mode)))

Dired Configuration

Emacs Writing Studio uses the following configuration for Dired:

  (use-package dired
    :ensure
    nil
    :commands
    (dired dired-jump)
    :init
    (with-eval-after-load 'dired (require 'dired-x))
    :config
    (put 'dired-find-alternate-file 'disabled nil)
    :custom
    (dired-listing-switches
     "-goah --group-directories-first --time-style=long-iso")
    (dired-dwim-target t)
    (delete-by-moving-to-trash t)
    (dired-omit-files (rx (seq bol "."))) ;; Omit dot files
    :bind
    (:map dired-mode-map ("." . dired-omit-mode))
    :hook
    (dired-mode . dired-omit-mode))

Backup files

This last bit of configuration code defines how Emacs deals with automated backups. The default setting is that the system stores these files in the folder where the original files lives, clutterin/g your drive with copies of your stuff. The setting below modifies the backup-directory-alist variable so that Emacs saves all backups in your configuration folder. This configuration also eliminates lock files, which are only useful when working in shared folders.

  ;; Backup files
  (setq-default backup-directory-alist
                `(("." . ,(expand-file-name "backups/" user-emacs-directory)))
                create-lockfiles nil)  ; No lock files

Alternatively, you could instruct Emacs to not save backups at all with (setq-default make-backup-files nil). I prefer keeping backups as they have saved my bacon a few times in the past.

Recent Files and Bookmarks

Whenever you return to Emacs you might want to open a file you were working on recently. The recent files minor mode (recentf-mode) provides a transient list of the files you most recently opened.

This minor mode saves the most recent opened files when you exit Emacs to a file in your configuration folder. However, it might be more useful to save the recent files regularly to ensure it is saved. The run-at-time function runs a function at a regular interval, in this case every five minutes. The recentf-edit-list function opens the file with your recent acquisitions and lets you delete selected files.

By default, the recent files mode stores the last twenty opened files, which you can change by modifying the recentf-max-saved-items variable.

  ;; Recent files
  (use-package recentf
    :config
    (recentf-mode t)
    (run-at-time nil (* 5 60) 'recentf-save-list)
    :custom
    (recentf-max-saved-items 50))

Recent files are transient as they are continuously updated as you open new files. For a more permanent list of files you like to open, use bookmarks.

You can store a file as a bookmark with C-x r m (bookmark-set). The bookmark will also store the location of the cursor, so you can maintain multiple bookmarks for a file. The default name for the bookmark is the name of the file. You can also enter a bespoke name in the minibuffer before hitting ENTER.

To view a list of all bookmarks in the minibuffer and select the one you like to open, use C-x r b (bookmark-jump).

Bookmarks are saved in the bookmarks file in your configuration folder every time a new bookmark is created. The bookmark-save-flag is set to one so that the bookmarks file is saved every time you add a new one. The default value only saves it when you exit Emacs, which mean you could loose bookmarks in the unlikely event of an Emacs or system crash.

If you like to remove bookmark no longer required then use the bookmark-delete function, which has no default keybinding but is bound to C-x r D in the Emacs Writing Studio configuration.

  ;; Bookmarks
  (use-package bookmark
    :custom
    (bookmark-save-flag 1)
    :bind
    ("C-x r D" . bookmark-delete))

Emacs Writing Studio

If you like to support my work, then please purchase the Emacs Writing Studio book.

Emacs Writing Studio

Emacs Writing Studio

A comprehensive guide for writers seeking to streamline their workflow using Emacs. The book covers everything from organising ideas and writing distraction-free to publishing in multiple formats. It’s perfect for both beginners and experienced Emacs users, offering practical tips and a tailored configuration to enhance your writing process.

You can find the source files for the book and the latest configuration files on GitHub:

Emacs is a malleable system, so everybody will have their personal preferences to undertake a task. Any article on how to be productive with Emacs is thus opinionated. If you have a different way of doing things, please share your views and leave a comment below, or complete the contact form to send me an email.

The next article in this series discusses how to use dired to view images.

Share this content

You might also enjoy reading these articles

Writing Prose with Emacs

Exploring Your Ideas With the Denote-Explore Package

Reading eBooks with Emacs