Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VI Editor : Yank the entire file to clipboard (specific to OS X)

Tags:

vim

macos

Is there any way to copy all lines from a file to clipboard in VI editor. I have tried *yG, +yG, "+yG and :%y+ from previous posts in SO, but nothing works in OS X.

like image 243
Antony Thomas Avatar asked Jul 26 '12 05:07

Antony Thomas


2 Answers

The default Vim shipped with Mac OS X, /usr/bin/vi[m], isn't compiled with clipboard support.

You have three options:

  1. use pbcopy from the command line, without using Vim

     $ cat filename | pbcopy
    
  2. use pbcopy from Vim

     :%w !pbcopy
    
  3. get your own Vim with clipboard support

    You can do that through MacPorts or Homebrew, by downloading MacVim or by building from the source.

Also the correct way to use a specific register with y is "{register}y.

See $ man pbcopy in your terminal and :help clipboard and :help ! in Vim.

like image 125
romainl Avatar answered Oct 01 '22 10:10

romainl


G just means "go to the end of the file", and you need quotes before * or + to make them effective as clipboard registers. You're looking for something more like gg"*yG which means:

gg - go to the top of the file

"* - use the * register

y - begin yank

G - go to the bottom of the file

or you could use :!cat % | pbcopy which is not unlike the fine solutions romainl provided.

like image 20
Conner Avatar answered Oct 01 '22 08:10

Conner