I have two windows open - one in editor mode with some SQL, bash, etc and the other in ansi-term. I want to mark my text and then execute it in the ansi-term window. Yes, I could make a shortcut involving copy paste but I was curious if there was anything canonical. Copy-paste also forces me to switch out of terminal mode and into editor mode in ansi-term
3 Answers
Emacs supports sending the region/buffer to the interpreter. This is supported in various modes, including python, haskell, octave, and even in shell scripts. Unfortunately, they keybindings are different in every mode.
For shell scripts (sh-mode
), you can use C-c C-n
to send the current line, or the highlighted region to the inferior shell. Unfortunately, it looks like it is hardcoded to use shell
, not ansi-term
.
For SQL (sql-mode
), similar features are available under C-c C-r
(send region) and C-c C-b
(send entire buffer). See the mode help of sql-mode for mode keybindings. You may need to install an interpreter that sql-mode can use (or configure sql-mode to use the right interpreter).
You can try something like this
(defun send-region-to-ansi ()
"If region active, send it to ansi-term buffer."
(interactive)
(if (region-active-p)
(send-region "*ansi-term*" (region-beginning) (region-end))))
Maybe you don't even want to mark a region. I use a function that marks the current paragraph automatically.
(defun send-paragraph-to-ansi ()
(interactive)
(mark-paragraph)
(send-region "*ansi-term*" (region-beginning) (region-end))
(deactivate-mark))
The most basic command, independent of a major mode, is shell-command-on-region
or M-|
.
The buffer where the output from above command is send to is *Shell Command Output*
.