4

I've been struggling to make a simple task working on my aquamacs.

I have an R session running on a remote server via ssh.

my Aquamacs is running locally on my machine.

What I want is to be able to send the current line or current selection to the active R session on my iterm2. This is easily done on sublime text 2 via the command "Cmd-Enter" with the plugin sendText.

How can we achieve that simple task ?

Best,

7
  • Is there a reason you're not just connecting to your R session from within emacs? It looks like SendText uses apple script to automate the behavior of other running applications, this could be done in Emacs but it's really not a good solution. The best solution would be to connect to your R session from within emacs, via a shell buffer or possibly a remote slime connection. Then use built in Emacs functions to send the process text. Apr 4, 2014 at 14:35
  • Well for several reasons: 1) because I like working on iterm2 2) I tried to follow instructions for your suggestion following the post from "github.com/laco/emacs-starter-kit/blob/master/…". Here the "M-x ess-remote" step failed with "No process is associated with this buffer" and I personnaly found this process complicated. Why not send "eval" to the active terminal as is the case with ST2 sendText?
    – klebsiella
    Apr 4, 2014 at 18:26
  • The SendText source is very simple, the really important part is right here for you: github.com/wch/SendText/blob/master/SendText.py#L40-L43 you can easily port this to a call to start-process in Emacs, I may try it myself if I get time. In fact, I got started on it, but I'm not on a osx machine so I can't test it: gist.github.com/9981411 Apr 4, 2014 at 19:17
  • Thanks for your fast replies. This starts to look promissing. I inserted the "gist" code into my .emacs and tried to send the current line to my active iterm by M-x sendtext:send-iterm2. This gave the following error "The mark is not set now, so there is no region". As I do not know lisp and python, it was hard to me to debug. Did it work in your hand ?
    – klebsiella
    Apr 5, 2014 at 10:13
  • For info, I found a solution by connecting to my R session from within emacs (first connecting to the remote server and there launching R) and using the isend-mode. Mainly, I followed the instructions on (github.com/ffevotte/isend-mode.el) and bound the send key with: (eval-after-load "isend-mode" '(define-key isend-mode-map (kbd "C-c RET") 'isend-send)). Hope this will help others... Thanks JordonBiondo
    – klebsiella
    Apr 6, 2014 at 21:16

1 Answer 1

2

I ran into this same problem, and found the following solution. I modified (very slightly) this gist, and added it as a package to Aquamacs.

Here are the basic steps:

Copy the gist above into a new file, and then change the function iterm-send-string to the following:

(defun iterm-send-string (str)   
  "Send STR to a running iTerm instance."
  (let* ((str (iterm-maybe-remove-empty-lines str))
         (str (iterm-handle-newline str))
         (str (iterm-escape-string str)))
    (shell-command (concat "osascript "
                          "-e 'tell app \"iTerm2\"' "
                          "-e 'tell current window' "
                          "-e 'tell current session' "
                          "-e 'write text \"" str "\"' "
                          "-e 'end tell' "
                          "-e 'end tell' "
                          "-e 'end tell' "))))

To make this a package you can add the following to the top of the file

;;; iterm.el --- sends selections to iTerm.app -*- lexical-binding: t; -*-

;; modified from: https://gist.github.com/johnmastro/88cc318f4ce33b626c9d

;; Author: David Little <[email protected]>
;; Keywords: lisp
;; Version: 0.0.1

;;; Commentary:

;; Works on Mac OS X: sends a selction to iTerm
;; To match SublimeText's key binding:
;; (global-set-key (kbd "<C-return>") 'iterm-send-text)

;;; Code:

Then you just need to call M-x package-install-file. Once you bind the iterm-send-text function to a shortcut (e.g. C-return in the comments above) you should be in business!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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