Vim: Copying to and retrieving from the clipboard
My memory when it comes to remembering how to get text to and from Vim via the clipboard is pretty bad so I thought I’d try summarising what I know and see if that works out any better.
We can access the system clipboard via the '+' buffer so the commands revolve around that.
Copying to the clipboard
To copy the whole file to the clipboard we can use this command:
:%y+
Or if we want to get the data between lines 4 and 10 then we could do:
:4,10y+
If we want just the current line the following will work:
:.y+
If we want to copy lines via visual mode, we would use 'v' to go into that and select which lines we want to copy followed by:
"+y
Retrieving from the clipboard
Retrieving data from the clipboard is a case of accessing the '+' buffer.
Therefore to paste whatever’s in the clipboard into a file we’d do this:
"+p
If we wanted to paste it just before the current position of the cursor then we’d do this instead:
"+P
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.