7

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")
4
  • Have you tried M-x term? This one seems to work quite like a real terminal.
    – cYrus
    Jul 6, 2012 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, 2012 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, 2015 at 20:16
  • @joefromct check out my self-answer below.
    – spike
    Jul 21, 2015 at 20:38

4 Answers 4

3

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)))
3
  • 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, 2012 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, 2012 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, 2012 at 17:26
4

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

E.x.:

(es-send-via-tmux "echo hello") 
5
  • If I put the definition of your es-send-via-tmux in my ~/.emacs, and then try to call it via M-x es-send-via-tmux, the function es-send-via-tmux is not found (not even after restarting emacs). How can this be turned into a standard emacs function? Jan 12, 2022 at 22:52
  • @MariusHofert that function can be called from elisp but not interactively. To make a function interactive you have to add interactive like (interactive "MCommand: ") in the definition.
    – spike
    Jan 14, 2022 at 15:32
  • Hi spike, thanks for the update. Could you add a full solution to this, please? If I do what you suggest, I get either Wrong number of arguments or [No match], depending how I call it. In any case, a solution which sends the command (or the current line if non given) would be great. Jan 14, 2022 at 16:22
  • @MariusHofert please ask a new question about this, it's hard to put code in a comment
    – spike
    Jan 14, 2022 at 18:14
  • Thanks. See superuser.com/questions/1698921/… (asking for Terminal instead of tmux). Alternatively, you could edit your post here to add code. Jan 14, 2022 at 18:32
2

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
2
  • 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, 2012 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, 2012 at 8:39
0

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.

https://github.com/haberdashPI/iterm.el/blob/master/iterm.el

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .