14

I just upgraded Emacs to 27.1 using brew cask. One noticeable change is that when I launch the app and c-x c-f the directory is / instead of ~/.

This really bothers me, is there a way to change it back?

14
  • The directory is the root directory?? What's the exact directory that you have to use to launch the app? As for ~/ that a shortcut to your home directory. ~/ = $HOME = /Users/DreamLinuxer/ See Bash Tilde Expansion for more info.
    – Allan
    Commented Aug 12, 2020 at 19:11
  • I just open it from spotlight, in 26.3 it is always my home directory but after upgrading it becomes /. Commented Aug 12, 2020 at 19:41
  • I'm not understanding what you mean by "it." What is "it is always..." and "after upgrading it becomes..." referencing? Where are you seeing this? Can you post a screen shot?
    – Allan
    Commented Aug 12, 2020 at 19:43
  • 2
    When I open emacs from spotlight and try to open a file using c-x c-f this is what shows up i.imgur.com/RM1sQih.png . But in 26.3 the path is ~/ instead of /. Commented Aug 12, 2020 at 20:01
  • does this help ? stackoverflow.com/questions/60464/… or emacs.stackexchange.com/questions/4253/…
    – anki
    Commented Aug 12, 2020 at 20:03

3 Answers 3

12

I had the same issue after installing Emacs 27.1, my default directory became "/"

I could fix it by adding the following to my .emacs file.

(setq default-directory "~/")
(setq command-line-default-directory "~/")
5
  • I tried adding (setq default-directory "~/") to my .emacs, but when I c-x c-f the default directory is still /. But weirdly, if I switch to another buffer like *scratch* then it becomes ~/. So I think maybe this is related to some unknown change of *GNU Emacs buffer. Commented Aug 14, 2020 at 20:18
  • 2
    Ah, I inhibit my splash screen so didn't noticed. To get the splash screen to also start in ~/ add the additional line (setq command-line-default-directory "~/"). I'll update the answer
    – Brice
    Commented Aug 15, 2020 at 6:47
  • 3
    This has the drawback that emacs -nw foo.c will open ~/foo.c and not ./foo.c. Commented Aug 19, 2020 at 12:45
  • 1
    For spacemacs users this needs to be in the dotspacemacs/user-init section not user-config as it needs to be loaded before subsequent packages. Sigh. Seems they've known about this forever but still released it lists.gnu.org/archive/html/help-gnu-emacs/2020-04/msg00240.html Commented Oct 4, 2020 at 0:05
  • @TurePålsson The problem seems gone using 'emacsclient -nw -a "" -c "$@"' -- I alias it in my .zshrc file, alias sp='emacsclient -nw -a "" -c "$@"'. Files seem to open correctly from the command line. Commented Nov 8, 2020 at 15:25
3

I have worked around this issue by editing /Applications/Emacs.app/Contents/MacOS/Emacs, inserting

Dir.chdir(ENV['HOME'])

before the call to exec at line 69

exec [emacs[:exe], emacs[:exe]], *ARGV 

This is obviously just a temporary workaround, but it restores the previous behaviour of Emacs 26 without modifying any of my configuration or affecting Emacs command line calls, since it only modifies the Finder launch.

I've also posted this at https://github.com/caldwell/build-emacs/issues/98#issuecomment-720609277 EDIT: “Looks like the fix will be in 27.2.” https://debbugs.gnu.org/cgi/bugreport.cgi?bug=44446#11

1
  • This almost worked for me, but when I run from the command-line, I need relative path names to work. I changed Dir.chdir(ENV['HOME']) to: if ENV['PWD']\n Dir.chdir(ENV['PWD'])\n else\n Dir.chdir(ENV['HOME'])\n end . That did the trick! (sorry about the bad formatting) Commented Jan 18, 2021 at 17:11
3

Following the lead of @rptb1 , I modified /Applications/Emacs.app/Contents/MacOS/Emacs, inserting

  if ENV['PWD']
    Dir.chdir(ENV['PWD'])
  else
    Dir.chdir(ENV['HOME'])
  end

before the call to exec:

exec [emacs[:exe], emacs[:exe]], *ARGV 

The advantage of this over the previous answer is that if you start emacs with the open command, you'll get the current working directory, which is normally what you'd expect, instead of your home directory. This is critical for me because I often use the command-line to open a file in the current directory.

Although this is mostly a duplicate of his answer, I am posting it as a new answer because my comment on his answer came out so badly formatted. I hope the upcoming bug fix preserves the current directory, as emacs 26 did.

3
  • Did you get this behaviour from open before this bug was introduced?
    – rptb1
    Commented Jan 20, 2021 at 15:10
  • @rbtb1: The behavior I got from open before I upgraded to emacs 27.1 more-or-less matches the behavior I now get with 27.1 combined with this script change. Does that answer your question? Commented Jan 21, 2021 at 17:17
  • Yes thanks. I use emacsclient for this kind of thing so I never noticed it. Let's hope 27.2 comes out soon so we can discard these dirty hacks :)
    – rptb1
    Commented Jan 21, 2021 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .