0

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.

4

1 回答 1

2

display-graphic-p当函数返回时,您可以检测到 Emacs 正在终端中运行nil,并且您可以从default-directory变量中获取调用 Emacs 的目录。

所以像

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

在您的 init 中应该更改为“当前”目录,然后eshell在从终端调用 Emacs 时启动。

如果您总是希望调用eshell,您应该能够(eshell)从代码中删除并简单地保留您的emacs-startup-hook.

编辑:用此答案中推荐的window-system函数调用替换变量。display-graphic-p

编辑 2:如果您只是想修改您emacs-startup-hook以更改为您调用 Emacs 的任何目录然后启动eshell(无论窗口系统如何),您可以使用类似

(add-hook 'emacs-startup-hook
          (lambda ()
            (cd default-directory)
            (eshell)))
于 2014-05-22T18:13:19.530 回答