4

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?

  • 3
    isend-mode seems to do the trick... – Juliusz Oct 4 '16 at 6:22
  • thx for the hint. I haven't found something and wrote a function by myself. You should post this as an answer. – bertfred Oct 4 '16 at 7:52
  • well, the question might still be relevant as emacs terminal emulators are not as good as gnome-terminal (think vim or less). Ideally those commands would be sent to a gnome-terminal window – Juliusz Oct 4 '16 at 7:58
  • Indeed. I'd sure be interested in that solution. – bertfred Oct 4 '16 at 8:04
  • There is also more sophisticated eepitch functionality in eev package. – artscan Oct 4 '16 at 11:36
1

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:

https://github.com/ideasman42/emacs-run-stuff

  • tried it with both the above version and termite, terminator etc and dosent seem to open any term window – zeltak Jul 24 '17 at 9:26
  • @zeltak terminator works here, added commented text to use. – ideasman42 Jul 24 '17 at 11:11
  • thx @ideasman42. do now it does open terminator but the highlighted command dosent seem to run in the new terminator window. what i get is a new buffer in emas with the actual command, ie if i stand over 'ls' and launch the function it opens a new empty terminator window and a buffer in emacs wit the contents of ls – zeltak Jul 25 '17 at 3:07
  • I don't get the new buffer, but it does open a new terminal, edited answer. – ideasman42 Jul 25 '17 at 6:05
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.