I would like Emacs to send a few chars (command, ideally) to a specific buffer (ssh shell) so that the session does not terminate. How can I achieve this?


1 Answer 1


As npostavs suggests, it is usually in your interest to use shell- or program-specific settings/functionality for this sort of task.

Nevertheless, the following is a simple PoC using timers[1] for sending input to comint-based subshells like shell, as well as the term emulator[2].

(defun my-term-send-string (&optional buffer string)
  "Send STRING to a shell process associated with BUFFER.
By default, BUFFER is \"*terminal*\" and STRING is empty."
  (let ((process (get-buffer-process (or buffer "*terminal*"))))
    (when (process-live-p process)
      (with-current-buffer (process-buffer process)
        (let ((input (or string "")))
          (cond ((derived-mode-p 'comint-mode)
                 (insert input)
                ((derived-mode-p 'term-mode)
                 (term-send-string process input)

;; Send empty input to `*terminal*' every 30 seconds
(run-at-time t 30 #'my-term-send-string)

;; Send ":" null command to `*shell*' every minute
(run-at-time t 60 #'my-term-send-string "*shell*" ":")

[1]: (elisp) Timers.

[2]: (emacs) Shell.

Your Answer

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

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