Skip to content

Instantly share code, notes, and snippets.

@robin-a-meade
Last active June 12, 2026 22:53
Show Gist options
  • Select an option

  • Save robin-a-meade/14a279d28abdfb0526307ccb2c9b2381 to your computer and use it in GitHub Desktop.

Select an option

Save robin-a-meade/14a279d28abdfb0526307ccb2c9b2381 to your computer and use it in GitHub Desktop.
OSC 52 escape sequence to copy to clipboard in terminal

OSC 52 escape sequence to copy to clipboard in terminal

OSC 52 (Operating System Command 52) is an ANSI escape sequence that allows a terminal application to read or write to your local system's clipboard.

Note

Not all terminal emulators support OSC 52, and those that do usually restrict it to writing to the clipboard, disallowing reading from it.

Restricting it to writing is to prevent the scenario of a malicious application reading sensitive information from the user's clipboard.

More about terminal support below.

Quick test: Paste this into your terminal

printf "\e]52;c;%s\a" "$(printf '%s' 'Hello, World!' | base64 -w 0)" # GNU
printf "\e]52;c;%s\a" "$(printf '%s' 'Hello, World!' | base64 -b 0)" # macOS/BSD

The text Hello, World! should now be on your clipboard, assuming your terminal supports the OSC 52 escape sequence.

Explanation:

  • \e]52 begins an escape sequence for the OSC 52 command
    • The \e] part means we are starting an Operating System Command (OSC)
    • \e is ESC . Equivalents: \033 (octal) and \x1b (hex)
    • 52 is the specific command number used for clipboard operations
  • c specifies standard clipboard for the clipboard specifier argument (details).
  • \a is BEL. Equivalents: \007 (octal) and \x07 (hex)
  • The -w 0 option to base64 disables base64's default behavior of line wrapping. You could also use base64 | tr -d '\n'. On macOS/BSD, use -b 0 instead of -w 0.

Terminal support for OSC 52

Terminals that support OSC 52 for writing to the clipboard include:

  • Ghostty
  • iTerm2
  • WezTerm
  • Alacritty
  • Kitty
  • Windows Terminal

Note

Gnome Terminal, Ptyxis, and other VTE-based terminals do not support OSC52 yet. Watch this issue:

Work-around: tmux can support OSC52, even in Gnome Terminal.

bash script to copy specified files, or stdin, to clipboard

I put this shell script at ~/.local/bin/osc52 and made it executable with chmod +x.

#!/usr/bin/env bash

# Open the OSC 52 escape sequence and specify the system clipboard (c)
printf '\e]52;c;'

# Read file args, or from stdin if no args, and encode with base64
# Options used:
#   -w 0 disables line wrapping (On BSD/macOS use -b instead of -w)
base64 -w 0 "${@}"

# Close the escape sequence
# Two escape sequences work:
#   \e\\   Standard string terminator
#   \a     BEL also works. This is non-standard but widely supported
# https://unix.stackexchange.com/q/729360
printf '\a'

Go binary executable for OSC 52

If you prefer a binary executable over a shell script, you can use this go program:

https://github.com/theimpostor/osc

Install:

go install -v github.com/theimpostor/osc@latest

Usage:

echo Hello | osc copy

vim

For vim, I use this plugin:

Then I visually select a block of text and press <leader> followed by c to copy it to the clipboard. (I use Vim’s default leader of backslash, so I press \ followed by c.)

For old vim (vim 7.4)

" OSC52 clipboard support for vim 7.4
" ========================================
"
" This function and visual keybinding will send the yanked text to the system clipboard, if
" your terminal supports the OSC 52 escape sequence.
"
" I first tried the ojroques/vim-oscyank plugin, but it wasn't
" compatible with vim 7.4. This function and keybinding is a workaround.
"
" Function to send yanked text to OSC52 sequence
function! OSC52()
    let yanked = @"
    "call system("base64 -w 0 | xargs -0 printf '\\e]52;c;%s\\a' >/dev/tty", yanked)
    call system("{ printf '\\e]52;c;'; base64 -w 0; printf '\\a'; } >/dev/tty", yanked)
    if v:shell_error == 0
        echo 'Copied to clipboard via OSC52'
    else
        echoerr 'OSC52 clipboard copy failed'
    endif
endfunction

" Map visual yank to the OSC52 function as a side effect
" Maybe I should add more yank mappings, but I decided to keep it simple
" Remember, this is merely a workaround for vim-oscyank not being compatible
" with vim 7.4. It won't be needed for long.
vnoremap <silent> y y:call OSC52()<CR>

Update: alternate way for vim 7.4

" Alternate implementation:
" Function to copy the visually selected range to the clipboard
" Limitation: linewise only
function! WriteVisualSelectionToClipboard() range
  "silent '<,'>w !base64 -w 0 | xargs -0 printf '\e]52;c;\%s\a' >/dev/tty
  silent '<,'>w !{ printf '\e]52;c;'; base64 -w 0; printf '\a'; } >/dev/tty
endfunction

" Map it to <leader>c in visual mode
" (Same default key binding as vim-oscyank)
" vnoremap is non-recursive and safe
vnoremap <leader>c :call WriteVisualSelectionToClipboard()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment