fix(terminal): OSC 52 multiline copy replaces newlines with NUL #41097

Problem: An OSC 52 sequence from a :terminal job passes the decoded
payload to the clipboard provider as a single list item. Command-line
providers (pbcopy, xclip, ...) receive it with channel semantics, where
a newline inside an item is sent as NUL (:h chansend()), so multiline
copies arrive with NUL bytes instead of newlines.

Solution: Split the payload on newlines into a proper list of lines.
A trailing newline yields a final empty item, which chansend() turns
back into a newline, so payloads round-trip exactly.

(cherry picked from commit e58f29ca3e)
This commit is contained in:
Chris Hebert
2026-08-05 05:37:02 -05:00
committed by github-actions[bot]
parent e6c93b5996
commit d23507a0f6
2 changed files with 36 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ describe(':terminal', function()
local function clipboard(reg, type)
if type == 'copy' then
return function(lines)
vim.g.clipboard_lines = lines
local data = table.concat(lines, '\n')
vim.g.clipboard_data = data
end
@@ -46,20 +47,40 @@ describe(':terminal', function()
]])
end)
local function osc52(arg)
return string.format('\027]52;;%s\027\\', arg)
end
it('can write to the system clipboard', function()
eq('Test', eval('g:clipboard.name'))
local text = 'Hello, world! This is some\nexample text\nthat spans multiple\nlines'
local encoded = exec_lua('return vim.base64.encode(...)', text)
local function osc52(arg)
return string.format('\027]52;;%s\027\\', arg)
end
fn.jobstart({ testprg('shell-test'), '-t', osc52(encoded) }, { term = true })
retry(nil, 1000, function()
eq(text, exec_lua([[ return vim.g.clipboard_data ]]))
end)
-- Multiline payloads must arrive split into separate list items: a newline
-- inside a single item would be sent to a command-line provider as NUL.
eq({
'Hello, world! This is some',
'example text',
'that spans multiple',
'lines',
}, exec_lua([[ return vim.g.clipboard_lines ]]))
end)
it('multiline copy does not send newlines as NUL #41097', function()
local text = '\nfoo\n\nbar\n'
local encoded = exec_lua('return vim.base64.encode(...)', text)
fn.jobstart({ testprg('shell-test'), '-t', osc52(encoded) }, { term = true })
retry(nil, 1000, function()
eq({ '', 'foo', '', 'bar', '' }, exec_lua([[ return vim.g.clipboard_lines ]]))
end)
end)
end)