I am using, per the Mastering Emacs blog, the following OSC7 command for directory tracking (in bash):

printf "\e]7;file://%s%s\e\\" "$HOSTNAME" "$PWD"

This mostly works, however the Mac has some strange opinions about how to set $HOSTNAME. If you're using public wifi, you can – via reverse DNS lookup – end up with someone else's computer name in $HOSTNAME. (At this moment, my hostname is "Fidels-MBP" instead of the correct "ebenezer2023.local".)

Fine, I thought, I don't need the hostname anyway and the file: URI scheme allows you to omit it. Except neither of those will work. What I had to do was this:

function myprompt () {
    real_hostname=$(scutil --get ComputerName)
    printf "\e]7;file://%s%s\e\\" "$real_hostname.local" "$PWD"

This seems to work, but it seems fragile. (Appending .local is required.)

My hunch is that both a null hostname and "localhost" resolve using the same mechanism that makes $HOSTNAME wrong, in which case I guess there's nothing to be done. But could it be a bug?

  • What's the question? Not clear.
    – Drew
    Commented Feb 11 at 0:32

1 Answer 1


Arguably it is a bug. The function ansi-osc-directory-tracker in Emacs expects the host part of the url to be nil if it was left unspecified, but the function which actually parses urls (url-generic-parse-url) sets it to the empty string instead. It does set the port to nil if it is an empty string, but it doesn’t do that for the host.

Which one is wrong? It is hard to say. But you could use M-x report-emacs-bug to send an email to the developers with that information and let them hash it out. In the mean time fixing your prompt as you have done is the right thing to do.

  • Actually, it's worse than I supposed. It turns out that even if I hardcode the hostname in the OSC7 prompt, when I'm connected to a wifi network that persuades OSX that the hostname is "DESKTOP-TEBA9J7", directory tracking fails (despite scutil giving the right hostname). I guess something thinks I'm "really" on DESKTOP-TEBA9J7 and so the reference to a file: URI with a different hostname fails. I'll dump the issue to M-x report-emacs-bug. Thanks. Commented Feb 13 at 23:39
  • That part probably isn’t an Emacs problem. It just means that when Emacs asks for the address that corresponds to that name, it gets no answer or the wrong answer. You might be able to work around it by putting in an ip address instead of a name though.
    – db48x
    Commented Feb 14 at 0:27

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.