Skip to content

add OSC 52 clipboard support #14995

Closed
Closed

Description

@peterwu

A recent PR feat(clipboard): add OSC 52 clipboard support #25872 allows Neovim v0.10 to support both copy and paste operations using OSC52.

It's trivial to implement the copy operation with Vimscript but it's quite difficult to implement the paste operation without some changes.

If I understand it correctly, Neovim basically repurposes the TermReponse event to capture the content sent from the terminal after the right paste escape chars are written to the terminal. neovim/neovim#25868

Is your feature request about something that is currently impossible or hard to do? Please describe the problem.
It's difficult to use the existing VIM features to support the OSC52 paste operation, esp. the part where the content from the terminal can't be easily captured and made available.

Describe the solution you'd like
Implement OSC52 copy and paste natively and provide g: options for configuration (e.g. on/off)

Describe alternatives you've considered
Use a 3rd party program, such as this one (https://github.com/theimpostor/osc) to help capture the pasted content via a call to system().

Activity

  1. zeertzjq commented on Jun 13, 2024

    @zeertzjq
    Member

    Note: Nvim's TermResponse event is completely different from Vim's TermResponse and TermResponseAll.

  2. peterwu commented on Jun 13, 2024

    @peterwu
    Author

    For completeness, this is the PR where Neovim repurposed TermReponse.

    neovim/neovim#25868

  3. chrisbra commented on Jun 15, 2024

    @chrisbra
    Member

    This migh be a big complicated, because typically terminal escapes can occur later, even withing other events that need to be handled. I have no idea how complex setting this up.

  4. peterwu commented on Jun 15, 2024

    @peterwu
    Author

    maybe add a new code to handle osc52 paste in TermResponseAll?

    :help TermResponseAll already warns

    	Note that this event may be triggered halfway
    	executing another event, especially if file I/O,
    	a shell command or anything else that takes time
    	is involved.
    
  5. added a commit that references this issue on Aug 27, 2025
    1f51bbc
  6. andreihh commented on Aug 31, 2025

    @andreihh

    It's great to see this fixed, I'm looking forward to not have to rely on https://github.com/ojroques/vim-oscyank :). I looked at the commit that fixed this, and I'm not sure how I can enable the OSC52 clipboard in my vimrc?

  7. peterwu commented on Sep 5, 2025

    @peterwu
    Author

    Not perfect but here is what I have in my vimrc. HTH.

    # paste from clipboard
    if has('clipboard')
        nnoremap <silent> <Leader>p "+p
        nnoremap <silent> <Leader>P "+P
        xnoremap <silent> <Leader>p "+p
        xnoremap <silent> <Leader>P "+P
    else
        nnoremap <silent> <Leader>p <ScriptCmd>PasteFromClipboard("p")<CR>
        nnoremap <silent> <Leader>P <ScriptCmd>PasteFromClipboard("P")<CR>
        xnoremap <silent> <Leader>p <ScriptCmd>PasteFromClipboard("p")<CR>
        xnoremap <silent> <Leader>P <ScriptCmd>PasteFromClipboard("P")<CR>
    endif
    
    g:pending_paste_command = ""
    
    def PasteFromClipboard(pending_paste_command: string)
        echoraw("\x1b]52;c;?\x07")
    
        g:pending_paste_command = pending_paste_command
    enddef
    
    augroup capture_osc52_paste | autocmd!
        autocmd TermResponseAll osc {
            var match = matchlist(v:termosc, '^\%x1b]52;c;\(.*\)\%x07$')
            if !empty(match)
                var b64text = match[1]
                var decoded_text = b64text->base64_decode()->blob2str()->join()
                setreg(v:register, decoded_text)
                execute 'normal! "' .. v:register .. g:pending_paste_command
                g:pending_paste_command = ""
            endif
        }
    augroup END
    
    
  8. added a commit that references this issue on Sep 7, 2025
    7409728
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions