It’s not uncommon to want to copy something from Vim and paste into it into your terminal, your tmux, your browser, or elsewhere. I despise the inefficiencies of switching to the mouse, so I’m always on the lookout for a method of pasting in the terminal that which I have in a Vim buffer.
When text is copied into the *
(star) register in Vim, it’s lands in your system clipboard. (There is an additional +
register which, on many systems are the same but on Linux with X11 can be different. See :help x11-selection
.)
When switch from Vim to any other application, you can simply paste from the system clipboard and the star register contents from Vim will be paste.
How does one copy into the *
register? The same way one copies into any specific register, by prepending any yank command with “n
where n is a register name. Here are some examples:
“*yy
- Yank the current line into your * register, the system clipboard.“*yap
- Yank Around Paragraph: copies the whole paragraph, method, or function.
Conversely, and in fact maybe even more useful: If you’ve copied into the system clipboard outside from outside of Vim (who doesn’t copy from StackOverflow?), then you can paste it from the star register, by prepending any paste command with “*
like so:
“*p
- Paste from the system clipboard below the current line“*P
- Paste from the system clipboard above the current line
In the short screencast below, I copy some code from StackOverflow using regular macOS copy commands. Then, back in Vim, I paste into the current buffer from the clipboard register using “*p
. Watch:
For more information about registers in general and the star and plus registers, checkout help :registers
.
Was this useful? Help us improve!
With your feedback, we can improve VimTricks. Click a link to vote:
Hi! It didn't work for me. Instead I used the trick with :w !sudo tee % in this way :w !xclip -selection clipboard