config-dired.el (7950B)
1 ;;; config-dired.el -- -*- lexical-binding: t; -*- 2 ;;; Commentary: 3 ;;; Configuration of Dired 4 ;;; Code: 5 6 7 (defmacro vde/dired-fd (name doc prompt &rest flags) 8 "Make commands for selecting 'fd' results with completion. 9 NAME is how the function should be named. DOC is the function's 10 documentation string. PROMPT describes the scope of the query. 11 FLAGS are the command-line arguments passed to the 'fd' 12 executable, each of which is a string." 13 `(defun ,name (&optional arg) 14 ,doc 15 (interactive "P") 16 (let* ((vc (vc-root-dir)) 17 (dir (expand-file-name (if vc vc default-directory))) 18 (regexp (read-regexp 19 (format "%s matching REGEXP in %s: " ,prompt 20 (propertize dir 'face 'bold)))) 21 (names (process-lines "fd" ,@flags regexp dir)) 22 (buf "*FD Dired*")) 23 (if names 24 (if arg 25 (dired (cons (generate-new-buffer-name buf) names)) 26 (icomplete-vertical-do () 27 (find-file 28 (completing-read (format "Items matching %s (%s): " 29 (propertize regexp 'face 'success) 30 (length names)) 31 names nil t))))) 32 (user-error (format "No matches for « %s » in %s" regexp dir))))) 33 34 (use-package dired 35 :unless noninteractive 36 :commands (dired find-name-dired) 37 :bind (("C-c RET" . vde/open-in-external-app) 38 ("C-c f g" . vde/dired-get-size) 39 ("M-s d" . vde/dired-fd-dirs) 40 ("M-s z" . vde/dired-fd-files-and-dirs) 41 ("C-c f f" . find-name-dired) 42 (:map dired-mode-map 43 ("M-p" . vde/dired-up) 44 ("^" . vde/dired-up) 45 ("<backspace>" . vde/dired-up) 46 ("M-n" . vde/dired-down) 47 ("!" . vde/sudired) 48 ("<prior>" . beginend-dired-mode-goto-beginning) 49 ("<next>" . beginend-dired-mode-goto-end))) 50 :config 51 (setq-default dired-auto-revert-buffer t 52 dired-recursive-copies 'always 53 dired-recursive-deletes 'always 54 dired-isearch-filenames 'dwim 55 delete-by-moving-to-trash t 56 dired-listing-switches "-lFaGh1v --group-directories-first" 57 dired-ls-F-marks-symlinks t 58 dired-dwim-target t) 59 (when (string= system-type "darwin") 60 (setq dired-use-ls-dired t 61 insert-directory-program "/usr/local/bin/gls")) 62 63 ;; Enable dired-find-alternate-file 64 (put 'dired-find-alternate-file 'disabled nil) 65 66 ;; Handle long file names 67 (add-hook 'dired-mode-hook #'toggle-truncate-lines) 68 (add-hook 'dired-mode-hook #'dired-hide-details-mode) 69 70 (defun vde/dired-up () 71 "Go to previous directory." 72 (interactive) 73 (find-alternate-file "..")) 74 75 (defun vde/dired-down () 76 "Enter directory." 77 (interactive) 78 (dired-find-alternate-file)) 79 80 (defun vde/open-in-external-app () 81 "Open the file(s) at point with an external application." 82 (interactive) 83 (let* ((file-list 84 (dired-get-marked-files))) 85 (mapc 86 (lambda (file-path) 87 (let ((process-connection-type nil)) 88 (start-process "" nil "xdg-open" file-path))) file-list))) 89 90 (defun find-file-reuse-dir-buffer () 91 "Like `dired-find-file', but reuse Dired buffers." 92 (interactive) 93 (set-buffer-modified-p nil) 94 (let ((file (dired-get-file-for-visit))) 95 (if (file-directory-p file) 96 (find-alternate-file file) 97 (find-file file)))) 98 99 (defun vde/sudired () 100 "Open directory with sudo in Dired." 101 (interactive) 102 (require 'tramp) 103 (let ((dir (expand-file-name default-directory))) 104 (if (string-match "^/sudo:" dir) 105 (user-error "Already in sudo") 106 (dired (concat "/sudo::" dir))))) 107 108 (defun vde/dired-get-size () 109 "Quick and easy way to get file size in Dired." 110 (interactive) 111 (let ((files (dired-get-marked-files))) 112 (with-temp-buffer 113 (apply 'call-process "du" nil t nil "-sch" files) 114 (message 115 "Size of all marked files: %s" 116 (progn 117 (re-search-backward "\\(^[0-9.,]+[A-Za-z]+\\).*total$") 118 (match-string 1)))))) 119 120 (vde/dired-fd 121 vde/dired-fd-dirs 122 "Search for directories in VC root or PWD. 123 With \\[universal-argument] put the results in a `dired' buffer. 124 This relies on the external 'fd' executable." 125 "Subdirectories" 126 "-i" "-H" "-a" "-t" "d" "-c" "never") 127 128 (vde/dired-fd 129 vde/dired-fd-files-and-dirs 130 "Search for files and directories in VC root or PWD. 131 With \\[universal-argument] put the results in a `dired' buffer. 132 This relies on the external 'fd' executable." 133 "Files and dirs" 134 "-i" "-H" "-a" "-t" "d" "-t" "f" "-c" "never") 135 ) 136 137 (use-package find-dired 138 :after dired 139 :commands (find-name-dired) 140 :config 141 (setq-default find-ls-option ;; applies to `find-name-dired' 142 '("-ls" . "-AFhlv --group-directories-first") 143 find-name-arg "-iname")) 144 145 (use-package dired-x 146 :after dired 147 :bind (("C-x C-j" . dired-jump)) 148 :commands (dired-jump dired-omit-mode) 149 :config 150 (setq-default dired-omit-files (concat dired-omit-files "\\|^\\.+$\\|^\\..+$") 151 dired-omit-verbose nil 152 dired-clean-confirm-killing-deleted-buffers nil)) 153 154 (use-package dired-aux 155 :unless noninteractive 156 :after dired 157 :config 158 (setq-default 159 ;; Ask for creation of missing directories when copying/moving 160 dired-create-destination-dirs 'ask 161 dired-create-destination-dirs-on-trailing-dirsep t 162 ;; Search only file names when point is on a file name 163 dired-isearch-filenames'dwim)) 164 165 ;; (use-package dired-collapse 166 ;; :unless noninteractive 167 ;; :commands (dired-collapse-mode) 168 ;; :hook (dired-mode . dired-collapse-mode)) 169 170 (use-package async) 171 (use-package dired-async 172 :unless noninteractive 173 :after (dired async) 174 :commands (dired-async-mode) 175 :hook (dired-mode . dired-async-mode)) 176 177 (use-package dired-narrow 178 :unless noninteractive 179 :after dired 180 :commands (dired-narrow) 181 :bind (:map dired-mode-map 182 ("M-s n" . dired-narrow)) 183 :config 184 (setq-default dired-narrow-exit-when-one-left t 185 dired-narrow-enable-blinking t 186 dired-narrow-blink-time 0.3)) 187 188 (use-package wdired 189 :unless noninteractive 190 :after dired 191 :commands (wdired-mode 192 wdired-change-to-wdired-mode) 193 :config 194 (setq-default wdired-allow-to-change-permissions t 195 wdired-create-parent-directories t)) 196 197 (use-package dired-rsync 198 :unless noninteractive 199 :after dired 200 :commands (dired-rsync) 201 :bind (:map dired-mode-map 202 ("r" . dired-rsync))) 203 204 ;; (use-package diredfl 205 ;; :unless noninteractive 206 ;; :commands (diredfl-mode) 207 ;; :config 208 ;; (setq diredfl-ignore-compressed-flag nil) 209 ;; :hook (dired-mode . diredfl-mode)) 210 211 (use-package trashed 212 :unless noninteractive 213 :commands (trashed) 214 :config 215 (setq trashed-action-confirmer 'y-or-n-p) 216 (setq trashed-use-header-line t) 217 (setq trashed-sort-key '("Date deleted" . t)) 218 (setq trashed-date-format "%Y-%m-%d %H:%M:%S")) 219 220 ;; (use-package dired-sidebar 221 ;; :unless noninteractive 222 ;; :commands (dired-sidebar-toggle-sidebar) 223 ;; :bind ("C-c C-n" . dired-sidebar-toggle-sidebar) 224 ;; :init 225 ;; (add-hook 'dired-sidebar-mode-hook 226 ;; (lambda () 227 ;; (unless (file-remote-p default-directory) 228 ;; (auto-revert-mode)))) 229 ;; :config 230 ;; (push 'toggle-window-split dired-sidebar-toggle-hidden-commands) 231 ;; (push 'rotate-windows dired-sidebar-toggle-hidden-commands) 232 ;; 233 ;; ;; (setq dired-sidebar-subtree-line-prefix "__") 234 ;; ;;(setq dired-sidebar-use-custom-font t) 235 ;; (setq dired-sidebar-theme 'arrow) 236 ;; (setq dired-sidebar-use-term-integration t)) 237 238 (use-package casual-dired 239 :bind (:map dired-mode-map ("C-o" . 'casual-dired-tmenu))) 240 241 (provide 'config-dired) 242 ;; config-dired.el ends here