Skip to content
Advertisement

Using elisp, how to start, send-keys-to, and stop a command in emacs terminal emulator?

Using elisp (not interactive key-chords), how can I run a command in emacs terminal emulator; and how can I send key-presses to that buffer?

Starting term seems to require (term "/bin/bash"), which has no scope for running a command. I assume that might be because term is intended as an interactive tool…

Also I want to send specific keys to the running app. Can this be done. I thought (insert ‘x) might work, but it doesn’t have a buffer parameter, nor does it allow for M- C- S- s-

Advertisement

Answer

You can send input directly to the terminal with term-send-raw-string. Example:

(progn
  (set-buffer "*terminal*")
  (term-send-raw-string "ls -ln"))

This will simulate the effect of typing ls -lRET into the terminal buffer.

While term isn’t very flexible about argument parsing, it’s usually sufficient to start up a shell and feed it commands with term-send-raw-string to load up the target program. Here’s a little piece of elisp that scripts some commands to an interactive program:

(progn 
  (let ((term-buffer (term "/bin/bash")))
    (set-buffer term-buffer)

    ;; start up vi
    (term-send-raw-string "vi hello.txtn")

    ;; some line noise :P
    (term-send-raw-string "ihello world33:wqn")

    ;; quit our shell
    (term-send-raw-string "exit")))
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement