I have an existing dired buffer created of arbitrary files (basically as described here). How could I add additional files to this existing buffer?
The files may, or may not, be in the same directory as any files in the existing dired buffer.
See command diredp-dired-union
(bound to C-x D
, aka C-x S-d
) from Dired+. The doc string:
diredp-dired-union is an interactive compiled Lisp function in `dired+.el'.
(diredp-dired-union DIRBUFS &optional SWITCHES)
Create a Dired buffer that is the union of some existing Dired buffers.
With a prefix arg, read `ls' switches.
You are prompted for the Dired buffers. Use `C-g' when done choosing
them. Then you are prompted for the name of the new Dired buffer.
Its `default-directory' is the same as the `default-directory' before
invoking the command.
The selected Dired listings are included in the order that you choose
them, and each entry is listed only once in the new Dired buffer. The
new Dired listing respects the markings, subdirectory insertions, and
hidden subdirectories of the selected Dired listings.
However, in case of conflict between marked or unmarked status for the
same entry, the entry is marked. Similarly, in case of conflict over
an included subdirectory between it being hidden or shown, it is
hidden, but its contained files are also listed.
Update, in reply to your comment question -
Yes, for now you need to first open a new Dired buffer with only the file(s) to add. You can do that by providing a non-positive prefix arg (e.g. C--
) to dired
(e.g. C-- C-x 4 d
). (You are prompted for the files to list.) Then use diredp-dired-union
to combine that Dired listing with any other Dired listings.
I've been intending to add a command that does just what you request, directly, but haven't gotten around to it yet. (Maybe this will incite me to do so, when I get a moment.) I have this in my TODO list:
Add command to add files/dirs to the current Dired buffer. Use code similar to
diredp-dired-union-1
. It just needs to calldired
, passing a cons of:
- The current buffer name (might need to kill it first).
- The current default-directory plus the newly read files/dirs.
Or if the current listing was already from a cons (how to know:
dired-directory
is a cons or not) then just append the newly read files/dirs.
This should be simple to do; I just haven't gotten around to it. For now, just add files to a new Dired buffer and then union that with your other Dired buffer.
UPDATE #2
I've added such commands now.
Commands diredp-add-to-dired-buffer
and diredp-add-to-dired-buffer-other-window
, bound globally to C-x E
and C-x 4 E
, let you add arbitrary file and directory names to an existing Dired buffer.
Commands diredp-dired-union
and diredp-dired-union-other-window
, bound globally to C-x D
and C-x 4 D
(mentioned above), now also let you add arbitrary file and dir names (in addition to unioning Dired buffers), if you use a non-positive prefix argument (e.g. C--
).
The variable dired-directory
basically holds the value of the DIRNAME
argument in
(dired DIRNAME &optional SWITCHES)
, which you can manipulate at will. Don't forget to revert the buffer afterwards.