"Fossies" - the Fresh Open Source Software Archive

Member "emacs-26.1/doc/emacs/dired.texi" (23 Apr 2018, 63698 Bytes) of package /linux/misc/emacs-26.1.tar.xz:

Caution: As a special service "Fossies" has tried to format the requested Texinfo source page into HTML format but that may be not always succeeeded perfectly. Alternatively you can here view or download the uninterpreted Texinfo source code. A member file download can also be achieved by clicking within a package contents listing on the according byte size field. See also the latest Fossies "Diffs" side-by-side code changes report for "dired.texi": 25.3_vs_26.1.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1 Dired, the Directory Editor

Dired makes an Emacs buffer containing a listing of a directory, and optionally some of its subdirectories as well. You can use the normal Emacs commands to move around in this buffer, and special Dired commands to operate on the listed files. Dired works with both local and remote directories.

The Dired buffer is normally read-only, and inserting text in it is not allowed (however, the Wdired mode allows that, see section Editing the Dired Buffer). Ordinary printing characters such as d and x are redefined for special Dired commands. Some Dired commands mark or flag the current file (that is, the file on the current line); other commands operate on the marked files or on the flagged files. You first mark certain files in order to operate on all of them with one command.

The Dired-X package provides various extra features for Dired mode. See Dired-X in Dired Extra User’s Manual.

You can also view a list of files in a directory with C-x C-d (list-directory). Unlike Dired, this command does not allow you to operate on the listed files. @xref{Directories}.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.1 Entering Dired

To invoke Dired, type C-x d (dired). This reads a directory’s name using the minibuffer, and opens a Dired buffer listing the files in that directory. You can also supply a wildcard file name pattern as the minibuffer argument, in which case the Dired buffer lists all files matching that pattern. A wildcard may appear in the directory part as well. For instance,

C-x d  ~/foo/*.el  <RET>
C-x d  ~/foo/*/*.el  <RET>

The former lists all the files with extension ‘.el’ in directory ‘foo’. The latter lists the files with extension ‘.el’ in all the subdirectories of ‘foo’.

The usual history and completion commands can be used in the minibuffer; in particular, M-n puts the name of the visited file (if any) in the minibuffer (@pxref{Minibuffer History}).

You can also invoke Dired by giving C-x C-f (find-file) a directory’s name.

The variable dired-listing-switches specifies the options to give to ls for listing the directory; this string must contain ‘-l’. If you use a prefix argument with the dired command, you can specify the ls switches with the minibuffer before you enter the directory specification. No matter how they are specified, the ls switches can include short options (that is, single characters) requiring no arguments, and long options (starting with ‘--’) whose arguments are specified with ‘=’.

If your ls program supports the ‘--dired’ option, Dired automatically passes it that option; this causes ls to emit special escape sequences for certain unusual file names, without which Dired will not be able to parse those names. The first time you run Dired in an Emacs session, it checks whether ls supports the ‘--dired’ option by calling it once with that option. If the exit code is 0, Dired will subsequently use the ‘--dired’ option; otherwise it will not. You can inhibit this check by customizing the variable dired-use-ls-dired. The value unspecified (the default) means to perform the check; any other non-nil value means to use the ‘--dired’ option; and nil means not to use the ‘--dired’ option.

On MS-Windows and MS-DOS systems, and also on some remote systems, Emacs emulates ls. @xref{ls in Lisp}, for options and peculiarities of this emulation.

To display the Dired buffer in another window, use C-x 4 d (dired-other-window). C-x 5 d (dired-other-frame) displays the Dired buffer in a separate frame.

Typing q (quit-window) buries the Dired buffer, and deletes its window if the window was created just for that buffer.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.2 Navigation in the Dired Buffer

All the usual Emacs cursor motion commands are available in Dired buffers. The keys C-n and C-p are redefined to run dired-next-line and dired-previous-line, respectively, and they put the cursor at the beginning of the file name on the line, rather than at the beginning of the line.

For extra convenience, <SPC> and n in Dired are equivalent to C-n. p is equivalent to C-p. (Moving by lines is so common in Dired that it deserves to be easy to type.) <DEL> (move up and unflag) is also often useful simply for moving up (see section Deleting Files with Dired).

j (dired-goto-file) prompts for a file name using the minibuffer, and moves point to the line in the Dired buffer describing that file.

M-s f C-s (dired-isearch-filenames) performs a forward incremental search in the Dired buffer, looking for matches only amongst the file names and ignoring the rest of the text in the buffer. M-s f M-C-s (dired-isearch-filenames-regexp) does the same, using a regular expression search. If you change the variable dired-isearch-filenames to t, then the usual search commands also limit themselves to the file names; for instance, C-s behaves like M-s f C-s. If the value is dwim, then search commands match the file names only when point was on a file name initially. @xref{Search}, for information about incremental search.

Some additional navigation commands are available when the Dired buffer includes several directories. See section Moving Over Subdirectories.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.3 Deleting Files with Dired

One of the most frequent uses of Dired is to first flag files for deletion, then delete the files that were flagged.


Flag this file for deletion (dired-flag-file-deletion).


Remove the deletion flag (dired-unmark).


