2015-04-25 Some Dired goodies

Most Emacs users know that Dired – while atypical – is a perfectly reasonable file manager, with quite a few completely unique features. It is lacking, however, in a few rather fundamental areas – at least with its default configuration. Let me share here a bunch of Dired tweaks.

First of all, ! (dired-do-shell-command) does not know (by default) how to handle some filetypes. It simple to remedy this: for instance, this is what I have in my init.el.

(setq dired-guess-shell-alist-user
      '(("\\.pdf\\'" "evince")
	("\\.tex\\'" "pdflatex")
	("\\.ods\\'\\|\\.xlsx?\\'\\|\\.docx?\\'\\|\\.csv\\'" "libreoffice")))

Another thing that did annoy me was the fact that when I traverse the directory hierarchy, I leave a trail of open Dired buffers with all the directories I go through. This is also easy to change: just enable the dired-find-alternate-file function (bound to a in Dired) and use it to visit a file or directory in place (IOW, open it instead of the current Dired buffer – this also works for files I want to visit!).

(put 'dired-find-alternate-file 'disabled nil)

Another nice thing to know is the variable completion-ignored-extensions. It is a set of strings (not regexen!) which are “uninteresting” from completion point of view. (For example, it might be a good idea to put “.o” or “.aux” etc. there.) One might argue that this has little to do with Dired. Enter Dired-X, a built-in library enhancing Dired. One of the features of Dired-X is dired-omit-mode, which turns off displaying of “uninteresting” files. (Remember to set completion-ignored-extensions before you (require 'dired-x)!) You can turn it on and off with C-x M-o in Dired (assuming that you require 'dired-x in your init.el). (Notice: it used to be just M-o in older Emacsen.)

Also, it is handy to know that you can run an external command with ! (synchronously) or & (asynchronously). If you have more than one file marked, then you might want to run the command once for each file, put a question mark (surrounded by space) in place of the filename; the command given will be run once for each of the marked files. You might also want to use an asterisk (also surrounded by whitespace), which will be replaced by the list of all marked files.

This way, we have arrived at an interesting concept of Dired: marking. In traditional file managers, each file can be marked or not. In Dired, situation is more complicated. You can flag a file for deletion (which is denoted by a D to the left of the filename), or mark it (which is denoted by a * to the left of the filename). There are lots of commands to operate on marks (marking files satisfying certain conditions, and operating on marked files) – see Dired’s manual. One interesting thing I wanted to share here is the * c command. It lets the user change the marks – for instance, change all asterisks to capital D’s, so that all marked files are now flagged for deletion! This way, you can have a few categories of marks at the same time. I’m not sure what this can be useful for (apart from the * c * D idiom), but it definitely is cool.

These tips are just, well, the tip of the iceberg. In fact, Dired is quite a remarkable file manager, with some really unique ideas. Try to explore it, and you will not miss your reward! Also, make sure to check out Xah Lee’s Dired hints (also, you might want to follow some links about Dired at the bottom of that post). (Final note: Wdired is one of the best things since sliced bread!)

CategoryEnglish, CategoryBlog, CategoryEmacs