I often find myself copying commands line by line from an emacs buffer to my terminal.
How do i bind a shortcut which sends the current line/selection in emacs to my gnome-terminal?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityI often find myself copying commands line by line from an emacs buffer to my terminal.
How do i bind a shortcut which sends the current line/selection in emacs to my gnome-terminal?
First of all, running commands in existing terminals isn't something all terminals support (at least not without some hackery), this seems something you might use screen or tmux for.
Also this isn't really related to emacs - if you find a way to do this - the following snippet could be easily modified.
Edit, it seems roxterm supports this, edited answer.
The following answer opens a new terminal.
Here is a small script which takes the selection (or current line) and runs it in gnome-terminal.
(defun shell-command-on-region-or-line ()
"Run selected text in a terminal or use the current line."
(interactive)
(shell-command
(concat
;; pick one!
"gnome-terminal -e "
; "roxterm --tab -e "
; "terminator -x "
(if (use-region-p)
;; current selection
(buffer-substring (region-beginning) (region-end))
;; current line
(thing-at-point 'line t)))))
Example key binding you wont hit by accident :)
;; Execute text in a terminal, no questions! (ctrl-alt-shift-enter to avoid accidents!)
(global-set-key (kbd "<c-m-s-return>") 'term-shell-command-on-region-or-line)
Note, I ended up finding this quite useful and made it into a package that has some option prefixes for opening in different ways, including path detection:
You have a few options within Emacs. M-! opens a minibuffer, which allows you to type or paste shell commands into it, and puts the results in another buffer. C-u M-! will do the same thing but insert the result directly into the buffer. There is also M-| which pipes a region to a shell command but I'm never used it.
eepitch
functionality ineev
package. – artscan Oct 4 '16 at 11:36