4 ms·
I run my computing environment on a remote box, to which I `mosh` to and run `tmux` on from a local MacOS box. Anything commandline (including Emacs) runs remot
by ylee 5y ago
I run my computing environment on a remote box, to which I `mosh` to and run `tmux` on from a local MacOS box. Anything commandline (including Emacs) runs remotely, and anything graphical (including web browser) runs locally.
Sometimes I need to copy text from the remote Emacs session to the local MacOS clipboard in a cleaner way than using MacOS select-and-copy. I ran into the quoting issue because
'
is needed before and after the text being sent to quote it for the shell, but then that character can't be sent in said text, breaking the entire pipe. I was stumped for hours before finding a solution by trial and error. I think the comment says it all:
; single quotes necessary to escape
; argument for shell
" -c '"
(replace-regexp-in-string
"'"
;;; This can't be the simplest way to do this
"'\\''\\'\\'''\\''"
(I see from isodude's comment that the above indeed isn't the simplest way to do this, and should try his solution sometime.)
The complete function follows for the curious (and for those who can suggest improvements):
;;; BOS
;;; my-clipboard-kill-ring-save - Send any text to remote clipboard
(defun my-clipboard-kill-ring-save (beg end)
"Copy region to kill ring with `kill-ring-save', and send to
remote clipboard."
(interactive "r")
(kill-ring-save beg end)
(condition-case nil
(let ((my-clip (current-kill 0 t)))
(unless (string-equal "" my-clip)
(shell-command-to-string ; send it to remote clipboard
; Calls my shell script that takes argument and, on local machine, either
; a) opens it as URL, or b) with the -c option, sends it to local clipboard
(concat browse-url-generic-program
; single quotes necessary to escape
; argument for shell
" -c '"
(replace-regexp-in-string
"'"
;;; This can't be the simplest way to do this
"'\\''\\'\\'''\\''"
my-clip t t)
"'")) ; closing of argument
(message "Clip \"%s\" sent to remote clipboard." my-clip)))
(error (message "No clip sent to remote clipboard"))))
(global-set-key (kbd "M-W") 'my-clipboard-kill-ring-save)
;;; EOS