If I've already yanked 37 lines, is there a way to send them to the clipboard without having to select them again?
I need to move those yanked lines into a GUI application.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityI’m going to go ahead and infer from your use of the word “select”, that you yanked via visual mode.
If that’s the case, then you can use gv
to reselect the previous selection. You can then yank to the clipboard as usual "+y
.
gv
is enough to put it in PRIMARY selection on systems using X11 (e.g. linux). So, one can gv
then middle-click to paste. One can also use a clipboard manager that synchronizes the clipboard and primary selection to paste with Ctrl+v.
:h clipboard-unnamed
is not needed for what I described, but it seems like a good, related, additional feature. With it, apparently not only are visual selections immediately available in PRIMARY selection, but also the text that's yanked, deleted, changed, or put without involving visual selection.
:h clipboard
, what I described in my first comment is controlled by :h clipboard-autoselect
, further down is :h clipboard-autoselectplus
which is similar, but puts the visual selection automatically in the OS CLIPBOARD, the one that's pasted with Ctrl+v.
vim --version
. If the default vim
from your package manager doesn't have +clipboard, you might try the vim
that is installed along with the gvim
package -- that usually does.
The +
and *
registers can be used to access the clipboard; and you can assign values to registers with let @<register-name>
. For example if you've pressed y
to yank text to the "0
register:
:let @+ = @0
If you're using Windows or macOS then @+
and @*
are identical and it doesn't matter which one you use; if you're using Xorg then @*
is the mouse middle click clipboard (PRIMARY) and @+
is the the <C-v>
clipboard (CLIPBOARD). I'm actually not sure how Wayland works with this.
How can I copy text to the system clipboard from Vim? has some more information about Vim and clipboards, which might be useful.
not sure how Wayland works with this
FYI, Wayland is a set of protocols. Any composer/server must support only core protocols featuring a single "clipboard". Anything else is optional and is not included into reference implementation. In practice, a second clipboard may be supported by 3rd party composers using custom protocol(s).
I have the following maps in my .vimrc to do just that (i.e. after I've prematurely yanked... copy the yanked text into my system clipboard by pressing <LEADER>y/Y
, where leader is by default the \
key):
" copy contents of unnamed register to system CTRL-C clipboard
nnoremap <silent> <LEADER>y :let @+=@0<CR>
\ :echo 'copied to CTRL-C clipboard'<CR>
" copy contents of unnamed register to system highlighted clipboard
nnoremap <silent> <LEADER>Y :let @*=@0<CR>
\ :echo 'copied to highlight clipboard'<CR>
As pointed out by @MartinTournoij, if you're using Windows or macOS then these two maps are identical and you can just use one of them.
A pair of simple mappings for all who suffer:
" - to set unnamed register
nmap - <cmd>call setreg('@',
\ #{points_to: v:register is '"' ? v:count % 10 : v:register})<CR>
" + to set clipboard register
nmap + <cmd>call setreg('+',
\ getreginfo(v:register is '"' ? v:count % 10 : v:register))<CR>
"Plus" key to set clipboard register (to cure premature yanking):
+
to set clipboard to "zero" register (last yank);1+
to set clipboard to "one" register (last delete);"-+
to set clipboard to "minus" register (last small delete); etc."Minus" key to set unnamed register (to cure premature deletion):
-
to point unnamed to "zero" register (last yank);1-
to point unnamed to "one" register (last delete);"--
to point unnamed to "minus" register (last small delete); etc.