Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bravosierrasierra/6b6d11e8abcf3214ef669fe347139508 to your computer and use it in GitHub Desktop.
Save bravosierrasierra/6b6d11e8abcf3214ef669fe347139508 to your computer and use it in GitHub Desktop.
dired
** dired
*** base settings
#+begin_src emacs-lisp
(put 'dired-find-alternate-file 'disabled nil)
;; если открыт другой буфер с dired, то по-умолчанию в пути подставлять его каталог
(setq dired-dwim-target t)
(if (eq system-type 'windows-nt) ;OR gnu/linux
(setq dired-listing-switches "-lah")
(setq dired-listing-switches "-lah --group-directories-first"))
(if (eq system-type 'darwin)
(setq insert-directory-program "/usr/local/bin/gls"))
;; (when (eq system-type 'darwin)
;; (use-package ls-lisp)
;; (setq ls-lisp-use-insert-directory-program nil))
;; windows settings See (customize-group 'ls-lisp)
(setq ls-lisp-dirs-first t)
(setq ls-lisp-ignore-case t)
; hide the link count, user, and group columns - default is '(links uid gid)
(setq ls-lisp-verbosity '(links))
; use ISO dates (the first is for recent dates, second for old dates)
(setq ls-lisp-format-time-list '("%Y-%m-%d %H:%M" "%Y-%m-%d %H:%M"))
(setq ls-lisp-use-localized-time-format t)
;; Always copy/delete recursively
(setq dired-recursive-copies (quote always))
(setq dired-recursive-deletes (quote top))
;; Auto refresh dired, but be quiet about it
(setq global-auto-revert-non-file-buffers t)
(setq auto-revert-verbose nil)
(define-key dired-mode-map (kbd "q") 'kill-this-buffer)
(define-key dired-mode-map (kbd "K") 'dired-do-kill-lines)
(define-key dired-mode-map (kbd "k") 'dired-previous-line)
(define-key dired-mode-map (kbd "J") 'dired-goto-file)
(define-key dired-mode-map (kbd "j") 'dired-next-line)
(define-key dired-mode-map (kbd "M-p") 'ace-window)
(define-key dired-mode-map (kbd "M-q") 'ace-window)
(define-key dired-mode-map (kbd "/") 'helm-occur)
(define-key dired-mode-map (kbd "b") 'custom/eww-visit-file)
;; (define-key dired-mode-map (kbd "C-j") 'dired-find-alternate-file)
(define-key dired-mode-map (kbd "C-j") 'dired-find-file)
(define-key dired-mode-map (kbd "C-d") 'evil-scroll-page-down)
;; (define-key dired-mode-map (kbd "C-u") 'evil-scroll-page-up)
(define-key dired-mode-map (kbd "C-b") 'evil-scroll-page-up)
(define-key package-menu-mode-map (kbd "h") 'left-char)
(define-key package-menu-mode-map (kbd "j") 'next-line)
(define-key package-menu-mode-map (kbd "k") 'previous-line)
(define-key package-menu-mode-map (kbd "l") 'right-char)
;; Better M-< and M->
(defun dired-back-to-top ()
(interactive)
(beginning-of-buffer)
(dired-next-line 2))
(define-key dired-mode-map
(vector 'remap 'beginning-of-buffer) 'dired-back-to-top)
(defun dired-jump-to-bottom ()
(interactive)
(end-of-buffer)
(dired-next-line -1))
(define-key dired-mode-map
(vector 'remap 'end-of-buffer) 'dired-jump-to-bottom)
;; (eval-after-load 'tramp
;; '(progn
;; ;; Allow to use: /sudo:user@host:/path/to/file
;; (add-to-list 'tramp-default-proxies-alist
;; '(".*" "\\`.+\\'" "/ssh:%h:"))))
#+end_src
*** dired details (disabled)
[2017-03-23 Thu 16:57]
#+begin_src emacs-lisp
;; (use-package dired-details)
;; (dired-details-install)
;; ;;(setq dired-details-hidden-string " [\u25bc] ")
;; (setq dired-details-hidden-string "[…] ")
#+end_src
*** dired+
[2017-03-23 Thu 16:56]
http://www.emacswiki.org/emacs/DiredPlus
#+begin_src emacs-lisp
(use-package dired+
:ensure t
:init
(progn
;; Details toggling is bound to "(" in `dired-mode' by default
(setq diredp-hide-details-initially-flag nil))
:config
(setq dired-omit-verbose nil)
;; (setq diredp-hide-details-initially-flag nil)
;; (setq diredp-hide-details-propagate-flag nil)
)
#+end_src
*** dired-x
[2017-03-23 Thu 16:57]
#+begin_src emacs-lisp
;; включаем omit-mode по-умолчанию, т.е. скрываем по-умолчанию файлы .bashrc и т.д.
;; отключается в меню или по C-x M-o
;; http://www.emacswiki.org/emacs/DiredOmitMode
;;(setq dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$") ;;default
(require 'dired-x)
(setq-default dired-omit-files-p t) ; Buffer-local variable
;; (setq dired-omit-files "^\\.?#\\|^\\.$\\|^\\.[^.].+$")
(setq dired-omit-files "^\\.?#\\|^\\.$\\|^\\.[^.].+$\\|^.DS_Store$\\|^__MACOSX$")
(add-hook 'dired-mode-hook (lambda () (dired-omit-mode)))
#+end_src
*** dired async mode (disabled)
[2017-03-23 Thu 16:53]
#+begin_src emacs-lisp
;; (autoload 'dired-async-mode "dired-async.el" nil t)
;; (dired-async-mode nil)
#+end_src
*** dired get selected items size via "?" key
[2017-03-23 Thu 16:52]
#+begin_src emacs-lisp
;; get directory-size
(defun dired-get-size ()
(interactive)
(let ((files (dired-get-marked-files)))
(with-temp-buffer
(if (eq system-type 'darwin)
(apply 'call-process "gdu" nil t nil "-sch" files)
(apply 'call-process "/usr/bin/du" nil t nil "-sch" files))
(message "Size of all marked files: %s"
(progn
(re-search-backward "\\(^[0-9.,]+[A-Za-z]+\\).*total$")
(match-string 1))))))
(define-key dired-mode-map (kbd "?") 'dired-get-size)
#+end_src
*** dired recent dirs via C-x C-d
[2017-03-23 Thu 16:51]
#+begin_src emacs-lisp
;; open recent directory, requires ivy (part of swiper)
;; borrows from http://stackoverflow.com/questions/23328037/in-emacs-how-to-maintain-a-list-of-recent-directories
(defun bjm/ivy-dired-recent-dirs ()
"Present a list of recently used directories and open the selected one in dired"
(interactive)
(let ((recent-dirs
(delete-dups
(mapcar (lambda (file)
(if (file-directory-p file) file (file-name-directory file)))
recentf-list))))
(let ((dir (ivy-read "Directory: "
recent-dirs
:re-builder #'ivy--regex
:sort nil
:initial-input nil)))
(dired dir))))
(global-set-key (kbd "C-x C-d") 'bjm/ivy-dired-recent-dirs)
#+end_src
*** dired open file in external app via "C-return"
[2017-03-23 Thu 16:51]
#+begin_src emacs-lisp
;;----------------------------------------
;;Open file by C-return in external application
(defun ergoemacs-open-in-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?") ) )
(when doIt
(cond
((string-equal system-type "windows-nt")
(mapc (lambda (fPath) (w32-shell-execute "open" (replace-regexp-in-string "/" "\\" fPath t t)) ) myFileList)
)
((string-equal system-type "darwin")
(mapc (lambda (fPath) (shell-command (format "open \"%s\"" fPath)) ) myFileList) )
((string-equal system-type "gnu/linux")
(mapc (lambda (fPath) (let ((process-connection-type nil)) (start-process "" nil "xdg-open" fPath)) ) myFileList) ) ) ) ) )
(define-key dired-mode-map (kbd "<C-return>") 'ergoemacs-open-in-external-app)
;;----------------------------------------
#+end_src
*** dired sort menu
[2017-03-23 Thu 16:50]
#+begin_src emacs-lisp
;;Sort settings
(use-package dired-sort-menu+)
(add-hook 'dired-load-hook
(lambda () (use-package dired-sort-menu)))
#+end_src
*** dired shell instead dired via "M-return"
[2017-03-23 Thu 16:49]
#+begin_src emacs-lisp
;; open eterm for current directory by pressing M-return
(defun shell-instead-dired ()
(interactive)
(let ((dired-buffer (current-buffer)))
;; (eshell)
(shell (concat default-directory "-shell"))
;; ;; (kill-buffer dired-buffer) ;; remove this line if you don't want to kill the dired buffer
(delete-other-windows)
))
(define-key dired-mode-map (kbd "<M-return>") 'shell-instead-dired)
#+end_src
*** dired sudo toggle
[2017-03-23 Thu 16:48]
#+begin_src emacs-lisp
;; Allow to switch from current user to sudo when browsind `dired' buffers.
;; To activate and swit with "C-c C-s" just put in your .emacs:
(use-package dired-toggle-sudo)
(define-key dired-mode-map (kbd "C-c C-s") 'dired-toggle-sudo)
#+end_src
*** dired create new file via 'N' key
[2017-03-23 Thu 16:47]
#+begin_src emacs-lisp
;;------------------------------
;;
;; для создания нового файла из dired нажимаем "N" и вводем имя. каталоги в имени вроде foo/bar/filename будут созданы, если их нет
(eval-after-load 'dired
'(progn
(defun my-dired-create-file (file)
"Create a file called FILE. If FILE already exists, signal an error."
(interactive
(list (read-file-name "Create file: " (dired-current-directory))))
(let* ((expanded (expand-file-name file))
(try expanded)
(dir (directory-file-name (file-name-directory expanded)))
new)
(if (file-exists-p expanded)
(error "Cannot create file %s: file exists" expanded))
;; Find the topmost nonexistent parent dir (variable `new')
(while (and try (not (file-exists-p try)) (not (equal new try)))
(setq new try
try (directory-file-name (file-name-directory try))))
(when (not (file-exists-p dir))
(make-directory dir t))
(write-region "" nil expanded t)
(when new
(dired-add-file new)
(dired-move-to-filename))))
(define-key dired-mode-map (kbd "N") 'my-dired-create-file)))
#+end_src
*** dired-subtree via TAB
[2017-03-23 Thu 16:47]
#+begin_src emacs-lisp
;; show directory subtree by tab/shift-tab
(use-package dired-subtree)
(define-key dired-mode-map (kbd "<tab>") 'dired-subtree-toggle)
(define-key dired-mode-map (kbd "<backtab>") 'dired-subtree-cycle)
#+end_src
*** dired-archive
[2017-03-23 Thu 16:46]
#+begin_src emacs-lisp
;;----------------------------------------
;; http://stackoverflow.com/questions/10226836/how-to-tar-and-compress-marked-files-in-emacs
;; Работа с архивами: просто копируем файлы в архив, например
;; You can also archive files just by marking and copying them to an archive file.
;; For example, mark several files in dired, and select m-x dired-do-copy.
;; When prompted for destination, type test.zip. The files will be added to the zip archive automatically.
;; You can also uncompress files by selecting them in dired and running the command dired-do-extract
;; To set this up, look at the following variables: dired-to-archive-copy-alist dired-extract-alist
;; Here's my setup, which has served me for many years...
;; ;; dired-a provides support functions, including archiving, for dired
;; (load "dired-a")
;; Alist with information how to add files to an archive (from dired-a)
;; Each element has the form (REGEXP ADD-CMD NEW-CMD). If REGEXP matches
;; the file name of a target, that target is an archive and ADD-CMD is a command
;; that adds to an existing archive and NEW-CMD is a command that makes a new
;; archive (overwriting an old one if it exists). ADD-CMD and NEW-CMD are:
;; 1. Nil (meaning we cannot do this for this type of archive) (one of
;; ADD-CMD and NEW-CMD must be non-nil).
;; 2. A symbol that must be a function e.g. dired-do-archive-op.
;; 3. A format string with two arguments, the source files concatenated into
;; a space separated string and the target archive.
;; 4. A list of strings, the command and its flags, to which the target and
;; the source-files are concatenated."
(setq dired-to-archive-copy-alist
'(("\\.sh\\(ar\\|[0-9]\\)*$" nil "shar %s > %s")
("\\.jar$" ("jar" "uvf") ("jar" "cvf"))
("\\.tar$" ("tar" "-uf") ("tar" "-cf"))
("\\.tgz$\\|\\.tar\\.g?[zZ]$" ("tar" "-uf %s" "|" "gzip > %s") ("tar" "-czvf"))
("\\.ear$" ("zip" "-qr") ("zip" "-qr"))
; ("\\.rar$" ("rar" "a") ("rar" "a"))
("\\.war$" ("zip" "-qr") ("zip" "-qr"))
("\\.zip$" ("zip" "-qr") ("zip" "-qr"))
("\\.wmz$" ("zip" "-qr") ("zip" "-qr")) ;; for media player skins
("\\.arc$" ("arc" "a") nil)
("\\.zoo$" ("zoo" "aP") nil)
))
;; use pkzip with manipulating zip files (t) from within dired (use zip
;; and unzip otherwise)
(setq archive-zip-use-pkzip nil)
;; add these file types to archive mode to allow viewing and changing
;; their contents
(add-to-list 'auto-mode-alist '("\\.[ejrw]ar$\\'" . archive-mode))
;; modify the dired-extract switches to use the directory
;; ~/download/tryout as the default extract directory for zip files
(defconst MY_TRYOUT_DIR "~/downloads/tryout"
"Directory for extracting files")
(setq dired-extract-alist
`(
("\\.u\\(ue\\|aa\\)$" . dired-uud)
("\\.jar$" . "jar -xvf %s")
("\\.tar$" . ,(concat "tar -xf %s -C " MY_TRYOUT_DIR))
("\\.tgz$\\|\\.tar\\.g?[zZ]$" . ,(concat "tar -xzf %s -C " MY_TRYOUT_DIR))
("\\.arc$" . "arc x %s ")
("\\.bz2$" . ,(concat "bunzip2 -q %s"))
("\\.rar$" . ,(concat "unrar x %s " MY_TRYOUT_DIR "\\"))
("\\.zip$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
("\\.ear$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
("\\.war$" . ,(concat "unzip -qq -Ux %s -d " MY_TRYOUT_DIR))
("\\.zoo$" . "zoo x. %s ")
("\\.lzh$" . "lha x %s ")
("\\.7z$" . "7z e %s ")
("\\.g?[zZ]$" . "gzip -d %s") ; There is only one file
))
#+end_src
*** dired-ediff
[2017-03-23 Thu 16:46]
#+begin_src emacs-lisp
; ----------------------------------------
;; dired ediff selected files by = key
; ----------------------------------------
;; http://stackoverflow.com/questions/18121808/emacs-ediff-marked-files-in-different-dired-buffers
;; works for files marked in the same dired buffer and also for files in different buffers. In
;; addition to working on 2 marked files potentially across dired buffers, it handles the case when
;; there are 0 or 1 marked files. 0 marked files will use the file under the cursor as file A, and
;; prompt for a file to compare with. 1 marked files will use the marked file as file A, and prompt
;; for a file to compare with. The file under the point is used as the default in the prompt. I
;; bound this to =
;;ediff-split-window-function ;; http://www.gnu.org/software/emacs/manual/html_node/ediff/Miscellaneous.html
(defun mkm/ediff-marked-pair ()
"Run ediff-files on a pair of files marked in dired buffer"
(interactive)
(let* ((marked-files (dired-get-marked-files nil nil))
(other-win (get-window-with-predicate
(lambda (window)
(with-current-buffer (window-buffer window)
(and (not (eq window (selected-window)))
(eq major-mode 'dired-mode))))))
(other-marked-files (and other-win
(with-current-buffer (window-buffer other-win)
(dired-get-marked-files nil)))))
(cond ((= (length marked-files) 2)
(ediff-files (nth 0 marked-files)
(nth 1 marked-files)))
((and (= (length marked-files) 1)
(= (length other-marked-files) 1))
(ediff-files (nth 0 marked-files)
(nth 0 other-marked-files)))
((= (length marked-files) 1)
(let ((single-file (nth 0 marked-files)))
(ediff-files single-file
(read-file-name
(format "Diff %s with: " single-file)
nil (m (if (string= single-file (dired-get-filename))
nil
(dired-get-filename))) t))))
(t (error "mark no more than 2 files")))))
(define-key dired-mode-map "=" 'mkm/ediff-marked-pair)
#+end_src
*** dired bf-mode (disabled)
[2017-03-23 Thu 16:45]
#+begin_src emacs-lisp
;;----------------------------------------
;; bf-mode
;; Usage:
;; 1. invoke dired by C-x d.
;; 2. b enter bf-mode
;; 3. b or q exit from bf-mode
;; 4. n or p move cursor to target file in order to browse it.
;; 5. SPC scroll up browsing window
;; S-SPC scroll down browsing window
;; 6. r toggle read-only
;; j toggle browsing alternatively (html, archive and more)
;; s adjust browsable file size
(use-package bf-mode
:disabled
:ensure t
:config
;; list up file extensions which should be excepted
(setq bf-mode-except-exts
(append '("\\.dump$" "\\.data$" "\\.mp3$" "\\.lnk$" "\\.avi$" "\\.mov$" )
bf-mode-except-exts))
;; list up file extensions which should be forced browsing
;; (setq bf-mode-force-browse-exts
;; (append '("\\.txt$" "\\.and.more...")
;; bf-mode-force-browse-exts))
;; browsable file size maximum
(setq bf-mode-browsing-size 50) ;; 50 killo bytes
;; browsing htmls with w3m (needs emacs-w3m.el and w3m)
(setq bf-mode-html-with-w3m t)
;; browsing archive file (contents listing) verbosely
(setq bf-mode-archive-list-verbose t)
;; browing directory (file listing) verbosely
(setq bf-mode-directory-list-verbose t)
;; start bf-mode immediately after starting dired
(setq bf-mode-enable-at-starting-dired t)
;; quitting dired directly from bf-mode
(setq bf-mode-directly-quit t)
)
#+end_src
*** dired follow-mode (peep-dired)
[2017-05-01 Mon 22:15]
https://github.com/asok/peep-dired
#+begin_src emacs-lisp
(use-package peep-dired
:ensure t
:config
(setq peep-dired-cleanup-on-disable t)
;; (setq peep-dired-cleanup-eagerly t)
;; (evil-define-key 'normal peep-dired-mode-map (kbd "<SPC>") 'peep-dired-scroll-page-down
;; (kbd "C-<SPC>") 'peep-dired-scroll-page-up
;; (kbd "<backspace>") 'peep-dired-scroll-page-up
;; (kbd "j") 'peep-dired-next-file
;; (kbd "k") 'peep-dired-prev-file)
;; (add-hook 'peep-dired-hook 'evil-normalize-keymaps)
(define-key peep-dired-mode-map (kbd "j") 'peep-dired-next-file)
(define-key peep-dired-mode-map (kbd "k") 'peep-dired-prev-file)
(setq peep-dired-ignored-extensions '("mkv" "iso" "mp4" "mp3" "ogg"))
:defer t ; don't access `dired-mode-map' until `peep-dired' is loaded
:bind (:map dired-mode-map
("P" . peep-dired))
)
#+end_src
*** dired open with
[2017-03-23 Thu 16:41]
#+begin_src emacs-lisp
(defun add-dired-extension-list (command extension-list)
(mapc (lambda (extension)
(add-to-list 'dired-guess-shell-alist-user (list extension command)))
extension-list))
(add-hook 'dired-load-hook
(function (lambda ()
(load "dired-x")
(add-dired-extension-list "mplayer -really-quiet --" (make-regex-file-extension-list (make-file-part-extensions '("avi" "mkv" "mpg" "mpeg" "mp4" "flv" "wmv" "rm" "asf" "wav" "VOB" "ram" "divx" "mov" "ogv" "flc" "swf"))))
(add-dired-extension-list "xterm -e mplayer --" (make-regex-file-extension-list (make-file-part-extensions '("spx" "flac" "mp3" "ogg"))))
;; (add-to-list 'dired-guess-shell-alist-user (list "\\.cbr$" "comix --"))
;; (add-dired-extension-list "evince --" (make-regex-file-extension-list (make-file-part-extensions '("pdf" "ps"))))
;; (add-to-list 'dired-guess-shell-alist-user (list "\\.pdf$" "evince --"))
;; (add-to-list 'dired-guess-shell-alist-user (list "\\.cbz$" "comix --"))
)))
#+end_src
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment