How to copy contents of a (large) vim file to the OSX system clipboard to paste in other apps?

6,898

Solution 1

I've solved this in a better way than my other answer:

First of all get the new vim with

brew install vim

and then do

vim --version

Make sure it is 7.4+ Note that if you do vi --version you may that vi is at 7.3

Now make vi use that newer vim

alias vi=vim

and add that alias to your .bash_aliases file (or wherever you put aliases)

Finally add

set clipboard=unnamed

to your .vimrc (not .bashrc!)

Start a new shell and it should work.

Solution 2

I'm sure this is a duplicate but:

gg"*yG

gg go to the first line
"*y start a yank to the system clipboard "register"
G move to the end of the file (you will see how many lines were yanked)

Solution 3

This alternative option using a : command worked for me:

:1,$!pbcopy

This is useful for referring to line numbers, etc without having to navigate and highlight the area.

Share:
6,898

Related videos on Youtube

Michael Durrant
Author by

Michael Durrant

rails ruby rspec rock

Updated on September 18, 2022

Comments

  • Michael Durrant
    Michael Durrant over 1 year

    I like to use vim to edit other (non-programming languages) stuff. So I paste the content into vim and make the changes.

    How can I copy all of the text back into the primary clipboard for pasting?

    Note that I use iTerm2 and the ability to use * isn't there as it is with the basic osx terminal app.

    I don't want to copy to a vim internal register (such as the y yank command), I want to copy to the system clipboard.

    Also, I want the contents of the whole file, including the content which is off-screen?

    The alt-left-click-drag with mouse doesn't work.

    • Evgeny Vereshchagin
      Evgeny Vereshchagin over 8 years
      on OSX :%w !pbcopy
    • Michael Durrant
      Michael Durrant over 8 years
      I got /bin/bash: pbcopy: command not found
    • Michael Durrant
      Michael Durrant over 8 years
      However +1 as it led me to an answer (below). Thanks!
  • muru
    muru over 8 years
    @MichaelDurrant talking in ALL CAPS doesn't make you right. The * and + buffers are for interfacing with the system clipboard. See vi.stackexchange.com/a/96/205
  • Michael Durrant
    Michael Durrant over 8 years
    This simply didn't work for me.
  • muru
    muru over 8 years
    @MichaelDurrant Hmm. OSX, isn't it? Try the + register (gg"+yG).
  • Michael Durrant
    Michael Durrant over 8 years
    Thanks muru, Looks like my version of bash doesn't this functionality. I am on Yosemite 10.10.4