I have my config files in ~/config/emacs which ~/.emacs is symlinked to.

I'm trying to figure out how to make the default "emacs root directory" $HOME in all contexts (unless overridden by a buffer-local variable), whether launching an ansi terminal, launching dired, or anything like that.

$ ls -al .emacs.d | perl -pe 's/^.*([.]emacs[.]d)/$1/'
.emacs.d -> ./config/emacs

My .emacs.d/init.el file contains the following snippet in an attempt to force emacs open stuff like dired and ansi-term in my home directory.

(cd (getenv "HOME"))

How do I change the effective root directory for ansi-term and everything else?

Here's a complete example that should reproduce the surprising behavior I'm talking about on OS X and Linux.

I have a directory ~/tmp/emacs

$ echo '(cd (getenv "HOME"))' > ~/tmp/emacs/cd.el
$ cd /tmp
$ emacs -q -l ~/tmp/emacs/cd.el

And then from inside emacs,

Launch the ansi terminal and accept the default shell

M-x ansi-term C-m C-m

print the current working directory from inside the terminal

$ pwd

I expected working directory to be $HOME.

  • My suggestion would be to use a scalpel instead of a sledge hammer -- i.e., How do I launch Emacs and open a dired-mode buffer in a specific directory from the terminal? How do I launch Emacs and open an ansi-term buffer in a specific directory from the terminal? Etc. All of this can be achieved without altering system-wide or Emacs-session-wide defaults. I find individual solutions to be better than global solutions, that will undoubtedly have corner-cases and cause some problems in the future (some of which will be difficult to trace). – lawlist Sep 12 '16 at 4:00
  • @lawlist I think the sledgehammer is what I'm after. I just want emacs to behave as if it was started somewhere else. The default behavior is great when the path to my .emacs.d/init.el file doesn't contain symlinks. I was actually kind of surprised that emacs considered ~/.emacs.d/.. to be its root/home directory rather than just ~. – Gregory Nisbet Sep 12 '16 at 4:17
  • Once Emacs is open you can set an environmental variable with setenv -- e.g., (setenv "HOME" "/private/tmp"). I don't know if that helps any, but I just tried it in an eshell buffer and was able to cd to the new HOME directory by typing cd ~ after using setenv. You can probably do that while launching Emacs -- there are ways to pass elisp while launching Emacs from the terminal. – lawlist Sep 12 '16 at 4:21

The answer is easy if you consider that default-directory (the variable returned by pwd) is always buffer local and returns the default directory of the current buffer.

With the following addition, you can watch, what's going on:

(cd (getenv "HOME"))
(message "Current dir: %s" (pwd))
(message "Current buffer: %s" (buffer-name))

Without any file loaded, the file will be executed in the context of the first buffer created and that's *scratch*. After the initialization (and the file loaded with -l belongs to this phase) a new buffer with the start message is created and this one doesn't get the default-directory from the *scratch* buffer.

tl;dr default-directory is per file and changes, when you call ansi-term (or pwd) with another buffer open. cd only changes the default-directory of the current buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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