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.

The Dired buffer is read-only, and inserting text in it is not allowed.  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 (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.  See Directories.

Entering Dired
==============

To invoke Dired, type C-x d (dired).  This reads a directory 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.  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 (see Minibuffer History).

You can also invoke Dired by giving C-x C-f (find-file) a directory 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, Emacs emulates ls. See 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.

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 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 Dired Deletion).

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.  See Search, for information about incremental search.

Some additional navigation commands are available when the Dired buffer includes several directories.  See Subdirectory Motion.

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.

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

u
     Remove the deletion flag (dired-unmark).

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

x
     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 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.  See Misc File Ops.

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 (see Auto Save).

~
     Flag all backup files (files whose names end with ~) for deletion (see 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 #.  See Auto Save.

~ (dired-flag-backup-files) flags all files whose names say they are backup files---that is, files whose names end in ~.  See 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 Hiding Subdirectories.

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).

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

RET
e
     Equivalent to f.

o
     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.  See Windows.

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

mouse-1
mouse-2
     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.

v
     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; see 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.

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 Dired Deletion, for commands to flag and unflag files.)

m
* 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).

* *
     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
* 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
* 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).

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

* ? MARKCHAR
M-DEL
     Remove all marks that use the character MARKCHAR (dired-unmark-all-files).  The argument 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
M-}
     Move down to the next marked file (dired-next-marked-file) A file is ``marked'' if it has any kind of mark.

* C-p
M-{
     Move up to the previous marked file (dired-prev-marked-file)

t
* 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 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, 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 has changed since it was last visited.  If you don't want this, you may wish reverting the files you have visited in your buffers, or turning on the auto-revert mode in those buffers, before invoking this command.  See Reverting.

C-/
C-x u
C-_
     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.

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:

  * If you give the command a numeric prefix argument N, it operates on the next N files, starting with the current file.  (If N is negative, the command operates on the -N files preceding the current line.)

  * Otherwise, if some files are marked with *, the command operates on all those files.

  * Otherwise, the command operates on the current file only.

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.

D
     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 (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; see Printing).

Z
     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.

c
     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.

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

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

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

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

L
     Load the specified Emacs Lisp files (dired-do-load). See Lisp Libraries.

B
     Byte compile the specified Emacs Lisp files (dired-do-byte-compile).  See Byte Compilation (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 (see Identifier Search), it displays the *xref* buffer, where you can navigate between matches and display them as needed using the commands described in Xref Commands.

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 (see Xref Commands).  In particular, if you exit the query replace loop, you can use r in that buffer to replace more matches. See Identifier Search.

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 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:

  * If you use * surrounded by whitespace in the command string, then the command runs just once, with the list of file names substituted for the *.  The order of file names is the order of appearance in the Dired buffer.

     Thus, ! tar cf foo.tar * RET runs tar on the entire list of file names, putting them into one tar file foo.tar.

     If you want to use * as a shell wildcard with whitespace around it, write *"".  In the shell, this is equivalent to *; but since the * is not surrounded by whitespace, Dired does not treat it specially.

  * Otherwise, if the command string contains ? surrounded by whitespace, Emacs runs the shell command once for each file, substituting the current file name for ? each time.  You can use ? more than once in the command; the same file name replaces each occurrence.

  * If the command string contains neither * nor ?, Emacs runs the shell command once for each file, adding the file name at the end.  For example, ! uudecode RET runs uudecode on each file.

To iterate over the file names in a more complicated fashion, 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 ! 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 Dired Updating).

See Single Shell, for information about running shell commands outside Dired.

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 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 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 (see 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.

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 (see 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; see Setting Mark).  Otherwise, if the file at point has a backup file (see Backup), that is the default.

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):

i
     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 (see Setting Mark).  You can also use ^ to return to the parent directory in the same Dired buffer (see Dired Visiting).

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 Dired Updating).  You can also hide and show inserted subdirectories (see Hiding Subdirectories).

     [file dired-xtra.texi not included]

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 (see 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.

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

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

C-M-u
     Go up to the parent directory's header line (dired-tree-up).

C-M-d
     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-prev-dirline).

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.

M-$
     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 Subdirectories in Dired, for how to insert a subdirectory listing, and see Dired Updating for how delete it.

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.

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

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

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

s
     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 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.

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 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 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.

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.

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.

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 and 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.

Other Dired Features
====================

The command + (dired-create-directory) reads a directory 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.  See 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 headerline, 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.

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 (see 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.