How to copy from vim in ssh session to local clipboard on Mac

If you are using a Mac computer and editing vim inside an ssh session, then you can’t copy from the remote vim session to your clipboard using the familiar “+y. This is because the remove vim doesn’t know anything about your local pc’s clipboard. So, how do we copy text from a remote vim session to your local clipboard? There are 2 options :

1. Highlight the text with your mouse then press cmd+c

Note: if you have

set mouse=a

in your .vimrc file, this won’t work as any highlight with mouse switches vim to visual mode making cmd+c invalid. In this case you could press and hold alt before highlighting.

2. Put the text in a temporary file and transfer it to your local pc by scp, then put it in a the clipboard with pbcopy, i.e. go to local terminal and run these commands:

scp username@remotehost /tmp/stuff.txt
cat /tmp/stuff.txt | pbcopy