2

I'd like to do something like this

(global-set-key (kbd "C-c t") (kbd "M-! /usr/bin/open -a Terminal . RET"))

... in order to open a (real, the) terminal from the current working directory in macOS with GNU Emacs 26.1. Although /usr/bin/open -a Terminal . opens a new terminal from within macOS, the above key binding doesn't work (nothing happens). Why? The buffer *Messages* shows /bin/bash: /usr/bin/open-aTerminal.: No such file or directory, so spaces seem to be ignored.

1 Answer 1

4

You're missing some SPCs there. You can save a macro with F3/F4 and then dump it with M-x kmacro-edit-macro (C-c C-k RET) to inspect the output. For example:

M-!         ;; shell-command
xfce4-terminal      ;; self-insert-command * 14
SPC         ;; self-insert-command
-e          ;; self-insert-command * 2
SPC         ;; self-insert-command
bash            ;; self-insert-command * 4
RET         ;; newline

Nevertheless, there is no need to use that "macro like" binding. It's better to just call shell-command. Even if Terminal didn't support passing a working directory you can exploit the fact that shell-command is running a full-fledged shell for you and simply cd before launching the terminal emulator:

(global-set-key (kbd "C-c t")
                (lambda () (interactive)
                           (shell-command "cd .; xfce4-terminal")))

Btw, there is no point in starting the terminal in the current working directory. It's where any new process will be by default.

1
  • ... awesome, thanks a lot! Mar 20, 2019 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.