一些讓 dired 更好用的設定。
開啓目前編輯中檔案所屬目錄
使用命令 M-x dired-jump 或快捷:C-x C-j
用外部程式開啓檔案
一般 dired-mode 的檔案按下後是以 emacs 自己打開。如果希望透過外部程式打開,可以考慮使用一下 elisp code 來改造 dired (適用 linux)
(defun external-app ()
"Open the current file or dired marked files in external app."
(interactive)
(let ( doIt
(myFileList
(cond
((string-equal major-mode "dired-mode") (dired-get-marked-files))
(t (list (buffer-file-name))))))
(setq doIt (if (<= (length myFileList) 5)
t
(y-or-n-p "Open more than 5 files?")))
(mapc (lambda (fPath) (let ((process-connection-type nil))
(start-process "" nil "xdg-open" fPath)))
myFileList)))
解壓縮
雖然目的不一樣,不過結構是差不多的。或者,使用 Z 壓縮、反壓縮檔案,資料夾。(要把多個檔案壓縮,需先弄成資料夾哦!)
; zip files
(defun 2zip ()
(interactive)
(require 'dired)
(let ( (fileName (elt (dired-get-marked-files) 0)))
(shell-command (format "zip -r '%s.zip' '%s'"
(file-relative-name fileName)
(file-relative-name fileName)))))
;; untar (by Yu-Fu Wu <wennynnewenny@gmail.com>)
(defun untar ()
(interactive)
(require 'dired)
(let ( (fileName (elt (dired-get-marked-files) 0)))
(shell-command (format "tar xzvf '%s'"
(file-relative-name fileName)))))
刪除資料夾
資料夾需要遞迴操作與確認,透過下面這個 elisp,自動完成任務:
(setq dired-recursive-copies (quote always))
(setq dired-recursive-deletes (quote top))
用外部檔案管理員開啓資料夾
(defun open-in-folder ()
"Show current file in desktop (OS's file manager)."
(interactive)
(let ((process-connection-type nil)) (start-process "" nil "xdg-open" ".")))
dired+-mode
請先透過 el-get 或 package.el 安裝,似乎會自己掛鉤,不用再設定。提供下列能力(節錄,其他請上 dired-plus 網站):