Emacs Init: Dired Config

By Xah Lee. Date: . Last updated: .

Tips for setting up emacs dired.

Jump to File in Dired

In any open file, Alt+x dired-jumpCtrl+x Ctrl+j】 to jump to the directory of current buffer.

Before Emacs 28 (Released 2022-04) , you need to load a lib.

Put this in your Emacs Init File:

(if (< emacs-major-version 28)
      ;; for dired-jump
      (require 'dired-x))

Allow Copy/Delete Directory

You can copy or delete a dir, including all subdirectories.

Put this in your Emacs Init File:

;; allow dired to delete or copy dir
;; 'top means ask every time
;; 'always means no asking
(setq dired-recursive-copies 'top)
(setq dired-recursive-deletes top)

Smart Suggest Target Dir

When you do copy files, emacs prompts for a target dir. You can make emacs automatically suggest the target dir on the split pane. [see Emacs: Split Windows]

For example, Alt+x dired, then Alt+x split-window-below, then navigate to another dir. Now, when you press C to copy, the other dir in the split pane will be default destination. Same for rename R and others.

Put this in your Emacs Init File:

(setq dired-dwim-target t)

Hide File Owner Permission Info

emacs dired-hide-details-mode 2022-04-22

In dired, Alt+x dired-hide-details-mode(】.

If you want it always on, add a hook. Like this:

(defun my-dired-init ()
  "to be run as hook for `dired-mode'."
  (dired-hide-details-mode 1))

(add-hook 'dired-mode-hook 'my-dired-init)

Make dired use the same buffer for viewing directory

Dired leaves a trail of directories. After a while, you have a lot buffers of dirs.

If you want dired always use the same buffer, put this in your Emacs Init File:

(require 'dired )

(if (< emacs-major-version 28)
      (define-key dired-mode-map (kbd "RET") 'dired-find-alternate-file) ; was dired-advertised-find-file
      (define-key dired-mode-map (kbd "^") (lambda () (interactive) (find-alternate-file ".."))) ; was dired-up-directory
    (setq dired-kill-when-opening-new-dired-buffer t)))

Dired, Show All Subdirectories

(defun xah-dired-show-all-subdirs ()
"In `dired', show all files in subdirs (“recursively”) .
Note: for thousands of files, may take a few seconds.

Code originally by Oliver Winspear.
Version: 2023-10-31"
  (dired default-directory "-lR"))

;; here's older code:

;; (defun my-dired-recursive-expand ()
;;   "in `dired', show all subdirs.
;; By Oliver Winspear.
;; Version: 2023-10-31"
;;   (interactive)
;;   (when (not (eq major-mode 'dired-mode))
;;     (user-error "must be in `dired'."))
;;   (mapc (lambda (file)
;;           (when (file-directory-p file)
;;             (dired-insert-subdir file)))
;;         (directory-files-recursively default-directory "" t)))

Make Dired Use Easier Keys

Make keys easier to type. For example, 5 instead of shift+5 for %, and “5 n” instead of shift+= for +.

You can move other frequently dired commands you use that have difficult key, to prefix 5.

(defun my-dired-init ()
  "Config `dired'.
URL `http://xahlee.info/emacs/emacs/emacs_dired_tips.html'
Version 2021-07-30 2023-03-15 2023-04-05"

  (define-key dired-mode-map (kbd ",") #'dired-prev-dirline)
  (define-key dired-mode-map (kbd ".") #'dired-next-dirline)

  (define-key dired-mode-map (kbd "1") #'dired-do-shell-command)
  (define-key dired-mode-map (kbd "6") #'dired-up-directory)
  (define-key dired-mode-map (kbd "9") #'dired-hide-details-mode)

  (define-key dired-mode-map (kbd "b") #'dired-do-byte-compile)

  (define-key dired-mode-map (kbd "`") #'dired-flag-backup-files)

  (define-key dired-mode-map (kbd "e") nil)
  (define-key dired-mode-map (kbd "e c") #'dired-do-copy)
  (define-key dired-mode-map (kbd "e d") #'dired-do-delete)
  (define-key dired-mode-map (kbd "e g") #'dired-mark-files-containing-regexp)
  (define-key dired-mode-map (kbd "e h") #'dired-hide-details-mode)
  (define-key dired-mode-map (kbd "e m") #'dired-mark-files-regexp)
  (define-key dired-mode-map (kbd "e n") #'dired-create-directory)
  (define-key dired-mode-map (kbd "e r") #'dired-do-rename)
  (define-key dired-mode-map (kbd "e u") #'dired-unmark-all-marks)

  (require 'dired )
  (add-hook 'dired-mode-hook #'my-dired-init))

[see Ban Key Chords]

Emacs Dired, File Management