If I paste the clipboard with <C-r>, it is pasted with a strange newline

When I develop, I paste the contents of the clipboard in <C-r>+ format in insert mode to paste the code of the web browser.

However, if you put the contents of the clipboard with “+p”, there is no problem, but if you use <C-r> like <C-r>+, unnecessary newlines are included as shown below.

Anyone know why?

Try <C-R><C-O>+ instead.

See :h i_CTRL-R_CTRL-O

1 Like

Thank you for answer.

As you said, it was solved with <C-r><C-o>+

I was bothered to type <C-o> every time, so I solved it by adding the :set paste setting.

I would recommand mapping:

:inoremap <c-r>+ <c-r><c-o>+

The advantage is that you do not have to make :set paste paste your code then :set nopaste

For lua config I have:

local function map(mode, lhs, rhs, opts)
	local options = { noremap = true, silent = true }
	if opts then
		if opts.desc then
			opts.desc = "init.lua: " .. opts.desc
		end
		options = vim.tbl_extend('force', options, opts)
        end
	vim.keymap.set(mode, lhs, rhs, options)
end

-- avoid clipboard hacking security issue
-- http://thejh.net/misc/website-terminal-copy-paste
-- inoremap <C-R>+ <C-r><C-o>+
map("i", "<C-r>+", "<C-r><C-o>+", { desc = 'fix terminal copy paste hack issue' })
map('i', '<C-r>*', '<C-r><C-o>*', { desc = 'fix terminal copy paste hack issue' })
map('i', '<C-r>"', '<C-r><C-o>"', { desc = 'Pastes default register literally allowing you to dot repeat' })
map("i", "<S-Insert>", "<C-r><C-o>*", { desc = 'fix terminal copy paste hack issue' })