Vim: copy contents of current file to MacOS clipboard

If you’d like to copy the contents of the current file (buffer) to the Mac system clipboard, there are a couple of ways of doing this:

Option 1:

Use the terminal utilities to output the files and pipe contents to the clipboard.

:!cat % | pbcopy

Option 2:

Set the vim clipboard to be the system clipboard:

:set clipboard=unnamedplus

Then any yanking and deleting operations in vim will use the system clipboard. So, for example:

ggyG

Option 3:

Looking at the last yank command, did you know you can actually specify the target clipboard within the yank?

gg"*yG

In this command "* is the system clipboard. On some systems it might be "+ instead.