Move point to previous line and remove the deletion flag on that line (dired-unmark-backward).


Delete files flagged for deletion (dired-do-flagged-delete).

You can flag a file for deletion by moving to the line describing the file and typing d (dired-flag-file-deletion). The deletion flag is visible as a ‘D’ at the beginning of the line. This command moves point to the next line, so that repeated d commands flag successive files. A numeric prefix argument serves as a repeat count; a negative count means to flag preceding files.

If the region is active, the d command flags all files in the region for deletion; in this case, the command does not move point, and ignores any prefix argument.

The reason for flagging files for deletion, rather than deleting files immediately, is to reduce the danger of deleting a file accidentally. Until you direct Dired to delete the flagged files, you can remove deletion flags using the commands u and <DEL>. u (dired-unmark) works just like d, but removes flags rather than making flags. <DEL> (dired-unmark-backward) moves upward, removing flags; it is like u with argument -1. A numeric prefix argument to either command serves as a repeat count, with a negative count meaning to unflag in the opposite direction. If the region is active, these commands instead unflag all files in the region, without moving point.

To delete flagged files, type x (dired-do-flagged-delete). This command displays a list of all the file names flagged for deletion, and requests confirmation with yes. If you confirm, Dired deletes the flagged files, then deletes their lines from the text of the Dired buffer. The Dired buffer, with somewhat fewer lines, remains selected.

If you answer no or quit with C-g when asked to confirm, you return immediately to Dired, with the deletion flags still present in the buffer, and no files actually deleted.

You can delete empty directories just like other files, but normally Dired cannot delete directories that are nonempty. If the variable dired-recursive-deletes is non-nil, then Dired can delete nonempty directories including all their contents. That can be somewhat risky. If the value of the variable is always, Dired will delete nonempty directories recursively, which is even more risky. Even if you have set dired-recursive-deletes to nil, you might want sometimes to delete recursively directories without being asked for confirmation for all of them. This is handy when you have marked many directories for deletion and you are very sure that all of them can safely be deleted. For every nonempty directory you are asked for confirmation; if you answer all, then all the remaining directories will be deleted without more questions.

If you change the variable delete-by-moving-to-trash to t, the above deletion commands will move the affected files or directories into the operating system’s Trash, instead of deleting them outright. @xref{Misc File Ops}.

An alternative way of deleting files is to mark them with m and delete with D, see Operating on Files.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.4 Flagging Many Files at Once

The #, ~, ., % &, and % d commands flag many files for deletion, based on their file names:


