24

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.

1

4 Answers 4

15

I’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.

5
  • 2
    Additionally, the 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.
    – JoL
    Aug 12, 2022 at 17:52
  • 1
    @JoL I had no idea that the last visual selection was available in the primary selection on linux! Thank you for this!
    – mattb
    Aug 12, 2022 at 18:54
  • @Quasímodo :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.
    – JoL
    Aug 14, 2022 at 15:48
  • 1
    Checking the other possible values of :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.
    – JoL
    Aug 14, 2022 at 16:05
  • For anyone else with this issue, make sure that you have your Vim compiled with the "+clipboard" setting. This allows it to access system clipboard. You can check whether this is set with 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.
    – rudiejd
    Aug 14, 2022 at 16:40
22

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.

3
  • 1
    I did post an answer, but your way is much more direct - so now I'm using it instead of the less elegant maps I was using. Thanks!
    – mattb
    Aug 11, 2022 at 23:19
  • 2
    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).
    – Matt
    Aug 12, 2022 at 2:14
  • 2
    FWIW, I see the same behavior in X and Wayland.
    – Biggybi
    Aug 13, 2022 at 9:04
3

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.

0
3
+100

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.