简体   繁体   English

从emacs -nw更改osx terminal.app中的目录

[英]change directory in osx terminal.app from emacs -nw

Suppose I launch emacs -nw from DIRECTORY_A in Terminal.app (osx 10.9.5). 假设我从Terminal.app(osx 10.9.5)的DIRECTORY_A启动emacs -nw Then, inside emacs I navigate to DIRECTORY_B (eg, using dired). 然后,在emacs中,我导航到DIRECTORY_B (例如,使用Dired)。 At this point, if I close emacs ( Cx Cc ) or if I do Mx suspend-frame I find myself in DIRECTORY_A (of course this is standard behavior). 在这一点上,如果我关闭emacs( Cx Cc )或执行Mx suspend-frame我会发现自己处于DIRECTORY_A (当然,这是标准行为)。

I would like to be able to execute a command from within emacss that changes DIRECTORY_A (the directory from which I invoked emacs -nw ) to DIRECTORY_B (the current directory in emacs). 我希望能够从emacss内部执行将DIRECTORY_A (我从其调用emacs -nw DIRECTORY_B )更改为DIRECTORY_B (emacs中的当前目录)的命令。 So that when I exit emacs I would end-up in DIRECTORY_B . 这样,当我退出emacs时,我将以DIRECTORY_B结尾。

I (naively) attempted achieving this by using a shell command: 我(天真)尝试通过使用shell命令来实现这一点:

(defun my-cd-to-current-dir ()
  (interactive)
  (shell-command (concat "cd " (expand-file-name "."))))

In the *Messages* buffer I can see *Messages*缓冲区中,我可以看到

(Shell command succeeded with no output)

but when I exit emacs I am still in DIRECTORY_A . 但是当我退出emacs时,我仍然在DIRECTORY_A

Clarification: When I change directory within emacs, the value of the environmental variable PWD (accessed via M-! env ) reflects correctly the current directory ( DIRECTORY_B ). 澄清:当我在emacs中更改目录时,环境变量PWD的值(可通过M-! env访问)正确反映了当前目录( DIRECTORY_B )。 But if I do Mx suspend-frame and then issue env from the prompt in the terminal I get DIRECTORY_A . 但是,如果我执行Mx suspend-frame ,然后从终端的提示符下发出env ,则会得到DIRECTORY_A Hence, changing the value of PWD from within emacs doesn't seem to solve the problem (it is possible that I am doing something wrong). 因此,从emacs中更改PWD的值似乎不能解决问题(可能我做错了事)。

As a workaround, currently I am using 解决方法,目前我正在使用

(defun my-open-terminal-in-current-dir ()
  (interactive)
  (shell-command (concat "open -b com.apple.terminal " (expand-file-name "."))))

to open a new terminal application in the current directory. 在当前目录中打开一个新的终端应用程序。 Unfortunately, this is not very convenient so I would appreciate any ideas on how to solve the original problem. 不幸的是,这不是很方便,所以我将对解决原始问题的任何想法表示赞赏。

Hi here is a another solution. 您好,这是另一种解决方案。

;; thx http://superuser.com/questions/466619/open-new-terminal-tab-and-execute-script
;; thx http://qiita.com/ganmacs/items/cfc5f9c2213a6a9e6579

(defun cd-on-terminal (&optional command)
  "Change directory to current buffer path by Terminal.app.  COMMAND is execute after cd."
  (interactive)
  (util/execute-on-terminal
   (concat (format "cd %s" default-directory) command)))

(defun util/execute-on-terminal (command)
  "Change directory to current buffer path by Terminal.app.  COMMAND."
  (interactive "MCommand: ")
  (do-applescript
   (format "tell application \"Terminal\"
activate
tell application \"System Events\" to keystroke \"t\" using command down
repeat while contents of selected tab of window 1 starts with linefeed
delay 0.01
end repeat
do script \"%s\" in window 1
end tell"
           command)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM