feat(clipboard): support g:clipboard="osc52" #33021

Problem:
Forcing Neovim to use OSC52 for the system clipboard should be simple
and concise, since OSC52 is widely supported (Alacritty, Ghostty,
iTerm2, WezTerm, Kitty, xterm, tmux, etc.) and is the most portable
approach for syncing clipboards across SSH.

Solution:
Support g:clipboard="osc52".
This commit is contained in:
Andrei Heidelbacher
2025-03-24 13:17:56 +01:00
committed by GitHub
parent af4231d407
commit 563051a53e
2 changed files with 19 additions and 5 deletions

View File

@ -59,6 +59,14 @@ function! s:split_cmd(cmd) abort
return (type(a:cmd) == v:t_string) ? split(a:cmd, " ") : a:cmd return (type(a:cmd) == v:t_string) ? split(a:cmd, " ") : a:cmd
endfunction endfunction
function! s:set_osc52() abort
let s:copy['+'] = v:lua.require'vim.ui.clipboard.osc52'.copy('+')
let s:copy['*'] = v:lua.require'vim.ui.clipboard.osc52'.copy('*')
let s:paste['+'] = v:lua.require'vim.ui.clipboard.osc52'.paste('+')
let s:paste['*'] = v:lua.require'vim.ui.clipboard.osc52'.paste('*')
return 'OSC 52'
endfunction
let s:cache_enabled = 1 let s:cache_enabled = 1
let s:err = '' let s:err = ''
@ -69,6 +77,13 @@ endfunction
function! provider#clipboard#Executable() abort function! provider#clipboard#Executable() abort
" Setting g:clipboard to v:false explicitly opts-in to using the "builtin" clipboard providers below " Setting g:clipboard to v:false explicitly opts-in to using the "builtin" clipboard providers below
if exists('g:clipboard') && g:clipboard isnot# v:false if exists('g:clipboard') && g:clipboard isnot# v:false
if v:t_string ==# type(g:clipboard)
if 'osc52' == g:clipboard
" User opted-in to OSC 52 by manually setting g:clipboard.
return s:set_osc52()
endif
endif
if type({}) isnot# type(g:clipboard) if type({}) isnot# type(g:clipboard)
\ || type({}) isnot# type(get(g:clipboard, 'copy', v:null)) \ || type({}) isnot# type(get(g:clipboard, 'copy', v:null))
\ || type({}) isnot# type(get(g:clipboard, 'paste', v:null)) \ || type({}) isnot# type(get(g:clipboard, 'paste', v:null))
@ -172,11 +187,7 @@ function! provider#clipboard#Executable() abort
elseif get(get(g:, 'termfeatures', {}), 'osc52') && &clipboard ==# '' elseif get(get(g:, 'termfeatures', {}), 'osc52') && &clipboard ==# ''
" Don't use OSC 52 when 'clipboard' is set. It can be slow and cause a lot " Don't use OSC 52 when 'clipboard' is set. It can be slow and cause a lot
" of user prompts. Users can opt-in to it by setting g:clipboard manually. " of user prompts. Users can opt-in to it by setting g:clipboard manually.
let s:copy['+'] = v:lua.require'vim.ui.clipboard.osc52'.copy('+') return s:set_osc52()
let s:copy['*'] = v:lua.require'vim.ui.clipboard.osc52'.copy('*')
let s:paste['+'] = v:lua.require'vim.ui.clipboard.osc52'.paste('+')
let s:paste['*'] = v:lua.require'vim.ui.clipboard.osc52'.paste('*')
return 'OSC 52'
endif endif
let s:err = 'clipboard: No clipboard tool. :help clipboard' let s:err = 'clipboard: No clipboard tool. :help clipboard'

View File

@ -277,6 +277,9 @@ To disable the automatic detection, set the "osc52" key of |g:termfeatures| to
To force Nvim to always use the OSC 52 provider you can use the following To force Nvim to always use the OSC 52 provider you can use the following
|g:clipboard| definition: >lua |g:clipboard| definition: >lua
vim.g.clipboard = 'osc52'
Which is equivalent to: >lua
vim.g.clipboard = { vim.g.clipboard = {
name = 'OSC 52', name = 'OSC 52',
copy = { copy = {