Emacs: send line or region to shell
Contents
While exploring GRASS GIS and writing scripts to run its commands, I realized it would be way more convenient to have a shortcut for sending lines to the shell for evaluation. This, instead of copy-pasting each sentence from the script to the shell buffer (ugh).
Thanks to this answer on Stackoverflow, I was able to do just that with a minor tweak.
(defun sh-send-line-or-region (&optional step)
(interactive ())
(let ((proc (get-process "shell"))
pbuf min max command)
(unless proc
(let ((currbuff (current-buffer)))
(shell)
(switch-to-buffer currbuff)
(setq proc (get-process "shell"))
))
(setq pbuff (process-buffer proc))
(if (use-region-p)
(setq min (region-beginning)
max (region-end))
(setq min (point-at-bol)
max (point-at-eol)))
(setq command (concat (buffer-substring min max) "\n"))
(with-current-buffer pbuff
(goto-char (process-mark proc))
(insert command)
(move-marker (process-mark proc) (point))
(setq comint-scroll-to-bottom-on-output t)
)
(process-send-string proc command)
(display-buffer (process-buffer proc) t)
(when step
(goto-char max)
(next-line))
))
(defun sh-send-line-or-region-and-step ()
(interactive)
(sh-send-line-or-region t))
And because I’ve set <s-return>
to ess-eval-region-or-line-and-step
, I set the same shortcut for sending commands to the shell for evaluation.
(require 'sh-script)
(define-key sh-mode-map (kbd "<s-return>") 'sh-send-line-or-region-and-step)
Et voila!