2

I have ~/bin -> ~/some/long/path/bin. When I open ~/bin in Dired, it tells me at the top that it is looking at /Users/username/bin, i.e. it expands '~' in the current directory printed on the first line.

Is there a way for Dired to expand the symlinks in the current directory too, and show the underlying 'physical' location as /Users/username/some/long/path/bin? This would be in addition to the short directory name already shown on the first line.

I suppose I could modify Dired to call file-truename at the right place to accomplish this, but I wonder if I missed anything in dired.el or if it is already provided by one of Dired extensions. Surely there are others who would like to know.

1 Answer 1

1

Change your ls switches, using option dired-listing-switches or by using a prefix arg with command dired (in which case you're prompted for the ls switches for that particular Dired listing).

Depending on your ls (use M-x man RET ls RET to see what yours offers), you can use a switch such as these:

-H, --dereference-command-line
              follow symbolic links listed on the command line

--dereference-command-line-symlink-to-dir
              follow each command line symbolic link
              that points to a directory

       -L, --dereference
              when showing file information for a symbolic link, show informa-
              tion for the file the link references rather than for the link
              itself

In a comment you mention that you're really interested in the header lines of directories (presumably including inserted subdir listings) - showing whether they are symlinks and if so what they link to.

In that case no, as far as I know that's not available with Dired. You might want to file an enhancement request, using M-x report-emacs-bug.


Or you could code it yourself, using function file-symlink-p to test whether the directory is itself a symlink.

You could then advise or redefine function dired-insert-directory, where it inserts the directory header line, to do what you want. That's this code from file dired.el:

;; Note that dired-build-subdir-alist will replace the name
;; by its expansion, so it does not matter whether what we insert
;; here is fully expanded, but it should be absolute.
(insert "  " (or (car-safe (insert-directory-wildcard-in-dir-p dir))
                 (directory-file-name (file-name-directory dir))) ":\n")

However, I think it's quite likely that will break several Dired features that depend on the current format for the header line and use the directory name there for multiple purposes. (E.g., see that comment.)


Ask yourself how important it is that you see the symlink destination in the header line.

If what's important to you are (1) to have some indication that the directory in the header line is a symlink, and (2) then be able to find out what it symlinks to, then you can do this:

  1. Add font-locking to color or whatever the directory header line when the directory is a symlink.

  2. Add a command to use with the cursor on that header line, which echoes the symlink destination.

9
  • Thanks, but I was asking about the current directory (the first line in the Dired buffer). I can see the symlinks fine in the ls output. (Clarified the question.)
    – spepo
    Mar 19, 2022 at 23:55
  • I see. No, as far as I know that's not available with Dired. You might want to file an enhancement request, using M-x report-emacs-bug.
    – Drew
    Mar 20, 2022 at 4:42
  • I updated my answer to address what you say in your comment is your real request.
    – Drew
    Mar 20, 2022 at 5:19
  • Drew, I wonder what would break if the first line in Dired buffer was changed. You elude to some other comment, but I don't see the link to it. If that is something I would rather not break (or adapt), I might be satisfied with a message in the minibuffer displaying the 'truename' of the directory. Do you see any issues with that?
    – spepo
    Mar 20, 2022 at 6:51
  • 1
    @BosonBear: Dired+ doesn't do anything special wrt symlinks. It does what vanilla Dired does in this regard.
    – Drew
    May 12, 2022 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.