2008-11-20

2008-11-18

I too want to write about Dired...

Inspired by this blog post by Greg Newman about Dired, Emacs' Directory Editor, I wanted to add a few tips myself - I love Dired! Most, if not all, of what I will write about is of course already there in the excellent manual, but it seems that some are as lazy as me and does not always read manuals, so... :)

First a couple of comments to the post Greg did:

Starting Dired

Starting Dired can be done from `C-x C-f' (find-file) as well. Just type a directory name instead of a file name. However, starting it using `C-x d' has certain advantages: you can, for example, enter a file name pattern and the listing will then be limited to that, even if you type `g' to refresh the listing. This can be pretty handy. Also, if you like you can get a recursive listing, a really powerful tool. To do that, use a prefix argument, `C-u', before you execute `C-x d'. You will be prompted for switches to ls. Add a `R' to the existing prompt and type `RET'. Voila! All the power of Dired to a whole directory tree.

Navigation

You can navigate as usual with `C-n' and `C-p' but also with the more convenient `n' and `p'.

Copying and renaming

When copying or renaming you can type arrow down or `M-n' to get the current file's name at the prompt. This is really handy when renaming files. It also works from normal buffers if you use `M-x rename-file' or similar.

You can change the case of file names using the commands `% u' (`dired-upcase') and `% l' (`dired-downcase'). I use it all the time when I have copied photos from my digital camera (I hate those uppercase names, especially the file extensions).

Marking

The basics

At first glance, the concept of marking files in Dired does not seem very special. You mark files with `m' and unmark then with `u' and marked files can be operated on in various ways. I would like to present some powerful tools. First you should learn about the kill command, by default bound to `k', which does not really kill/delete any file, it just removes the file from the listing. You can get it back with the normal `undo' command (available on `C-x u' and other key bindings). Then there is `t' for toggling the marks. Marked files become unmarked and unmarked becomes marked.

Marking by file name or content, and more cool tricks

You can also mark files by matching a regexp against the file name, with `% m', and against the content of the file with `% g'. Together with kill and toggle, these are really powerful tools. Let's say I want to search through all java files in a certain directory for the pattern `'PATTERN and get a listing of only those, after which I want to do some operations (copy, rename, whatever). First I open the directory of interest and do `% m \.java$ RET'. This will mark all files with the .java extension. Next I will kill the rest by first toggling the marks with `t', and then killing them with `k'. Next, use `% g PATTERN RET' to mark only those files that contain PATTERN. When the search is done I do the toggle-kill combo again, and Voila!, I see only the files I want and can do whatever I want to do with them. If you would mark or unmark files or kill files from the listing by mistake, just use undo. Simple as that.

These features alone make Emacs worth learning, at least if you do a lot of file juggling like I do.

The end?

I think this has to do for now. One last thing though: `wdired'. It lets you edit file names as if they were normal text in a buffer, with all the possibilities that gives you with search/replace, rectangle commands etc. It's under the Immediate menu in Dired, under the name `Edit File Names'. Edit the file names and save with `C-x C-s'. Crazily handy for cleaning your mp3-collection :)

I recommend everyone to check out the menus that becomes visible when using Dired. There are a lot of hiddens gems up there. And, of course, read the manual section for Dired for more crazy marking and stuff. Try out `M-x find-dired' some time as well.

Okay okay okay, I said I would stop, and I will. Now.

Thanks for listening!