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 f for find or e for 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 b followed 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 D on 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 u to undelete the file. At some later time, you type x to 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 u at any time to remove the deletion flags from the files. Typing u moves you to the next file in the list, and, if it is marked, unmarks it. You can also use Del to 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
Emacs asks you to confirm the deletion by typing yes (Windows).
Type yes to delete them all or type no to return to the Dired buffer without deleting any of them.
This is the usual way of deleting files, but if you want a file deleted right away, type an uppercase D. Emacs asks if you want to delete the file (yes or no). Type yes to delete the file immediately or no to change your mind. In Dired, this is one of a number of cases in which the lowercase letter (like d to flag for deletion) and the uppercase letter (like D to delete immediately) have a different meaning.
To copy a file in Dired, type C next to it (it must be a capital C). Emacs asks for the name of the file you want to copy to. Type the name and press Enter. Emacs says, Copied: 1 file. To copy several files in the list, preface the C with a number. For example, typing 3C would copy this file and the next two files. (See "Working with Groups of Files" later in this chapter for fancier ways to select a group of files to operate on.)
To rename a file with Dired (similar to the Unix mv command), type R next to the filename. Emacs asks what the new name should be. Type it and press Enter. Emacs says, Moved: 1 file.
If you move files between platforms, you can wind up with some filenames in uppercase and some in lowercase. Files moving from older versions of Windows may be in all caps, for example. Simply mark the files in question by typing m, then press %l for lowercase or %u for uppercase. Voilà—painless case consistency.
5.2.3 Compressing and Uncompressing Files
Compressing files saves disk space, and Dired provides an easy way to do it. Put the cursor on the line of the file you want to compress and press Z (for dired-do-compress). Emacs asks the following:
Compress or uncompress filename? (y or n)
Emacs compresses the file if it's not compressed and uncompresses it if it is.[28] Press y to compress or uncompress the current file. Compression happens immediately, so you can watch both the extension and file size change as Emacs compresses the file.
What about editing compressed files? Although it's not on by default, Emacs has an automatic compression/decompression mode called auto-compress mode. To enter it for this session, type M-x auto-compress-mode Enter, which turns automatic compression on. To enable auto-compression automatically, add this line to your .emacs file:
(auto-compression-mode 1)
5.2.4 Comparing Files
In Chapter 4, we discussed comparing files in two windows. Emacs provides a way to do this using the diff command in Dired. Set the mark on the file you want diff to compare, put the cursor on the other file, then type =. Emacs compares the two files and opens a window with a *diff* buffer containing the output from the command.
Emacs has a separate option for comparing a file to its backup file. Put the cursor on the file you want to compare with its backup and type M-=. Emacs displays a *diff* buffer showing the differences between the two files.
If you are serious about version control, you may want to check out Chapter 12, which discusses version control as well as the GNU tool ediff.
5.2.5 Running Shell Commands on Files
While Dired's implementation of diff is useful (and there are implementations of chmod, grep, and find as well), in a more general sense, you can perform any command on a file by pressing an exclamation point (!). For example, let's alphabetize the phone list file using the sort command.
Move to the phone file and press !
Emacs asks what command you want to run (Mac OS X).
Type: sort
Emacs displays the output from the command in a separate window (Mac OS X).
Usually, asterisks (*) and question marks are used as wildcards in commands. In Dired, they have a special meaning. An asterisk means "use the file I'm on or the files I've marked"; that way you don't have to type filenames explicitly. When multiple files are marked, a question mark means to run this command separately on each file.
28
Emacs understands only compress and gzip formats, not ZIP or other proprietary file compression algorithms. When you uncompress files, Emacs recognizes and correctly uncompresses files with the following suffixes: