Copying text from a remote shell to your local clipboard can be tricky. Luckily, many terminals support OSC52, an escape sequence that lets you send data straight to the clipboard. Here’s a handy Bash function you can drop into your ~/.bashrc:
osc52() {
local data
data=$(openssl base64 | tr -d '\n')
printf "\e]52;c;%s\a" "$data"
}- Base64 encoding:
openssl base64converts the input to the required format. - Escape sequence:
\e]52;c;<data>\atells the terminal to put the decoded text into the clipboard. - Input via stdin: Just pipe anything you want to copy.
echo "Hello" | osc52 # copy a string
cat file.txt | osc52 # copy a file
ls -la | osc52 # copy command output- Works in terminals like iTerm2, xterm, and gnome-terminal.
- In
tmux, you may need to enableset -g set-clipboard on. - Some systems restrict OSC52 for security reasons.