The terminal experience inside emacs being what it is — ie really slow and kinda difficult to use —, I use iTerm2 as my main terminal. I put up three little emacs-lisp functions using AppleScript. They allow me to quickly switch between iTerm and spacemacs.

This one return the directory of the file currently opened. If it is a *scratch* buffer or something like that, it simply returns the home directory.

(defun get-file-dir-or-home () "If inside a file buffer, return the directory, else return home" (interactive) (let ((filename (buffer-file-name))) (if (not (and filename (file-exists-p filename))) "~/" (file-name-directory filename))))

This one allow me to cd to the directory of the file I am editing in emacs. If I am in a *scratch* buffer or something like that, it cd to the $HOME directory. It then focus the iTerm2 app.

(defun iterm-goto-filedir-or-home () "Go to present working dir and focus iterm" (interactive) (do-applescript (concat " tell application \"iTerm2\"

" " tell the current session of current window

" (format " write text \"cd %s\"

" (get-file-dir-or-home)) " end tell

" " end tell

" " do shell script \"open -a iTerm\"

" )) )

EDIT: 2016-09-05 I have taken Steve Purcell advice into account. Emacs already have a nice variable called default-directory . It is set to the directory of the buffer being edited, or nil if the buffer does not correspond to a file. So (or default-directory "~") does exactly what we want.

In case the directory name contains some strange characters like unicode chars or spaces, we need to escape that before passing to shell. That’s exactly what shell-quote-argument does. Unfortunately, we need to escape the escaping characters before passing it as arguments to an applescript statement. Thus the escaping madness of the next chunk. It now work flawlessly even in directory like “ ~/Users/me/Google Drive/pâte à pizza/ ”.

(defun sam--iterm-goto-filedir-or-home () "Go to present working dir and focus iterm" (interactive) (do-applescript (concat " tell application \"iTerm2\"

" " tell the current session of current window

" (format " write text \"cd %s\"

" ;; string escaping madness for applescript (replace-regexp-in-string "\\\\" "\\\\\\\\" (shell-quote-argument (or default-directory "~")))) " end tell

" " end tell

" " do shell script \"open -a iTerm\"

" )) )

This last one just focus the iTerm2 app, without modifying the working directory.

(defun iterm-focus () (interactive) (do-applescript " do shell script \"open -a iTerm\"

" ))

I have mapped it to SPC ' and SPC ? using general.el .