symlink.png

How to Make Dired Show the Actual Path When Following Symlinks

Intro

When using Dired, I recently addressed an issue with symlink
navigation that was bothering me. By default, when accessing a symlink
in Dired, the path displayed at the top of the buffer remains the path
of the symlink itself rather than showing the actual destination
directory
.

The problem

When you navigate to a symlink in Dired mode, Emacs treats the symlink
as if it is the real directory. While this works functionally, it
fails to provide clarity about your actual location in the filesystem.

A solution

To improve this behavior, here is a custom function that checks if the
selected item is a symlink directory and, if so, displays the real
path:

;; Show real location of symlinks in Dired
(defun my-dired-find-file ()
  "Open file or directory and show its canonical path."
  (interactive)
  (let ((original (dired-get-file-for-visit)))
    (if (file-directory-p original)
        (find-alternate-file (file-truename original))
      (find-file original))))

(with-eval-after-load 'dired
  (define-key dired-mode-map (kbd "RET") 'my-dired-find-file))

How it works

  • Defines a custom function my-dired-find-file that replaces the
    default behavior when pressing enter in Dired
  • Uses file-truename to resolve the actual path when dealing with
    directories
  • Preserves normal behavior for regular files
  • Rebinds the enter key in Dired to use this new function

Now entering a symlink in Dired mode displays the actual destination
path at the top of the buffer, providing better clarity about your
actual location in the filesystem.

Let me know your thoughts in the comments below!

Comments

To comment, sign in with your GitHub account.