I'd like to run a command in a running iterm tab (or terminal, whatever) from elisp. I don't want to use a terminal emulator running under emacs, since I've yet to find one that handles output as well as a standalone terminal emulator.

Is this possible?

I basically need this, but for a process that isn't running under emacs:

(term-simple-send proc "echo hello")
  • Have you tried M-x term? This one seems to work quite like a real terminal. – cYrus Jul 6 '12 at 17:03
  • 2
    I use ansi-term right now, and it works ok but not nearly as well as Terminal or iTerm. The buffer scrolls slower on output, sometimes gets "stuck" where the last output line isn't at the bottom of the buffer, I haven't figured out how to get it to use a different smaller font, C-x b occasionally puts me in the test buffer accidentally, trouble with unicode. I'd just like to use a native terminal if it's a possible thing. – spike Jul 6 '12 at 17:29
  • I think what both the op and I are looking for is something like this: github.com/epeli/slimux (I used this before i switched from Vim... there has to be an emacs way of doing this?) – joefromct Jul 21 '15 at 20:16
  • @joefromct check out my self-answer below. – spike Jul 21 '15 at 20:38

If you are really just concerned about output quality, why not call uxterm with the -hold option as so?

(defun external-xterm-shell-command (COMMAND)
  "Run a terminal command in an external xterm window."
  (interactive "sShell Command: ")
  (shell-command (concat "uxterm -hold -e " COMMAND)))
  • The running tab part is critical for me. I'm using this for automated testing and i want an always visible terminal that keeps getting reused, and doesn't steal focus when I run a test. Can you send a command to a running uxterm instance? I'd prefer iterm or Terminal.app (not firing up X11) but don't really care so much about that point. – spike Jul 7 '12 at 14:34
  • 2
    I'm not aware of anyway to do this, sorry. Why aren't you just appending output to a file? Emacs is pretty good at displaying those ;) – wdkrnls Jul 7 '12 at 15:57
  • That's not a bad idea actually, I could just tail -f the file in an open terminal. I'd probably lose color codes, but nice thought. (would upvote you but I have no rep on superuser) Going to leave the question open in case someone else comes along. – spike Jul 7 '12 at 17:26

I've found a solution that seems to be working nicely.

Using tmux, I can send a command to a running session like this:

(defun es-send-via-tmux (command)
  (message (concat "running: " command))
  (call-process "/usr/local/bin/tmux" nil nil nil "send-keys" "-t 1" command "C-m")


(es-send-via-tmux "echo hello") 

Been some time since I used OS X. IIRC, you can use the osascript utility to run AppleScript which could be used to send a command to Terminal.app. Something like:

tell application "Terminal"
  do script "ls"
end tell
  • I like this idea, but can't find a way to call a running terminal tab. everything I do opens a new one (I can run multiple commands in that new terminal, but only within the same tell ... end block) – spike Jul 11 '12 at 13:01
  • When I used this Terminal didn't have tabs; it used to run the command in the active window. Maybe stackoverflow.com/questions/1870270/… contains some hint. – Ankur Jul 12 '12 at 8:39

With iTerm.app I use the following script, which is very specific to my setup, but you can probably adapt it for your case. It lets you send code to any already existing window, and optionally change the directory to match the file you're sending the text from.


There's also this package: https://github.com/syohex/emacs-emamux That might be a more general solution. Haven't tried it myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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