# Dired history in Emacs

Emacs is reasonably consistent across its major-modes in its common usage patterns and keybindings. For example, C-M-a and C-M-e consistently move you to the beginnings and ends of functions in various language modes, and C-c C-z moves the focus to REPLs and back. In read-only modes n and p move the cursor up and down, and w generally copies the path, address or URL of the buffer to the kill ring.

One of these consistent (but less well known) keybindings is for going back and forward across nodes in a tree, arranged into a list: l and r traverse your history and “forward” history of visited help pages in help-mode, info nodes in Info-mode Info mode takes this further. L gives you an index of your visited nodes, which is itself placed into history! , web pages in eww, locations in pdf-tools and probably others. I think of this as moving [l]eft or [r]ight across a traversal path.

But there are some omissions. I frequently find myself using l and r in dired buffers to navigate my history of visited dired buffers, where it annoyingly does nothing. I assumed this feature would be part of one of the many, many Emacs packages that extend dired, but while there are big total makeovers (like ranger.el) for dired that include it, there wasn’t anything simple.

So I wrote a helper: dired-hist. It’s very basic (< 50 LOC) and will remain so, although I might add some customization options in time.

Davide Masserut
2022-02-24
Great package! Have you considered upstreaming it to Emacs?
Karthik
2022-02-24

@Davide: Thanks. I’d say it needs quite a bit of testing before I can do that. Maintaining both the past and future histories involves edge cases I haven’t bothered handling yet. Web browsers simply discard your future navigation history as soon as you start a new branch. Emacs tries to keep this around, so it takes more work.

dired-hist also doesn’t accommodate the dired window preferences of users, and probably won’t in the future either. The behavior of l and r should be different when the user has set each dired instance to open in a new window vs same window, etc.

arthur
2022-02-25

Web browsers simply discard your future navigation history as soon as you start a new branch. Emacs tries to keep this around, so it takes more work.

I think you are thinking too much here :-).

Take a look at help-mode.el in lisp directory of Emacs. You can just press C-h V help-xref-stack or C-h v help-xref-forward-stack to see how history is implemented in built-in help. I haven’t looked at how they do in info-mode, but back and forward there function similar. In my opinion, it is enough to keep just back/forward history in this case. Otherwise, if you would really like to have undo/redo similar to Emacs, you could create a hidden buffer and record Dired paths by simply insert/remove text at the end in order to use Emacs built-in undo/redo. You can create a “hidden” buffer by preceding it with two spaces in the name, something like " diredhist". Hope it helps.

Karthik
2022-03-02

@arthur help-xref-stack is what I modeled dired-hist after.

Otherwise, if you would really like to have undo/redo similar to Emacs, you could create a hidden buffer and record Dired paths by simply insert/remove text at the end in order to use Emacs built-in undo/redo. You can create a “hidden” buffer by preceding it with two spaces in the name, something like " diredhist". Hope it helps.

This is a great idea, thank you. I’ll experiment a bit.