Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 16: Interfacing with the System

Previous HourNext Hour

Sections in this Hour:

 

Editing Directories

There are also some special built-in commands that deal with the filesystem, such as M-x make-directory, M-x rename-file, and others. The easiest way to work with these commands is to use dired (directory editor). You can edit a directory with C-x d, which prompts you for a directory, though it is as easy to use find file (C-x C-f) and choose a directory instead of a file.

The resulting display is a listing as would be produced with the UNIX command ls -la. Each file is represented on one line with permissions, owner, and size. The sort order can be changed from alphabetized to date with the keystroke s. You can insert additional directories into the buffer by placing point on a line representing a directory (the first character in the permissions field is a d) with the keystroke i.

Note - Windows users also have the same display as UNIX users, although the ls command is not actually used and is internally emulated in Emacs.


There is an immense list of file operations that can be performed on files from this display. The commands allowed are broken into groups which are then bound to key patterns. In general, lowercase alphanumeric keys perform operations that do not affect the filesystem. Uppercase alphanumeric keys do perform operations that affect the filesystem. Another handy aspect is that almost all bindings are representative of the command to be executed. You can easily navigate up and down the listing with the spacebar and Delete key, respectively.

Caution - The notable exception to the upper- and lowercase rule is that although lowercase d (delete file) only marks a file to be deleted, lowercase x (expunge) performs an actual filesystem deletion. To remove a file from the list without deleting it from the filesystem, use the kill lines command bound to k.


Before I list all the most useful commands available, it is important to discuss marking. When editing directories there two kinds of user-controllable marks. There are deletion marks, which are represented by a capital D in the first column. There is also a generic mark represented by an asterisk (*) in the first column. You set a deletion mark with d, and a regular mark with m. All marks can be removed with the unmark command bound to u, or with the unmark backwards command bound to Delete.

In general, operations that affect the filesystem work on the file under the cursor unless there are marks. When there are marks, the entire group of files are affected.

Some common operations to perform when editing a directory include the following:

In addition to these commands, there are many extended mark commands bound behind the asterisk (*):

When you have successfully marked all your files, you can perform filesystem operations on them. When examining the list that follows, the current file is the file under the point or all files with a generic mark. It is not the file under the point and the generically marked files. Some of the most common are as follows:

Caution - These commands do real things to your filesystem, so always be careful what your marks are set to, and be cautious while experimenting with them.


There are also commands bound under the % key that allow regular expression filtering. These commands match the ones already listed. For example, %-m marks all files matching a regular expression that you enter. %-C copies files. %-d flags files matching the regular expression for deletion. The only commands that don't quite match are %-u and %-l, which convert the current file or marked files into upper- and lowercase respectively.

Note - When working with regular expressions, it is very important to remember that these are full-featured regular expressions as discussed in Hour 9 and not file expressions. Thus, if you enter foo*.ps, it will not match foofile.ps, but instead find fooooootps (if such a file exists).


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 16: Interfacing with the System

Previous HourNext Hour

Sections in this Hour: