Emacs provides the function process-send-region
to do this. If your buffer always has the same name, you can hard-code it in:
(defun my-send (beg end)
(interactive "r")
(process-send-region "guesuser@productionServerShellSession" beg end))
The first line defines a new function, my-send
, with two arguments, beg
and end
. The second line declares that this function is interactive, and will use the current region (that's what the "r" flag does). So when we call it, beg
and end
are filled with the start and end points of the active region.
The last line sends that region to your shell.
If you want to send the region to different buffers, you'll need to add some sort of logic to accommodate this. As a simple example, you could store your target buffer in a variable:
(defun my-set-target ()
(interactive)
(setq my-target (buffer-name)))
Calling this command will store the current buffer name in the variable my-target. Modifying the original command:
(defun my-send (beg end)
(interactive "r")
(process-send-region my-target beg end))
There are lots of ways you could smooth these out, to handle various errors (what if my-target hasn't been defined yet, or what if the region doesn't include a terminating newline, etc). But that should get you started.
Actually, I've been wanting this for a while, so I made a more complete version:
(defun tws-region-to-process (arg beg end)
"Send the current region to a process buffer.
The first time it's called, will prompt for the buffer to
send to. Subsequent calls send to the same buffer, unless a
prefix argument is used (C-u), or the buffer no longer has an
active process."
(interactive "P\nr")
(if (or arg ;; user asks for selection
(not (boundp 'tws-process-target)) ;; target not set
;; or target is not set to an active process:
(not (process-live-p (get-buffer-process
tws-process-target))))
(setq tws-process-target
(completing-read
"Process: "
(seq-map (lambda (el) (buffer-name (process-buffer el)))
(process-list)))))
(process-send-region tws-process-target beg end))