Created
July 26, 2018 17:50
-
-
Save dboitnot/370498e3053f1f4a91d0e0c620a60d1e to your computer and use it in GitHub Desktop.
Send the current line or selected text to iTerm2 from emacs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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