OSC52 is a terminal control sequence that allows remote side to set the system clipboard of the local side. This is the best way to get clipboard support when working with remote ssh sessions.

However, here're a couple of limitations to consider:

  1. Your terminal app needs to support OSC52. MacOS terminal.app doesn't support it. iterm2 does. xterm actually supports it, just require some additional setting.
  2. Mosh does not support OSC52. Native ssh supports it. Eternal Terminal (et) does support it.
  3. tmux supports passing copied text via OSC52, see tmux man, set-clipboard.
  4. In Vim, ojroques/vim-oscyank is a plugin to support yanking text via OSC52

With above consideration, so far the best combination that works for me has been using iterm2 + et.

Today it comes to my attension this little utility osc52pty, which adds OSC52 support for any terminal application (e.g., a shell). It basically removes the terminal app limitation from above consideration. I.e., with

1
2
3
# Do this in terminal.app:
osc52pty zsh
ssh foo@bar

For any terminal app, it detects osc52 bytes and pipes content to pbcopy. However this seems to only work with native ssh client. Neither et nor mosh work.