0

Suppose I launch emacs -nw from DIRECTORY_A in Terminal.app (osx 10.9.5). Then, inside emacs I navigate to DIRECTORY_B (e.g., using dired). At this point, if I close emacs (C-x C-c) or if I do M-x suspend-frame I find myself in DIRECTORY_A (of course this is standard behavior).

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). So that when I exit emacs I would end-up in DIRECTORY_B.

I (naively) attempted achieving this by using a shell command:

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

In the *Messages* buffer I can see

(Shell command succeeded with no output)

but when I exit emacs I am still in 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). But if I do M-x suspend-frame and then issue env from the prompt in the terminal I get 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).

Dimitar
  • 1
  • 2
  • 1
    possible duplicate of [Emacs, change of $PWD variable](http://stackoverflow.com/questions/17123765/emacs-change-of-pwd-variable) – l'L'l Apr 02 '15 at 04:16
  • Thanks for the suggestion but I don't think that this resolves my problem. – Dimitar Apr 02 '15 at 18:25

2 Answers2

0

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.

Dimitar
  • 1
  • 2
0

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)))