0

I'd like to have Dired pop up, either as a side window (like Treemacs) or just to switch to its buffer (in the main window) with a keystroke.

I've already changed its behavior to reuse the same buffer instead of opening a new one per dir.

I know I can just use switch buffer.. but Dired's buffer name keeps changing to reflect the current dir - so it's not as seamless as I would like. I suspect it already exists in Dired+ or something, but I couldn't find it.

2 Answers 2

1

The question isn't too clear. C-x d pops up Dired. If you want it to do that with a single keystroke then just bind a single keystroke key sequence to command dired (or dired-other-window).

Or maybe you mean that you want a command/key that pops up Dired for the current directory (aka default-directory)? These commands do that - they just call dired, passing it the current directory as arg:

(defun dired-here ()
  (interactive)
  (dired default-directory))

(defun dired-here-other-window ()
  (interactive)
  (dired-other-window default-directory))
2
  • Thanks! Though that's not what I meant. I'll clarify. I want to have dired opened to some arbitrary dir, then to be able to conveniently hide it (go back to the buffer I was visiting before calling dired) and move back to it again later - while it's still displaying the same arbitrary dir.
    – user39849
    Feb 1, 2023 at 8:55
  • (1) Questions need to stand on their own, saying clearly what's requested - comments can be deleted at any time. (2) Your comment is answered by just using C-x b followed by M-p (perhaps repeated) or M-r. But the last part, while it's still... isn't clear at all: move back to the Dired buffer while it's still displaying the same arbitrary dir? What's meant by moving back to something that's still displayed? I answered the question as posed. If you really meant to pose a different question the do that separately - but please specify it clearly. Thx.
    – Drew
    Feb 1, 2023 at 16:02
1

Not sure whether you mean toggling the last visited dired buffer or some other dired buffer.

For the latter you can just use bookmarks. Set a bookmark to a dired buffer with M-x bookmark-set, call it my my-dired-buffer and keep re-bookmarking the same name to different buffers. Bind a key to jump to it:

(define-key global-map (kbd "<f7>")
  (lambda () (interactive)
    (if (equal (expand-file-name (bookmark-location "my-dired-buffer"))
               (expand-file-name default-directory))
        (previous-buffer)
      (bookmark-jump "my-dired-buffer"))))

If you are already in the bookmarked buffer this will take you back to the last buffer.

For the former, I think you just need to keep track of the last dired buffer and wrap your dired jump function with something that updates the tracking variable:

(setq my-last-dired-buffer nil)

(defun my-open-dired-bufer ()
  (interactive)
  (dired-jump)
  (setq my-last-dired-buffer (current-buffer)))

(define-key global-map (kbd "<f7>")
  (lambda () (interactive)
    (if (equal (current-buffer) my-last-dired-buffer)
        (previous-buffer)
      (switch-to-buffer my-last-dired-buffer))))

Note this only keeps track of buffers that were opened with my-open-dired-buffer.

You can also add a setter to set the current buffer as the last buffer:

(defun my-last-dired-buffer-set ()
  (interactive)
  (setq my-last-dired-buffer-set (current-buffer)))

In fact, you can just use this instead of using bookmarks and just don't wrap the dired-jump function and set the tracking variable with the setter manually.

You can complicate this quite a bit, but I am not sure it is worth it. It is very easy to change buffers in Emacs especially if you are using Evil mode, maybe with something like Doom Emacs.

Note, I haven't tested the above code heavily. But you get the idea I hope.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.