3

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 ()
  (interactive)
  (shell)
  (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).

Drew
  • 77,472
  • 10
  • 114
  • 243
Paul
  • 191
  • 7

1 Answers1

6

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."
  (interactive)
  (comint-send-string
   (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.)

phils
  • 50,977
  • 3
  • 79
  • 122
  • 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)) – Lorem Ipsum Jan 28 '19 at 19:50