1

I added a hook to my init.el to launch Emacs on eshell.

;;start on eshell
(add-hook 'emacs-startup-hook 'eshell)

The thing is, I often launch Emacs from the terminal and would love to have eshell working directory being the working directory of the terminal from which I launched Emacs.

Let's say I'm in a directory X, and launch Emacs

~/X $ emacs

I want this to happen

Welcome to the Emacs shell
~/X $ 

For now, I have added

(cd "~")

to my init.el, as a temporary solution (changes emacs path to home), but it's not good enough.

Edit

I want to run Emacs in its gui.

Edit 2

Chris's answer worked if not using open -a emacs to launch the application. But just with the executable path instead.

Felix D.
  • 2,180
  • 1
  • 23
  • 37

1 Answers1

2

You can detect that Emacs is running in a terminal when the function display-graphic-p returns nil, and you can get the directory from which you invoked Emacs from the default-directory variable.

So something like

(when (not (display-graphic-p))
  (cd default-directory)
  (eshell))

in your init should change to the "current" directory and then launch eshell when Emacs is invoked from the terminal.

If you always wish to invoke eshell you should be able to remove (eshell) from the code and simply keep your emacs-startup-hook.

Edit: Replaced variable window-system with call to function display-graphic-p as recommended in this answer.

Edit 2: If you simply want to modify your emacs-startup-hook to change to whatever directory you invoke Emacs from and then launch eshell (regardless of the windowing system), you can use something like

(add-hook 'emacs-startup-hook
          (lambda ()
            (cd default-directory)
            (eshell)))
Community
  • 1
  • 1
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • Thanks a lot for your input Chris. But I want to run emacs with its gui actually. Sorry for being unclear about that. I tried with only (cd default-directory) but it didn't work, as I sadly expected.. – Felix D. May 22 '14 at 19:38
  • @FelixD., I must have been confused by "launch Emacs from the terminal". I have updated my answer to show how to use an anonymous function in your `emacs-startup-hook` that should do what you want. – ChrisGPT was on strike May 22 '14 at 19:59
  • Thank you, but it's still not working. Could it be due to my terminal function that calls emacs? http://i61.tinypic.com/30abaqb.png , but I doubt it.. – Felix D. May 22 '14 at 20:03
  • @FelixD., possibly, I don't have a Mac to test on. If you run Emacs using `/path/to/your/Emacs.app` instead of using your shell wrapper, does it behave as expected? – ChrisGPT was on strike May 22 '14 at 20:11
  • yes thanks!! I'll guess I'll have to fix that. The problem is, oddly, when I open emacs the "classic way", the terminal seems to be pending while Emacs is active. With open -a emacs instead, the terminal acts normally and gives me a new prompt right after opening emacs. – Felix D. May 22 '14 at 20:22
  • @FelixD., glad you got it working. You can probably update your wrapper to include something like `--chdir $(pwd)` as an argument to Emacs, which should make your `open`ed Emacs respect your working directory. – ChrisGPT was on strike May 22 '14 at 20:25
  • Thanks! It doesn't work for now but I will overcome that fast enough! – Felix D. May 22 '14 at 20:30