2026-04-13 Binding TAB in Dired to something useful

I’m old enough to remember Norton Commander for DOS. Despite that, I never used Midnight Commander nor Sunrise CommanderDired is still my go-to file manager these days. In fact, Dired has a feature which seems to be inspired by NC: when there are two Dired windows, the default destination for copying, moving and symlinking is “the other” window.

Surprisingly, another feature which would be natural in an orthodox file manager is absent from Dired: TAB is not bound by its keymap. This means that pressing it results in a “Buffer is read-only” error. I figured that making TAB switch to the other Dired window would be pretty cool – and definitely more useful than just erroring out.

(defun dired-cycle-dired-windows ()
  "Switch to the next Dired window in the selected frame."
  (interactive)
  (select-window
   (cadr (seq-filter
          (lambda (window)
            (eq (buffer-local-value
                 'major-mode (window-buffer window))
                'dired-mode))
          (window-list)))))

(keymap-set dired-mode-map "TAB" #'dired-cycle-dired-windows)

And that’s it! From now on, pressing TAB in a Dired buffer will cycle through all Dired buffer in the currently selected frame.

Now this command could be made more elaborate. For example, it could go in another direction with a prefix argument. (No point for me, though – very rarely do I have more than two Dired windows open at the same time.) It could cycle through all Emacs frames (doable, but much more work for little gain). It could switch to another Dired buffer in the same window if there’s only one (clever, but I’m not sure if useful). That’s not the point – the point I’m making over and over again is that creating little but useful commands like this is a breeze in Emacs. In fact, creating this one took me maybe 10 minutes – and it would be under 5 minutes if not for the fact that I had to look up a few things, like how to get the buffer given the window, how to get major mode given a buffer etc.

As usual, I recommend the late Robert J. Chassell’s Introduction to Programming in Emacs Lisp if you want to learn to write commands like this and mold Emacs to your needs – and my book about Emacs Lisp if you want to see a few more (and more advanced) examples.

That’s it for today, until next time!

CategoryEnglish, CategoryBlog, CategoryEmacs, CategoryDired