How to write a function (it would go into .emacs) that sends some command to shell buffer (and initializes shell if needed)?

Something like:

(defun ()
  (sh-send-text "ssh my.server.com"))

(I currently do this via tramp and sshx but it is slower than if I manually open shell and just type in the ssh command. So I want to speed this up with a helper function).

1 Answer 1


comint-send-string is the function you're looking for. (shell is built on top of the comint library.)

It takes a PROCESS and a STRING. You can get the process from the shell buffer, and conveniently the shell function returns the buffer, so you can streamline it all into something like:

(defun my-server ()
  "SSH to my.server.com in `shell' buffer."
   (get-buffer-process (shell))
   "ssh my.server.com\n"))

Where the (shell) call will take care of creating the shell buffer and/or process if necessary. (n.b. if there's an existing one, shell will re-use that.)

  • You might also find it useful to include this so that the comint buffer always scrolls to the bottom: (add-hook 'comint-exec-hook (setq comint-scroll-to-bottom-on-output t)) Commented Jan 28, 2019 at 19:50

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.