Frage

I'd like to have one single dired buffer alive at any time, and I want to be able to bring it to the front with a particular hot key. However, in order to do that, I would need the dired buffer to always have the same name. But the buffer name changes every time a new directory is loaded. Is there a way to tell dired to not change the name? I tried tweaking the reading and loading hooks but it didn't work.

Alternatively, is there a command to bring forward an existing dired buffer?

Thanks!

War es hilfreich?

Lösung

You probably don't want to do that. Some Dired features depend on the buffer name being associated with the directory.

A better approach might be to have a command that switches to the only Dired buffer, since you will be having only one at a time.

(defun switch-to-dired-buffer ()
  "..."
  (interactive)
  (let ((dbufs  (cl-remove-if-not
                 (lambda (bf)
                   (with-current-buffer bf
                     (derived-mode-p 'dired-mode)))
                 (buffer-list))))
    (switch-to-buffer (car dbufs))))

You can also get fancier and prompt for which Dired buffer if there is more than one etc.

Andere Tipps

Possible drawbacks notwithstanding, the following code does what you want:

(setq dired-default-buffer-name "*Dired*")

(defun dired-set-default-buffer-name ()
  "Set name of Dired buffer to `dired-default-buffer-name'.

If there already is a buffer with that name, kill it first."
  (when (get-buffer dired-default-buffer-name)
    (kill-buffer (get-buffer dired-default-buffer-name)))
  (rename-buffer dired-default-buffer-name))

(defadvice dired (after dired-set-default-buffer-name activate compile)
  "Set name of Dired buffer to default name when launching Dired."
  (dired-set-default-buffer-name))

(defadvice dired-find-file (after dired-set-default-buffer-name activate compile)
  "Set name of Dired buffer to default name when entering a subdirectory."
  (when (eq major-mode 'dired-mode)
    (dired-set-default-buffer-name)))

(defun dired-switch-to-default-buffer ()
  "Bring up default Dired buffer."
  (interactive)
  (switch-to-buffer dired-default-buffer-name))

;; Set up global key binding for bringing up default Dired buffer:
(global-set-key (kbd "C-c C-d") 'dired-switch-to-default-buffer)

Note that if you use ido-mode you will need to replace dired with ido-dired in the first defadvice. Also, dired-switch-to-default-buffer doesn't check if you have already launched Dired; you will have to modify that function a little if you want it to work in that case as well (e.g. by opening Dired in some default location).


EDIT

If you find that you are in fact using functionality that depends on the name of the Dired buffer being associated with the directory displayed, you can add this:

(setq dired-original-buffer-name nil)

(defun dired-restore-original-name ()
  (rename-buffer dired-original-buffer-name))

(defun dired-toggle-name ()
  (interactive)
  (if (eq (buffer-name) dired-default-buffer-name)
      (dired-restore-original-name)
    (dired-set-default-buffer-name)))

(global-set-key (kbd "C-c C-t") 'dired-toggle-name)

... and modify dired-set-default-buffer-name to look like this:

(defun dired-set-default-buffer-name ()
  "Set name of Dired buffer to `dired-default-buffer-name'.

If there already is a buffer with that name, kill it first."
  (when (get-buffer dired-default-buffer-name)
    (kill-buffer (get-buffer dired-default-buffer-name)))
  (setq dired-original-buffer-name (buffer-name)) ;; Keep track of original
                                                  ;; buffer name so we can
                                                  ;; restore it
  (rename-buffer dired-default-buffer-name))

This will allow you to toggle between the original name of the Dired buffer and the default name by issuing the associated key binding (currently: C-c C-t, but you can of course modify that to suit your needs).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top