6

This works (puts the date and time in the clipboard) in just iTerm:

printf "\e]1337;Copy=:$(date | base64)\a"; echo $(pbpaste)

This works in tmux running locally (using the DCS passthrough):

printf "\ePtmux;\e\e]1337;Copy=:$(date | base64)\a\e\\"; echo $(pbpaste)

This works in tmux running remotely:

printf "\ePtmux;\e\e]1337;Copy=:$(date | base64)\a\e\\"; echo $(ssh -p 2222 -qt localhost pbpaste)

My only problem is running tmux remotely under a local tmux:

printf "\ePtmux;\e\ePtmux;\e\e]1337;Copy=:$(date | base64)\a\e\\\e\\"; echo $(ssh -p 2222 -qt localhost pbpaste)

I think the problem is the inner \e\\ is being interpreted as the outer \e\\.

Is there some way to escape the inner \e\\ so it makes it the outer tmux properly?

Chas. Owens
  • 163
  • 6

1 Answers1

10

You need to double every \e for each tmux, including the \e in the terminating \e\\, so:

printf "\ePtmux;\e\ePtmux;\e\e]1337;Copy=:$(date | base64)\a\e\e\\\e\\"

Alternatively if you configure tmux to use OSC 52 and then turn it on in iTerm2 ("Applications in terminal may access clipboard" from a quick search) it will pass through each tmux (creating a paste buffer in each) to the host clipboard. For tmux you will need something like:

set -as terminal-overrides ',tmux*:Ms=\\E]52;%p1%s;%p2%s\\007'
set -as terminal-overrides ',screen*:Ms=\\E]52;%p1%s;%p2%s\\007'
set -s set-clipboard on

Then you can do this in the innermost tmux:

printf "\033]52;$(date)\007"

Of course this will mean anything you copy in tmux will also go into the host clipboard which you may not want.

Nicholas Marriott
  • 3,630
  • 9
  • 12
  • Setting up OSC 52 works great, thanks. But I wanted to send other escape sequences as well (for growl notifications and other stuff). The printf is hanging, but that seems to be related to printf as `perl -e 'print "\ePtmux;\e\ePtmux;\e\e\e\e]1337;Copy=:@{[`date | base64`]}\n\a\e\e\\\e\\"'` is working. – Chas. Owens Dec 11 '19 at 19:20
  • Try piping both printf and perl to `cat -v` or `od` or something (eg `printf "..."|cat -v`) and compare what they are sending to tmux to see what printf is doing differently. – Nicholas Marriott Dec 11 '19 at 19:45
  • Thank you so much! Note that I had to use single backslashesi the `terminal-overrides` (so that `tmux show -s terminal-overrides` displays double backslashes instead of four...) – Konrad Jul 20 '21 at 07:09
  • Also your OSC52 test command did not work for. That's probably on my terminal (mintty). I had to use `printf "\033]52;c;$(date | base64)\a"`. – Konrad Jul 20 '21 at 07:13