5.2 Using Dired, the Directory Editor
Dired is one of the most interesting features of Emacs. With Dired, you can look at a listing of all the files in a directory, delete them, rename them, copy them, and perform almost all basic file operations. More important, Dired can make you more productive. For example, you can work with groups of files, deleting, moving, compressing, or even query-replacing strings in them.
There are several ways to start directory editing. If you're not in Emacs, invoke Emacs with a directory name as an argument, for example:
% emacs literature
Emacs starts up editing the directory literature: you'll see a single window that contains a listing of the literature directory. You can also start the directory editor by using C-x C-f (or any other command for visiting a file) and naming a directory, rather than a file. For example, typing C-x C-f literature gets you ready to edit the literature directory. Typing C-x d (for dired) or selecting the folder icon on the toolbar also starts Dired; you then specify a directory name. Finally, dragging a folder onto the Emacs window also starts Dired.[26]
No matter how you start the editor, the result is the same.
Type: C-x C-f literature Enter
A basic directory editor display.
As you can see, Dired's display is similar to what you see if you type ls -l at a Unix shell prompt. The permissions associated with the file, the owner, the group name, the size of the file, and the date last modified all precede the filename. All files and directories are listed, including those whose names start with a dot. The cursor starts out on a filename, rather than in the first column.
Also, if your display supports colors (unfortunately this book doesn't), you'll see that directories are blue, backup and auto-save files are tan, and symbolic links are purple. Colors are a function of font-lock mode. If you don't see colors in your directory listing, type M-x font-lock-mode Enter or add the following line to your .emacs file:
(global-font-lock-mode t)
By default, the list is sorted by filename, but you can sort it by date instead. Look at the mode line. It says (Dired by name). To change the order of the display, type s (for dired-sort-toggle-or-edit). This command puts the newest files at the top of the list, solving the "Where's that file I worked on yesterday?" problem quite easily. The mode line says (Dired by date). Typing s again toggles the sort, putting it back in alphabetical order.
If you remember the commands used to edit the buffer list (from Chapter 4), you will find that they are almost identical to the directory editor commands. You can do many additional things, but the basic commands are the same.
Remember, in the directory editor you are working directly with files, not with buffers. When you delete a file using Dired, it's gone permanently.
There are several ways to move around in Dired. The commands Space, C-n, and n all move you to the next file in the list. Del, C-p, and p all move you to the previous file. Arrow keys and PgUp and PgDown work as well. You can also use any of the search commands (incremental search, word search, and so on) to find a particular file.
5.2.1 Viewing and Editing Files
When you look at a directory listing, you may want to get a quick look at the files. Dired's v command does just this: put the cursor on the file you want to view and press v (for dired-view-file). Emacs displays the file in view mode.[27] This is a read-only mode, so you can't modify the file. Press C-c or q to return to the directory listing. While you're viewing the file, you can use s to start an incremental search, or press Enter to scroll the display down one line. Typing = tells you what line the cursor is on. There are a number of shortcuts for other Emacs commands (like marking text), but frankly, the regular commands work correctly. There's no reason to remember a special set of commands when the ones you already know work.
26
The one exception to this is running Emacs in the Mac OS X Terminal application, which has its own drag-and-drop behavior. In the terminal—and thus in Emacs running in the terminal window—dragging and dropping a folder inserts the complete pathname of that folder rather than opening the folder in Dired.
27
What if it's a file that shouldn't be viewed in Emacs, like a JPG or a PDF? In this case, the variable dired-view-command-alist associates viewers with file extensions. The defaults for this command work on Linux, but require some tweaking on other platforms. See Chapter 10 for an example of using Custom to change this variable for Mac OS X and Windows.