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 literaturegets 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 -lat 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 Enteror 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 sagain 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.
Warning
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 nall move you to the next file in the list. Del, C-p, and pall move you to the previous file. Arrow keys and PgUpand PgDownwork 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 vcommand 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-cor qto return to the directory listing. While you're viewing the file, you can use sto start an incremental search, or press Enterto 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.
If you want to edit a file from the Dired buffer, move to the line the file is on and press Enter(a variety of other keystrokes work as well, such as ffor find or efor edit). Emacs finds the file and you can edit it. This is a completely normal editing buffer: you can make any changes you want, save them, visit other files, and so on. Typing C-x bfollowed by the name of the directory you were working in moves you back to the Dired buffer. Or you can use the buffer menu ( C-x C-b) to find and display the Dired buffer.
Viewing and editing files is nice, but you already know how to do that—right? You're waiting for the interesting stuff: how to delete files.
5.2.2 Deleting, Copying, and Renaming Files
As we've said, file deletion is almost identical to buffer deletion with the buffer list. If you learned how to delete buffers, you know the basics of deleting files with Dired. First, you flag a file for deletion by moving to the file's name and typing d. Doing this places a Don the left margin and moves the cursor to the next file in the list. You can flag as many files as you want. You can change your mind at this point and type uto undelete the file. At some later time, you type xto delete the files (more on this in a minute). The following screen shows what the Dired buffer looks like when you flag a few files for deletion.
Type: d d d

Three files flagged for deletion (Windows).
As we mentioned, you can type uat any time to remove the deletion flags from the files. Typing umoves you to the next file in the list, and, if it is marked, unmarks it. You can also use Delto unmark. This command undeletes the previous file in the list and then moves up one line.
Because Emacs generates backup files and, at times, auto-save files, you may want to delete them from time to time. Emacs offers shortcut commands to flag such files. Typing #
flags all the auto-save files (files whose names start and end with #
) for deletion. Emacs flags them with D. Typing ~flags all the backup files (whose names end with ~) for deletion. You can remove the flags from backup files you want to keep, for example, the backup copies of files you've recently worked on.
When you really want files to be deleted from disk, press x. Emacs displays the names of all the files flagged for deletion and asks you if you want to delete them.
Type: x

Читать дальше