Skip to content

Instantly share code, notes, and snippets.

@dboitnot
Created July 26, 2018 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dboitnot/370498e3053f1f4a91d0e0c620a60d1e to your computer and use it in GitHub Desktop.
Save dboitnot/370498e3053f1f4a91d0e0c620a60d1e to your computer and use it in GitHub Desktop.
Send the current line or selected text to iTerm2 from emacs
(defun it2-tell-current (&rest messages)
"Send a message (or list of messages) to the current session of the current window in iTerm2"
(do-applescript
(concat
" tell application \"iTerm2\"\n"
" tell the current session of current window\n"
(string-join messages "\n")
"\n end tell\n"
" end tell\n")))
(defun it2-write-text (text)
"Write a string into the current session of the current window in iTerm2"
(it2-tell-current
(concat "write text \""
(replace-regexp-in-string "\\([\"\\]\\)" "\\\\\\1" text)
"\"")))
(defun it2-write-region-or-line (s e)
"send the content of the current region if there is one or line if not"
(interactive "r")
(it2-write-text
(if (and mark-active (/= (point) (mark)))
(buffer-substring s e) ;; send the selected region
(buffer-substring (start-of-line-at-pos (point))
(end-of-line-at-pos (point))))))
(global-set-key (kbd "s-<return>") 'it2-write-region-or-line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment