Enabling pbcopy over SSH with iTerm2
When working in the command line, you may want to quickly copy command output or file contents to your clipboard, perhaps to share with someone or to paste somewhere directly. On macOS, the simplest approach is using the pbcopy
command, which copies any piped input to your clipboard, similar to xclip
on Linux.
However, pbcopy
typically doesn’t work in remote SSH sessions. While there are various workarounds with different levels of complexity and thoroughness, I’ve found a straightforward solution for iTerm2 users that’s simpler than the alternatives discussed in this Stack Overflow thread.
Here’s how to enable pbcopy
over SSH:
-
First, define this function on the remote server by adding it to your
~/.bashrc
:pbcopy() { input=$(cat) encoded=$(printf "%s" "$input" | base64 -w 0) printf "\033]52;c;%s\007" "$encoded" }
-
In iTerm2 on your local machine, navigate to Settings > General > Selection and enable “Applications in terminal may access clipboard”. You can leave “Allow sending of clipboard contents?” set to “Always Ask” as it doesn’t affect this functionality.
-
For tmux users, add this line to your
~/.tmux.conf
to ensure clipboard interaction works within tmux sessions:set -s set-clipboard on
- This setting allows
tmux
to interact with the system clipboard, ensuring that thepbcopy
function works seamlessly withintmux
sessions.
Note: This method is specific to macOS and iTerm2.