Skip to content

Instantly share code, notes, and snippets.

@ram535
Last active August 12, 2021 04:30
Show Gist options
  • Save ram535/8d7d6dfd11eb56d378de01b0f195798f to your computer and use it in GitHub Desktop.
Save ram535/8d7d6dfd11eb56d378de01b0f195798f to your computer and use it in GitHub Desktop.
Dired configuration. This configuration uses straight.el, general.el, evil.
(use-package dired
:straight (:type built-in)
:hook ((dired-mode . hl-line-mode)
(dired-mode . dired-hide-details-mode))
:custom
(dired-recursive-copies 'always)
(dired-recursive-deletes 'always)
;;use to copy to the next buffer visible
(dired-dwim-target t)
;; Auto refresh Dired, but be quiet about it
(global-auto-revert-non-file-buffers t)
(auto-revert-verbose nil)
:config
;; Enable global auto-revert
(global-auto-revert-mode t)
(setq dired-listing-switches "-Bhl --group-directories-first -v")
(set-face-attribute 'dired-header nil
:foreground "#282c34"
:weight 'bold)
(defcustom list-of-dired-switches
'(("-Bhl --group-directories-first -v" . "")
("-ahl -v --group-directories-first -v" . "everything"))
"List of ls switches (together with a name to display in the mode-line) for dired to cycle among.")
(defun me/cycle-dired-switches ()
"Cycle through the list `list-of-dired-switches' of switches for ls"
(interactive)
(setq list-of-dired-switches
(append (cdr list-of-dired-switches)
(list (car list-of-dired-switches))))
(dired-sort-other (caar list-of-dired-switches))
(setq mode-name (concat "Dired " (cdar list-of-dired-switches)))
(force-mode-line-update))
(defun me/dired-open()
(interactive)
(cond
;; use dired-find-file if it is a directory
((file-directory-p (dired-get-file-for-visit))
(dired-find-file))
;; use dired-find-file if the mime type of the file is emacs.desktop
((string= "emacs.desktop" (string-trim-right (shell-command-to-string
(format "xdg-mime query filetype %s | xargs xdg-mime query default"
(shell-quote-argument (dired-get-file-for-visit))))))
(dired-find-file))
(t
;; use xdg-open for everything else
;; start-process quote the arguments so you do not need the sell-quote-argument function
(start-process "ram-dired-open" nil "xdg-open" (dired-get-file-for-visit)))))
:general
(:states 'normal
:keymaps 'dired-mode-map
"j" 'dired-next-line
"k" 'dired-previous-line
"l" 'me/dired-open
"h" 'dired-up-directory
;; copy file
"yy" 'dired-do-copy
;; copy file name
"yn" 'dired-copy-filename-as-kill
;; copy full paht
"yp" (lambda() (interactive) (dired-copy-filename-as-kill 0))
;; quick access directories
"gk" (lambda() (interactive) (dired "~/Documents"))
"gn" (lambda() (interactive) (dired "~/Documents/notes"))
"gd" (lambda() (interactive) (dired "~/Downloads"))
"gp" (lambda() (interactive) (dired "~/Projects"))
;; mark file or directory
"m" 'dired-mark
"u" 'dired-unmark
"t" 'dired-toggle-marks
;; rename file or directory
"cw" 'dired-do-rename
;; update current directory
"r" 'revert-buffer
;; create new directory
"nd" 'dired-create-directory
;; create new file
"nf" 'dired-create-empty-file
"q" 'quit-window
;; toggle files and directories details
"d" 'dired-hide-details-mode
;; delte file of directory
"D" 'dired-do-delete
"za" 'me/cycle-dired-switches
"q" 'quit-window))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment