Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs Shell mode: how to send region to shell?

Tags:

emacs

Is there some module or command that'll let me send the current region to shell?

I want to have something like Python-mode's python-send-region which sends the selected region to the currently running Python shell.

like image 960
Yaroslav Bulatov Avatar asked Jun 08 '11 23:06

Yaroslav Bulatov


2 Answers

Ok, wrote an easy bit. Will probably spend some time to write a complete minor mode.

For time being the following function will send current line (or region if the mark is active). Does quite a good job for me:

(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))       ) ;;pop-to-buffer does not work with save-current-buffer -- bug?     (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)) (defun sh-switch-to-process-buffer ()   (interactive)   (pop-to-buffer (process-buffer (get-process "shell")) t))  (define-key sh-mode-map [(control ?j)] 'sh-send-line-or-region-and-step) (define-key sh-mode-map [(control ?c) (control ?z)] 'sh-switch-to-process-buffer) 

Enjoy.

like image 152
VitoshKa Avatar answered Oct 20 '22 12:10

VitoshKa


(defun shell-region (start end)   "execute region in an inferior shell"   (interactive "r")   (shell-command  (buffer-substring-no-properties start end))) 
like image 24
Jürgen Hötzel Avatar answered Oct 20 '22 12:10

Jürgen Hötzel