Flag all auto-save files (files whose names start and end with ‘#’) for deletion (@pxref{Auto Save}).


Flag all backup files (files whose names end with ‘~’) for deletion (@pxref{Backup}).

. (Period)

Flag excess numeric backup files for deletion. The oldest and newest few backup files of any one file are exempt; the middle ones are flagged.

% &

Flag for deletion all files with certain kinds of names which suggest you could easily create those files again.

% d regexp <RET>

Flag for deletion all files whose names match the regular expression regexp.

# (dired-flag-auto-save-files) flags all files whose names look like auto-save files—that is, files whose names begin and end with ‘#’. @xref{Auto Save}.

~ (dired-flag-backup-files) flags all files whose names say they are backup files—that is, files whose names end in ‘~’. @xref{Backup}.

. (period, dired-clean-directory) flags just some of the backup files for deletion: all but the oldest few and newest few backups of any one file. Normally, the number of newest versions kept for each file is given by the variable dired-kept-versions (not kept-new-versions; that applies only when saving). The number of oldest versions to keep is given by the variable kept-old-versions.

Period with a positive numeric argument, as in C-u 3 ., specifies the number of newest versions to keep, overriding dired-kept-versions. A negative numeric argument overrides kept-old-versions, using minus the value of the argument to specify the number of oldest versions of each file to keep.

% & (dired-flag-garbage-files) flags files whose names match the regular expression specified by the variable dired-garbage-files-regexp. By default, this matches certain files produced by TeX, ‘.bak’ files, and the ‘.orig’ and ‘.rej’ files produced by patch.

% d flags all files whose names match a specified regular expression (dired-flag-files-regexp). Only the non-directory part of the file name is used in matching. You can use ‘^’ and ‘$’ to anchor matches. You can exclude certain subdirectories from marking by hiding them while you use % d. See section Hiding Subdirectories.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.5 Visiting Files in Dired

There are several Dired commands for visiting or examining the files listed in the Dired buffer. All of them apply to the current line’s file; if that file is really a directory, these commands invoke Dired on that subdirectory (making a separate Dired buffer).


Visit the file described on the current line, like typing C-x C-f and supplying that file name (dired-find-file). @xref{Visiting}.


Equivalent to f.


Like f, but uses another window to display the file’s buffer (dired-find-file-other-window). The Dired buffer remains visible in the first window. This is like using C-x 4 C-f to visit the file. @xref{Windows}.


Visit the file described on the current line, and display the buffer in another window, but do not select that window (dired-display-file).


Visit the file whose name you clicked on (dired-mouse-find-file-other-window). This uses another window to display the file, like the o command.


View the file described on the current line, with View mode (dired-view-file). View mode provides convenient commands to navigate the buffer but forbids changing it; @xref{View Mode}.


Visit the parent directory of the current directory (dired-up-directory). This is equivalent to moving to the line for ‘..’ and typing f there.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.6 Dired Marks vs. Flags

Instead of flagging a file with ‘D’, you can mark the file with some other character (usually ‘*’). Most Dired commands to operate on files use the files marked with ‘*’. The only command that operates on flagged files is x, which deletes them.

Here are some commands for marking with ‘*’, for unmarking, and for operating on marks. (See section Deleting Files with Dired, for commands to flag and unflag files.)

* m

Mark the current file with ‘*’ (dired-mark). If the region is active, mark all files in the region instead; otherwise, if a numeric argument n is supplied, mark the next n files instead, starting with the current file (if n is negative, mark the previous -n files). If invoked on a subdirectory header line (see section Subdirectories in Dired), this command marks all the files in that subdirectory.

* *

Mark all executable files with ‘*’ (dired-mark-executables). With a numeric argument, unmark all those files.

* @

Mark all symbolic links with ‘*’ (dired-mark-symlinks). With a numeric argument, unmark all those files.

* /

Mark with ‘*’ all files which are directories, except for ‘.’ and ‘..’ (dired-mark-directories). With a numeric argument, unmark all those files.

* s

Mark all the files in the current subdirectory, aside from ‘.’ and ‘..’ (dired-mark-subdir-files).

* u

Remove any mark on this line (dired-unmark). If the region is active, unmark all files in the region instead; otherwise, if a numeric argument n is supplied, unmark the next n files instead, starting with the current file (if n is negative, unmark the previous -n files).

* <DEL>

Move point to previous line and remove any mark on that line (dired-unmark-backward). If the region is active, unmark all files in the region instead; otherwise, if a numeric argument n is supplied, unmark the n preceding files instead, starting with the current file (if n is negative, unmark the next -n files).

* !

Remove all marks from all the files in this Dired buffer (dired-unmark-all-marks).

* ? markchar

Remove all marks that use the character markchar (dired-unmark-all-files). If invoked with M-<DEL>, the command prompts for markchar. That markchar is a single character—do not use <RET> to terminate it. See the description of the * c command below, which lets you replace one mark character with another.

With a numeric argument, this command queries about each marked file, asking whether to remove its mark. You can answer y meaning yes, n meaning no, or ! to remove the marks from the remaining files without asking about them.

* C-n

Move down to the next marked file (dired-next-marked-file). A file is “marked” if it has any kind of mark.

* C-p

Move up to the previous marked file (dired-prev-marked-file).

* t

Toggle all marks (dired-toggle-marks): files marked with ‘*’ become unmarked, and unmarked files are marked with ‘*’. Files marked in any other way are not affected.

* c old-markchar new-markchar

Replace all marks that use the character old-markchar with marks that use the character new-markchar (dired-change-marks). This command is the primary way to create or use marks other than ‘*’ or ‘D’. The arguments are single characters—do not use <RET> to terminate them.

You can use almost any character as a mark character by means of this command, to distinguish various classes of files. If old-markchar is a space (‘ ’), then the command operates on all unmarked files; if new-markchar is a space, then the command unmarks the files it acts on.

To illustrate the power of this command, here is how to put ‘D’ flags on all the files that have no marks, while unflagging all those that already have ‘D’ flags:

* c D t  * c <SPC> D  * c t <SPC>

This assumes that no files were already marked with ‘t’.

% m regexp <RET>
* % regexp <RET>

Mark (with ‘*’) all files whose names match the regular expression regexp (dired-mark-files-regexp). This command is like % d, except that it marks files with ‘*’ instead of flagging with ‘D’.

Only the non-directory part of the file name is used in matching. Use ‘^’ and ‘$’ to anchor matches. You can exclude subdirectories by temporarily hiding them (see section Hiding Subdirectories).

% g regexp <RET>

Mark (with ‘*’) all files whose contents contain a match for the regular expression regexp (dired-mark-files-containing-regexp). This command is like % m, except that it searches the file contents instead of the file name. Note that if a file is visited in an Emacs buffer, and dired-always-read-filesystem is nil (the default), this command will look in the buffer without revisiting the file, so the results might be inconsistent with the file on disk if its contents have changed since it was last visited. If you don’t want this, you may wish to revert the files you have visited in your buffers, or to turn on Auto-Revert mode in those buffers, before invoking this command. @xref{Reverting}. If you prefer that this command should always revisit the file, without you having to revert the file or enable Auto-Revert mode, you might want to set dired-always-read-filesystem to non-nil.

C-x u

Undo changes in the Dired buffer, such as adding or removing marks (dired-undo). This command does not revert the actual file operations, nor recover lost files! It just undoes changes in the buffer itself.

In some cases, using this after commands that operate on files can cause trouble. For example, after renaming one or more files, dired-undo restores the original names in the Dired buffer, which gets the Dired buffer out of sync with the actual contents of the directory.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.7 Operating on Files

This section describes the basic Dired commands to operate on one file or several files. All of these commands are capital letters; all of them use the minibuffer, either to read an argument or to ask for confirmation, before they act. All of them let you specify the files to manipulate in these ways:

Certain other Dired commands, such as ! and the ‘%’ commands, use the same conventions to decide which files to work on.

Commands which ask for a destination directory, such as those which copy and rename files or create links for them, try to guess the default target directory for the operation. Normally, they suggest the Dired buffer’s default directory, but if the variable dired-dwim-target is non-nil, and if there is another Dired buffer displayed in the next window, that other buffer’s directory is suggested instead.

Here are the file-manipulating Dired commands that operate on files.

C new <RET>

Copy the specified files (dired-do-copy). The argument new is the directory to copy into, or (if copying a single file) the new name. This is like the shell command cp.

If dired-copy-preserve-time is non-nil, then copying with this command preserves the modification time of the old file in the copy, like ‘cp -p’.

The variable dired-recursive-copies controls whether to copy directories recursively (like ‘cp -r’). The default is top, which means to ask before recursively copying a directory.


Delete the specified files (dired-do-delete). This is like the shell command rm.

Like the other commands in this section, this command operates on the marked files, or the next n files. By contrast, x (dired-do-flagged-delete) deletes all flagged files.

R new <RET>

Rename the specified files (dired-do-rename). If you rename a single file, the argument new is the new name of the file. If you rename several files, the argument new is the directory into which to move the files (this is like the shell command mv).

Dired automatically changes the visited file name of buffers associated with renamed files so that they refer to the new names.

H new <RET>

Make hard links to the specified files (dired-do-hardlink). This is like the shell command ln. The argument new is the directory to make the links in, or (if making just one link) the name to give the link.

S new <RET>

Make symbolic links to the specified files (dired-do-symlink). This is like ‘ln -s’. The argument new is the directory to make the links in, or (if making just one link) the name to give the link.

M modespec <RET>

Change the mode (also called permission bits) of the specified files (dired-do-chmod). modespec can be in octal or symbolic notation, like arguments handled by the chmod program.

G newgroup <RET>

Change the group of the specified files to newgroup (dired-do-chgrp).

O newowner <RET>

Change the owner of the specified files to newowner (dired-do-chown). (On most systems, only the superuser can do this.)

The variable dired-chown-program specifies the name of the program to use to do the work. (This variable is necessary because different systems put chown in different places).

T timestamp <RET>

Touch the specified files (dired-do-touch). This means updating their modification times to the present time. This is like the shell command touch.

P command <RET>

Print the specified files (dired-do-print). You must specify the command to print them with, but the minibuffer starts out with a suitable guess made using the variables lpr-command and lpr-switches (the same variables that lpr-buffer uses; @pxref{Printing}).


Compress the specified files (dired-do-compress). If the file appears to be a compressed file already, uncompress it instead. Each marked file is compressed into its own archive. This uses the gzip program if it is available, otherwise it uses compress.


Compress the specified files (dired-do-compress-to) into a single archive anywhere on the file system. The compression algorithm is determined by the extension of the archive, see dired-compress-files-alist.


Decrypt the specified files (epa-dired-do-decrypt). See Dired integration in EasyPG Assistant User’s Manual.


Verify digital signatures on the specified files (epa-dired-do-verify). See Dired integration in EasyPG Assistant User’s Manual.


Digitally sign the specified files (epa-dired-do-sign). See Dired integration in EasyPG Assistant User’s Manual.


Encrypt the specified files (epa-dired-do-encrypt). See Dired integration in EasyPG Assistant User’s Manual.


Load the specified Emacs Lisp files (dired-do-load). @xref{Lisp Libraries}.


Byte compile the specified Emacs Lisp files (dired-do-byte-compile). See Byte Compilation in The Emacs Lisp Reference Manual.

A regexp <RET>

Search all the specified files for the regular expression regexp (dired-do-find-regexp).

This command is a variant of xref-find-references (@pxref{Identifier Search}), it displays the ‘*xref*’ buffer, where you can navigate between matches and display them as needed using the commands described in @ref{Xref Commands}.

If any of the marked files are directories, then this command searches all of the files in those directories, and any of their subdirectories, recursively, except files whose names match grep-find-ignored-files and subdirectories whose names match grep-find-ignored-directories.

Q regexp <RET> to <RET>

Perform query-replace-regexp on each of the specified files, replacing matches for regexp with the string to (dired-do-find-regexp-and-replace).

This command is a variant of xref-query-replace-in-results. It presents an ‘*xref*’ buffer that lists all the matches of regexp, and you can use the special commands in that buffer (@pxref{Xref Commands}). In particular, if you exit the query replace loop, you can use r in that buffer to replace more matches. @xref{Identifier Search}.

Like with dired-do-find-regexp, if any of the marked files are directories, this command performs replacements in all of the files in those directories, and in any of their subdirectories, recursively, except for files whose names match grep-find-ignored-files and subdirectories whose names match grep-find-ignored-directories.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.8 Shell Commands in Dired

The Dired command ! (dired-do-shell-command) reads a shell command string in the minibuffer, and runs that shell command on one or more files. The files that the shell command operates on are determined in the usual way for Dired commands (see section Operating on Files). The command X is a synonym for !.

The command & (dired-do-async-shell-command) does the same, except that it runs the shell command asynchronously. (You can also do this with !, by appending a ‘&’ character to the end of the shell command.) When the command operates on more than one file, it runs multiple parallel copies of the specified shell command, one for each file. As an exception, if the specified shell command ends in ‘;’ or ‘;&’, the shell command is run in the background on each file sequentially; Emacs waits for each invoked shell command to terminate before running the next one.

For both ! and &, the working directory for the shell command is the top-level directory of the Dired buffer.

If you tell ! or & to operate on more than one file, the shell command string determines how those files are passed to the shell command:

To iterate over the file names in a more complicated fashion, you might prefer to use an explicit shell loop. For example, here is how to uuencode each file, making the output file name by appending ‘.uu’ to the input file name:

for file in * ; do uuencode "$file" "$file" >"$file".uu; done

The same example with ‘`?`’ notation:

uuencode ? ? > `?`.uu

The ! and & commands do not attempt to update the Dired buffer to show new or modified files, because they don’t know what files will be changed. Use the g command to update the Dired buffer (see section Updating the Dired Buffer).

@xref{Single Shell}, for information about running shell commands outside Dired.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.9 Transforming File Names in Dired

This section describes Dired commands which alter file names in a systematic way. Each command operates on some or all of the marked files, using a new name made by transforming the existing name.

Like the basic Dired file-manipulation commands (see section Operating on Files), the commands described here operate either on the next n files, or on all files marked with ‘*’, or on the current file. (To mark files, use the commands described in Dired Marks vs. Flags.)

All of the commands described in this section work interactively: they ask you to confirm the operation for each candidate file. Thus, you can select more files than you actually need to operate on (e.g., with a regexp that matches many files), and then filter the selected names by typing y or n when the command prompts for confirmation.

% u

Rename each of the selected files to an upper-case name (dired-upcase). If the old file names are ‘Foo’ and ‘bar’, the new names are ‘FOO’ and ‘BAR’.

% l

Rename each of the selected files to a lower-case name (dired-downcase). If the old file names are ‘Foo’ and ‘bar’, the new names are ‘foo’ and ‘bar’.

% R from <RET> to <RET>
% C from <RET> to <RET>
% H from <RET> to <RET>
% S from <RET> to <RET>

These four commands rename, copy, make hard links and make soft links, in each case computing the new name by regular-expression substitution from the name of the old file.

The four regular-expression substitution commands effectively perform a search-and-replace on the selected file names. They read two arguments: a regular expression from, and a substitution pattern to; they match each old file name against from, and then replace the matching part with to. You can use ‘\&’ and ‘\digit’ in to to refer to all or part of what the pattern matched in the old file name, as in replace-regexp (@pxref{Regexp Replace}). If the regular expression matches more than once in a file name, only the first match is replaced.

For example, % R ^.*$ <RET> x-\& <RET> renames each selected file by prepending ‘x-’ to its name. The inverse of this, removing ‘x-’ from the front of each file name, is also possible: one method is % R ^x-\(.*\)$ <RET> \1 <RET>; another is % R ^x- <RET> <RET>. (Use ‘^’ and ‘$’ to anchor matches that should span the whole file name.)

Normally, the replacement process does not consider the files’ directory names; it operates on the file name within the directory. If you specify a numeric argument of zero, then replacement affects the entire absolute file name including directory name. (A non-zero argument specifies the number of files to operate on.)

You may want to select the set of files to operate on using the same regexp from that you will use to operate on them. To do this, mark those files with % m from <RET>, then use the same regular expression in the command to operate on the files. To make this more convenient, the % commands to operate on files use the last regular expression specified in any % command as a default.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.10 File Comparison with Dired

The = (dired-diff) command compares the current file (the file at point) with another file (read using the minibuffer) using the diff program. The file specified with the minibuffer is the first argument of diff, and file at point is the second argument. The output of the diff program is shown in a buffer using Diff mode (@pxref{Comparing Files}).

If the region is active, the default for the file read using the minibuffer is the file at the mark (i.e., the ordinary Emacs mark, not a Dired mark; @pxref{Setting Mark}). Otherwise, if the file at point has a backup file (@pxref{Backup}), that is the default.

You could also compare files using ediff-files, see Major Entry Points in Ediff User’s Manual.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.11 Subdirectories in Dired

A Dired buffer usually displays just one directory, but you can optionally include its subdirectories as well.

The simplest way to include multiple directories in one Dired buffer is to specify the options ‘-lR’ for running ls. (If you give a numeric argument when you run Dired, then you can specify these options in the minibuffer.) That produces a recursive directory listing showing all subdirectories at all levels.

More often, you will want to show only specific subdirectories. You can do this with i (dired-maybe-insert-subdir):


Insert the contents of a subdirectory later in the buffer.

If you use this command on a line that describes a file which is a directory, it inserts the contents of that directory into the same Dired buffer, and moves there. Inserted subdirectory contents follow the top-level directory of the Dired buffer, just as they do in ‘ls -lR’ output.

If the subdirectory’s contents are already present in the buffer, the i command just moves to it.

In either case, i sets the Emacs mark before moving, so C-u C-<SPC> returns to your previous position in the Dired buffer (@pxref{Setting Mark}). You can also use ‘^’ to return to the parent directory in the same Dired buffer (see section Visiting Files in Dired).

Use the l command (dired-do-redisplay) to update the subdirectory’s contents, and use C-u k on the subdirectory header line to remove the subdirectory listing (see section Updating the Dired Buffer). You can also hide and show inserted subdirectories (see section Hiding Subdirectories).

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.12 Subdirectory Switches in Dired

You can insert subdirectories with specified ls switches in Dired buffers using C-u i. You can change the ls switches of an already inserted subdirectory at point using C-u l.

Dired preserves the switches if you revert the buffer. Deleting a subdirectory forgets about its switches.

Using dired-undo (see section Dired Marks vs. Flags) to reinsert or delete subdirectories that were inserted with explicit switches can bypass Dired’s machinery for remembering (or forgetting) switches. Deleting a subdirectory using dired-undo does not forget its switches. When later reinserted using i, it will be reinserted using its old switches. Using dired-undo to reinsert a subdirectory that was deleted using the regular Dired commands (not dired-undo) will originally insert it with its old switches. Reverting the buffer, however, will relist it using the buffer’s default switches. If any of this yields problems, you can easily correct the situation using C-u i or C-u l.

Dired does not remember the R switch. Inserting a subdirectory with switches that include the R switch is equivalent to inserting each of its subdirectories using all remaining switches. For instance, updating or killing a subdirectory that was inserted with the R switch will not update or kill its subdirectories.

The buffer’s default switches do not affect subdirectories that were inserted using explicitly specified switches. In particular, commands such as s that change the buffer’s switches do not affect such subdirectories. (They do, however, affect subdirectories without explicitly assigned switches.)

You can make Dired forget about all subdirectory switches and relist all subdirectories with the buffer’s default switches using M-x dired-reset-subdir-switches. This also reverts the Dired buffer.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.13 Moving Over Subdirectories

When a Dired buffer lists subdirectories, you can use the page motion commands C-x [ and C-x ] to move by entire directories (@pxref{Pages}).

The following commands move across, up and down in the tree of directories within one Dired buffer. They move to directory header lines, which are the lines that give a directory’s name, at the beginning of the directory’s contents.


Go to next subdirectory header line, regardless of level (dired-next-subdir).


Go to previous subdirectory header line, regardless of level (dired-prev-subdir).


Go up to the parent directory’s header line (dired-tree-up).


Go down in the directory tree, to the first subdirectory’s header line (dired-tree-down).


Move up to the previous directory-file line (dired-prev-dirline). These lines are the ones that describe a directory as a file in its parent directory.


Move down to the next directory-file line (dired-next-dirline).

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.14 Hiding Subdirectories

Hiding a subdirectory means to make it invisible, except for its header line.


Hide or show the subdirectory that point is in, and move point to the next subdirectory (dired-hide-subdir). This is a toggle. A numeric argument serves as a repeat count.


Hide all subdirectories in this Dired buffer, leaving only their header lines (dired-hide-all). Or, if any subdirectory is currently hidden, make all subdirectories visible again. You can use this command to get an overview in very deep directory trees or to move quickly to subdirectories far away.

Ordinary Dired commands never consider files inside a hidden subdirectory. For example, the commands to operate on marked files ignore files in hidden directories even if they are marked. Thus you can use hiding to temporarily exclude subdirectories from operations without having to remove the Dired marks on files in those subdirectories.

See section Subdirectories in Dired, for how to insert a subdirectory listing, and see Updating the Dired Buffer, for how to delete it.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.15 Updating the Dired Buffer

This section describes commands to update the Dired buffer to reflect outside (non-Dired) changes in the directories and files, and to delete part of the Dired buffer.


Update the entire contents of the Dired buffer (revert-buffer).


Update the specified files (dired-do-redisplay). You specify the files for l in the same way as for file operations.


Delete the specified file lines—not the files, just the lines (dired-do-kill-lines).


Toggle between alphabetical order and date/time order (dired-sort-toggle-or-edit).

C-u s switches <RET>

Refresh the Dired buffer using switches as dired-listing-switches.

Type g (revert-buffer) to update the contents of the Dired buffer, based on changes in the files and directories listed. This preserves all marks except for those on files that have vanished. Hidden subdirectories are updated but remain hidden.

To update only some of the files, type l (dired-do-redisplay). Like the Dired file-operating commands, this command operates on the next n files (or previous -n files), or on the marked files if any, or on the current file. Updating the files means reading their current status, then updating their lines in the buffer to indicate that status.

If you use l on a subdirectory header line, it updates the contents of the corresponding subdirectory.

If you use C-x d or some other Dired command to visit a directory that is already being shown in a Dired buffer, Dired switches to that buffer but does not update it. If the buffer is not up-to-date, Dired displays a warning telling you to type g to update it. You can also tell Emacs to revert each Dired buffer automatically when you revisit it, by setting the variable dired-auto-revert-buffer to a non-nil value.

To delete file lines from the buffer—without actually deleting the files—type k (dired-do-kill-lines). Like the file-operating commands, this command operates on the next n files, or on the marked files if any. However, it does not operate on the current file, since otherwise mistyping k could be annoying.

If you use k to kill the line for a directory file which you had inserted in the Dired buffer as a subdirectory (see section Subdirectories in Dired), it removes the subdirectory listing as well. Typing C-u k on the header line for a subdirectory also removes the subdirectory line from the Dired buffer.

The g command brings back any individual lines that you have killed in this way, but not subdirectories—you must use i to reinsert a subdirectory.

The files in a Dired buffers are normally listed in alphabetical order by file names. Alternatively Dired can sort them by date/time. The Dired command s (dired-sort-toggle-or-edit) switches between these two sorting modes. The mode line in a Dired buffer indicates which way it is currently sorted—by name, or by date.

C-u s switches <RET> lets you specify a new value for dired-listing-switches.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.16 Dired and find

You can select a set of files for display in a Dired buffer more flexibly by using the find utility to choose the files.

To search for files with names matching a wildcard pattern use M-x find-name-dired. It reads arguments directory and pattern, and chooses all the files in directory or its subdirectories whose individual names match pattern.

The files thus chosen are displayed in a Dired buffer, in which the ordinary Dired commands are available.

If you want to test the contents of files, rather than their names, use M-x find-grep-dired. This command reads two minibuffer arguments, directory and regexp; it chooses all the files in directory or its subdirectories that contain a match for regexp. It works by running the programs find and grep. See also M-x grep-find, in @ref{Grep Searching}. Remember to write the regular expression for grep, not for Emacs. (An alternative method of showing files whose contents match a given regexp is the % g regexp command, see Dired Marks vs. Flags.)

The most general command in this series is M-x find-dired, which lets you specify any condition that find can test. It takes two minibuffer arguments, directory and find-args; it runs find in directory, passing find-args to tell find what condition to test. To use this command, you need to know how to use find.

The format of listing produced by these commands is controlled by the variable find-ls-option. This is a pair of options; the first specifying how to call find to produce the file listing, and the second telling Dired to parse the output.

The command M-x locate provides a similar interface to the locate program. M-x locate-with-filter is similar, but keeps only files whose names match a given regular expression.

These buffers don’t work entirely like ordinary Dired buffers: file operations work, but do not always automatically update the buffer. Reverting the buffer with g deletes all inserted subdirectories, and erases all flags and marks.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.17 Editing the Dired Buffer

Wdired is a special mode that allows you to perform file operations by editing the Dired buffer directly (the “W” in “Wdired” stands for “writable”). To enter Wdired mode, type C-x C-q (dired-toggle-read-only) while in a Dired buffer. Alternatively, use the ‘Immediate / Edit File Names’ menu item.

While in Wdired mode, you can rename files by editing the file names displayed in the Dired buffer. All the ordinary Emacs editing commands, including rectangle operations and query-replace, are available for this. Once you are done editing, type C-c C-c (wdired-finish-edit). This applies your changes and switches back to ordinary Dired mode.

Apart from simply renaming files, you can move a file to another directory by typing in the new file name (either absolute or relative). To mark a file for deletion, delete the entire file name. To change the target of a symbolic link, edit the link target name which appears next to the link name.

If you edit the file names to create a new subdirectory, Wdired will automatically create these new directories. To inhibit this behavior, set wdired-create-parent-directories to nil.

The rest of the text in the buffer, such as the file sizes and modification dates, is marked read-only, so you can’t edit it. However, if you set wdired-allow-to-change-permissions to t, you can edit the file permissions. For example, you can change ‘-rw-r--r--’ to ‘-rw-rw-rw-’ to make a file world-writable. These changes also take effect when you type C-c C-c.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.18 Viewing Image Thumbnails in Dired

Image-Dired is a facility for browsing image files. It provides viewing the images either as thumbnails or in full size, either inside Emacs or through an external viewer.

To enter Image-Dired, mark the image files you want to look at in the Dired buffer, using m as usual. Then type C-t d (image-dired-display-thumbs). This creates and switches to a buffer containing image-dired, corresponding to the marked files.

You can also enter Image-Dired directly by typing M-x image-dired. This prompts for a directory; specify one that has image files. This creates thumbnails for all the images in that directory, and displays them all in the thumbnail buffer. This takes a long time if the directory contains many image files, and it asks for confirmation if the number of image files exceeds image-dired-show-all-from-dir-max-files.

With point in the thumbnail buffer, you can type <RET> (image-dired-display-thumbnail-original-image) to display a sized version of it in another window. This sizes the image to fit the window. Use the arrow keys to move around in the buffer. For easy browsing, use <SPC> (image-dired-display-next-thumbnail-original) to advance and display the next image. Typing <DEL> (image-dired-display-previous-thumbnail-original) backs up to the previous thumbnail and displays that instead.

To view the image in its original size, either provide a prefix argument (C-u) before pressing <RET>, or type C-<RET> (image-dired-thumbnail-display-external) to display the image in an external viewer. You must first configure image-dired-external-viewer.

You can delete images through Image-Dired also. Type d (image-dired-flag-thumb-original-file) to flag the image file for deletion in the Dired buffer. You can also delete the thumbnail image from the thumbnail buffer with C-d (image-dired-delete-char).

More advanced features include image tags, which are metadata used to categorize image files. The tags are stored in a plain text file configured by image-dired-db-file.

To tag image files, mark them in the dired buffer (you can also mark files in Dired from the thumbnail buffer by typing m) and type C-t t (image-dired-tag-files). This reads the tag name in the minibuffer. To mark files having a certain tag, type C-t f (image-dired-mark-tagged-files). After marking image files with a certain tag, you can use C-t d to view them.

You can also tag a file directly from the thumbnail buffer by typing t t and you can remove a tag by typing t r. There is also a special tag called “comment” for each file (it is not a tag in the exact same sense as the other tags, it is handled slightly differently). That is used to enter a comment or description about the image. You comment a file from the thumbnail buffer by typing c. You will be prompted for a comment. Type C-t c to add a comment from Dired (image-dired-dired-comment-files).

Image-Dired also provides simple image manipulation. In the thumbnail buffer, type L to rotate the original image 90 degrees anti clockwise, and R to rotate it 90 degrees clockwise. This rotation is lossless, and uses an external utility called jpegtran, which you need to install first.

[ << ] [ < ] [ Up ] [ > ] [ >> ]         [Top] [Contents] [Index] [ ? ]

1.19 Other Dired Features

The command + (dired-create-directory) reads a directory’s name, and creates that directory. It signals an error if the directory already exists.

The command M-s a C-s (dired-do-isearch) begins a multi-file incremental search on the marked files. If a search fails at the end of a file, typing C-s advances to the next marked file and repeats the search; at the end of the last marked file, the search wraps around to the first marked file. The command M-s a M-C-s (dired-do-isearch-regexp) does the same with a regular expression search. @xref{Repeat Isearch}, for information about search repetition.

The command w (dired-copy-filename-as-kill) puts the names of the marked (or next n) files into the kill ring, as if you had killed them with C-w. The names are separated by a space.

With a zero prefix argument, this uses the absolute file name of each marked file. With just C-u as the prefix argument, it uses file names relative to the Dired buffer’s default directory. (This can still contain slashes if in a subdirectory.) As a special case, if point is on a directory header line, w gives you the absolute name of that directory. Any prefix argument or marked files are ignored in this case.

The main purpose of this command is so that you can yank the file names into arguments for other Emacs commands. It also displays what it added to the kill ring, so you can use it to display the list of currently marked files in the echo area.

If you have an HTML file in the file listing, it can be useful to view that file with a browser. The W (browse-url-of-dired-file) command will use the standard configured browser to view that file.

The command ( (dired-hide-details-mode) toggles whether details, such as ownership or file permissions, are visible in the current Dired buffer. By default, it also hides the targets of symbolic links, and all lines other than the header line and file/directory listings. To change this, customize the options dired-hide-details-hide-symlink-targets and dired-hide-details-hide-information-lines, respectively.

If the directory you are visiting is under version control (@pxref{Version Control}), then the normal VC diff and log commands will operate on the selected files.

The command M-x dired-compare-directories is used to compare the current Dired buffer with another directory. It marks all the files that differ between the two directories. It puts these marks in all Dired buffers where these files are listed, which of course includes the current buffer.

The default comparison method (used if you type <RET> at the prompt) is to compare just the file names—file names differ if they do not appear in the other directory. You can specify more stringent comparisons by entering a Lisp expression, which can refer to the variables size1 and size2, the respective file sizes; mtime1 and mtime2, the last modification times in seconds, as floating point numbers; and fa1 and fa2, the respective file attribute lists (as returned by the function file-attributes). This expression is evaluated for each pair of like-named files, and files differ if the expression’s value is non-nil.

For instance, the sequence M-x dired-compare-directories <RET> (> mtime1 mtime2) <RET> marks files newer in this directory than in the other, and marks files older in the other directory than in this one. It also marks files with no counterpart, in both directories, as always.

On the X Window System, Emacs supports the drag and drop protocol. You can drag a file object from another program, and drop it onto a Dired buffer; this either moves, copies, or creates a link to the file in that directory. Precisely which action is taken is determined by the originating program. Dragging files out of a Dired buffer is currently not supported.

[Top] [Contents] [Index] [ ? ]

About This Document

This document was generated on May 29, 2018 using texi2html.

The buttons in the navigation panels have the following meaning:

Button Name Go to From 1.2.3 go to
[ << ] FastBack Beginning of this chapter or previous chapter 1
[ < ] Back Previous section in reading order 1.2.2
[ Up ] Up Up section 1.2
[ > ] Forward Next section in reading order 1.2.4
[ >> ] FastForward Next chapter 2
[Top] Top Cover (top) of document  
[Contents] Contents Table of contents  
[Index] Index Index  
[ ? ] About About (help)  

where the Example assumes that the current position is at Subsubsection One-Two-Three of a document of the following structure:

This document was generated on May 29, 2018 using texi2html.