Emacs Init: Dired Config

By Xah Lee. Date: . Last updated: .

Tips for setting up 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:

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

Open All Marked Files

to open all marked files, do

Ctrl+u Alt+x dired-do-find-marked-files

require dired-x.

(require 'dired-x)
;; for dired-do-find-marked-files

Allow Delete Directory, Copy 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 Window

Same for rename R and others.

For example:

  1. Alt+x dired
  2. Alt+x split-window-below
  3. navigate to a target dir.
  4. press C to copy, the other dir in the split pane will be default destination.

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
dired-hide-details-mode

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 )

(when (>= emacs-major-version 28)
  (setq dired-kill-when-opening-new-dired-buffer t))

(when (< emacs-major-version 28)
  (progn
    (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
    ))

Show Human Readable File Size

(setq dired-listing-switches "-alh")
;; default is "-al"

Make Dired Use Easier Keys

Make keys easier to type. Make all keys a key sequence sans modifier key.

You can move other frequently dired commands you use that have difficult key, to a leader key sequence.

(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"
  (interactive)

  (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)
  ;;
  )

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

〔see Ban Key Chords

Dired on MS Windows Config

Show Dir First

(setq ls-lisp-dirs-first t)
;; default is nil

Ignore Case Order

(setq ls-lisp-ignore-case t)
;; default is nil

Reference

Emacs Dired, File Management