Skip to main content
4 of 6
added 49 characters in body
Drew
  • 79.1k
  • 10
  • 123
  • 257

C-h f dired tells you the answer. Just pass to dired, as the DIRNAME argument, a list that has as its car the Dired buffer name you want (any string) and as cdr the list of file names you want listed in the buffer. Generally, you want to use absolute file names. For example:

(dired (list "My Dired Buffer Name*"     ; The Dired buffer name
             "/usr/foo/file1.el"         ; First file
             "/usr/bar/toto/some-file.c" ; Second file
             "/whatever/directory/"      ; Third is a directory
             "/a/file/somewhere.zzz"))   ; Fourth

Note that files you list need not even exist. They are listed anyway, and trying to access them from Dired visits buffers for them, just as if you had used C-x C-f. However, names of nonexistent directories are ignored (not listed).

If you use library Dired+ (dired+.el) then you can even do this interactively: just use a non-positive prefix argument (i.e., <= 0) with command dired. You are then prompted repeatedly for the directories and file names you want listed. You can use file-name wildcards (i.e., * for globbing), to include the matching files and directories. Use C-g to end inputting.

In other words, instead of listing a single directory, the Dired buffer can list any number of directories and file names, which can even belong to different directory trees.

(A non-negative prefix arg (i.e., >= 0) prompts you for the ls listing switches. So a zero prefix arg prompts you for both switches and files/dirs to list: first the ls switches and then the files/dirs.)

Drew
  • 79.1k
  • 10
  • 123
  • 257