1

I would like to configure dired so its navigation would behave as classic file managers do. I.e. binding

  • M-<left> to go backward in navigation history, while staying within the current dired buffer
  • M-<right> to go forward in navigation history, while staying within the current dired buffer
  • M-<up> to go to parent directory, while staying within the current dired buffer
  • RET to enter a directory, while staying within the current dired buffer

Is it possible? If yes, how could it be done?

1
  • Comments have been moved to chat; please do not continue the discussion here. Before posting a comment below this one, please review the purposes of comments. Comments that do not request clarification or suggest improvements usually belong as an answer, on Emacs Meta, or in Emacs Chat. Comments continuing discussion may be removed.
    – Dan
    Commented Apr 16, 2023 at 13:40

2 Answers 2

1

See below, for correction after understanding that by previous/next you meant navigation history and not movement to previous/next line.


(define-key dired-mode-map (kbd "M-<left>") #'dired-previous-line) ; Or `diredp-previous-line`, if you use Dired+

(define-key dired-mode-map (kbd "M-<right>") #'dired-next-line) ; Or `diredp-next-line`, if you use Dired+

(define-key dired-mode-map (kbd "M-<up>") #'dired-up-directory) ; Or `diredp-up-directory`, if you use Dired+

RET already does what you request.

There are standard key bindings for each of those actions: p, n, ^, RET. You might try learning them. Keys p (or C-p) and n (or C-n) are often bound in Emacs buffers to "previous" and "next commands appropriate to the major mode.

Library Dired+ enhances the first three of those in minor ways:

  • p and n optionally wrap around when at the beginning/end of the file listing

  • On MS Windows, if you're already at the root directory, ^ visits a navigable list of Windows drives.


UPDATE, after you wrote your comment underlining that you mean browsing/navigation history and not previous/next line in the buffer:

For "browsing/navigation history" it depends on what you mean. That can mean almost anything in Emacs. Neither Dired+ nor vanilla Dired offer commands for navigation history. And that term is too vague in a question to mean anything.

But keep in mind that the default behavior of Dired (and Dired+) creates a new buffer for each directory or file you visit.

You say (see below) that that's what you don't want. But with that default behavior you can easily and quickly get to any directory or file that you've previously visited! Just use C-x b with M-p or M-n or M-r or M-s. Or use completion against the buffer name.

Other than that, here are usual, general Emacs ways to navigate through your browsing history. They're not at all specific to Dired.

  • When you input a command with M-x, M-p gives you the previous command (repeat to go back in the history).

  • You can often go back in your history of keys/commands using C-x ESC ESC (or C-x M-ESC, which is the same thing). It shows you the Lisp expression for the previous "complex" command, and (again) you can use M-p repeatedly to go back further in the history.

  • Use C-u C-SPC to move backward among the places in the current buffer where you've set the mark (using C-SPC or any other command that sets the mark). Use C-u C-x C-@ to move among the places in different buffers where you've set the mark (one mark per buffer). See C-h k C-SPC.


As for your comment that Dired commands that visit a file or directory create a new buffer: yes, of course they do. That's generally what Emacs users want.

But there's an easy way to not have RET do that: just use a instead. That's command dired-find-alternate-file, which just uses find-alternate-file (which is bound globally to C-x C-v).

With Dired+ you can get this behavior by default, and for all commands that visit directories -- e.g., ^ in addition to n and p and RET. Just put this in your init file:

(diredp-toggle-find-file-reuse-dir 1)

And you can toggle this behavior any time, using C-M-R (aka C-M-S-r).

6
  • RET opens a new dired buffer.
    – crocefisso
    Commented Apr 14, 2023 at 19:06
  • Also dired-up-directory opens a new buffer. And I don't exactly get what dired-previous-line and dired-next-line do. But they don't seem to go backward and forward in browsing history.
    – crocefisso
    Commented Apr 14, 2023 at 19:17
  • I didn't try to evolve my question in the comment. I just explained why the provided solution didn't answer the question. Also, the "navigation history" part was already in my question before I started commenting it.
    – crocefisso
    Commented Apr 14, 2023 at 20:10
  • I added dired+ in my solution. What I miss with dired+ is the fortification provided by denote-dired-mode. Regarding what I meant by navigation history, it is something similar to web browsers behavior when you go backward and forward in browsing history (also bound with M-left and M-right). File managers such as Thunar also have this functionality (also bound with M-left and M-right).
    – crocefisso
    Commented Apr 14, 2023 at 23:42
  • Define browsing history. That's the problem. In my answer I noted a few different kinds of "browsing" (there are many more). A web browser has one kind: URL1, URL2, URL3,...URLN. Just URLs. Same thing with Emacs Info nodes - just node after node. But what if you also mix in searching, moving over various kinds of things, etc. You'll note that a web browser doesn't include navigating by searching on a web page (e.g. C-f) in its forward-and-back browsing history. In Emacs there are any number of different ways and places to move - there's no single, simple notion of browsing navigation.
    – Drew
    Commented Apr 15, 2023 at 2:16
0

Regarding the single buffer part of the question (bullet point 3 and 4). Two additional packages can provide with a solution: dired-single or dired+.

Firstly you have to bind dired-up-directory to M-<up>

(use-package dired
  :ensure nil
  :config
  (define-key dired-mode-map (kbd "M-<up>") #'dired-up-directory))

Then, if you prefer to use dired-single, add:

(use-package dired-single
  :config
  (define-key dired-mode-map [remap dired-up-directory]'dired-single-up-directory)
  (define-key dired-mode-map [remap dired-find-file] 'dired-single-buffer))

If you prefer to use dired+, add:

(use-package dired+
  :ensure nil
  :init (add-to-list 'load-path "PATH")
  :config (diredp-toggle-find-file-reuse-dir 1))

Where "PATH" is the path where dired+.el is located.

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.