When I open Vim from a terminal, copy some text to the system clipboard, and exit Vim, the system clipboard gets cleared.
How to keep the copied text in the clipboard?
When I open Vim from a terminal, copy some text to the system clipboard, and exit Vim, the system clipboard gets cleared.
How to keep the copied text in the clipboard?
Synthesizing answers from superuser, just add the following to your .vimrc
autocmd VimLeave * call system("xsel -ib", getreg('+'))
getreg('+')
. –
Grouch sudo
, you can use only in applications with sudo access. It took me an hour to realize why I was not able to paste in my terminal after copying something which I had opened as sudo
. –
Aelber autocmd VimLeavePre * call system("wl-copy -o", getreg('+'))
In my case this also does not clear the clipboard in case getreg('+')
returns nothing. –
Corral Install Parcellite, or glipper for Gnome and klipper for KDE.
Restart your computer or run it manually.
Based on Matt's answer, the following uses xclip
instead of xsel
:
autocmd VimLeave * call system('echo ' . shellescape(getreg('+')) .
\ ' | xclip -selection clipboard')
echo -n
and it won't add a newline to your clipboard ;) –
Dome xclip
solution is probably less error prone using this: system("xclip -selection clipboard -i", getreg('+'))
–
Circulate I ran into this issue and a related problem where suspending vim with ctrl-z
would also clear the clipboard. I've extended Matt's solution to fix both:
set clipboard=unnamedplus
if executable("xsel")
function! PreserveClipboard()
call system("xsel -ib", getreg('+'))
endfunction
function! PreserveClipboadAndSuspend()
call PreserveClipboard()
suspend
endfunction
autocmd VimLeave * call PreserveClipboard()
nnoremap <silent> <c-z> :call PreserveClipboadAndSuspend()<cr>
vnoremap <silent> <c-z> :<c-u>call PreserveClipboadAndSuspend()<cr>
endif
The if executable("xsel")
guard is there to avoid errors if xsel
is not installed. The nnoremap
mapping preserves the clipboard when suspending from normal mode and the vnoremap
mapping handles suspending from visual or select modes.
I've confirmed this works on vim 7.4 and 8.0.
xclip
like call system('echo ' . shellescape(getreg('+')) . \ ' | xclip -selection clipboard')
. When I use ctrl+shift+v will freeze my terminal for a minutes after use <c-z> suspend vim. –
Sulphone Use Neovim. It by default doesn't clear the clipboard on exit. You will still need to set clipboard=unnamedplus
(typically in ~/.config/nvim/init.vim
) and have xsel
or xclip
tools installed.
Keep in mind that some other defaults are different as well.
clipboard=unnamedplus
did the trick for me. It even fixed the issue of the clipboard not carrying across tabs. –
Jillane xclip
and not xsel
. In the meantime i solved it with ``` vim.api.nvim_create_autocmd("VimLeave", { pattern = "*", command = "call system('xclip -selection c', getreg('+'))", }) ``` can you link some source how is it default and why both xsel and xclip should do? Thanks –
Lilithe Based on Matt's answer
When using his method copying multiple lines added slashes to the end of lines when pasting.
This should remedy that.
autocmd VimLeave * exe ":!echo " . shellescape(getreg('+')) . " | xclip -selection clipboard"
When i used "shellescape" with "system" newlines kept getting escaped. But that didn't happen when i used exe.
Don't know the real reason. but this worked.
Please correct me if I'm wrong but from my understandings of Vim...
1) Vim uses registers instead of the clipboard to store copied/cut data.
2) These registers are preserved when exiting vim in a status file but are not accessible outside of the running process unless you manually open the file and inspect its contents
3) Saving stuff to the + registre while Vim runs allows you to paste to other applications.
4) By suspending vim (CTRL-Z) instead of closing it, these registers are still accessible.
Does that provide assistance?
Here's a solution for Neovim's init.lua
, which also avoids this bug and its resulting error message, when exiting Neovim:
vim.api.nvim_create_autocmd({ "VimLeave" }, {
callback = function()
vim.fn.jobstart('echo "' .. vim.fn.getreg('+') .. '" | xclip -selection clipboard -i', {detach=true})
end,
})
I can't test a solution for Wayland currently (assuming Wayland has the same issue), but for wl-copy
, it should be able to take the text to be copied as a list of strings after the command, so an approach like this should work:
vim.fn.jobstart({'wl-copy', vim.fn.getreg('+')}, {detach=true})
It does seem like it takes a second or two to actually get copied after exiting, though.
© 2022 - 2024 — McMap. All rights reserved.
+
register to copy? – Velum*
register and it started to work this way for+
register after some update (if I am not mistaking, it was X update). – Grouch