Vi(m) tip #9: Copy, Cut and Paste into Mac OS X clipboard

Alec Jacobson

June 30, 2010

weblog/

I can get a lot done in vim without ever having to use the mouse or really exit "vim world". But the one thing that I keep falling back on is copy and pasting from and to other applications. Here's a way to do these operations without resorting to the mouse or foreign keystrokes. Issue these in command mode:

Cut line under cursor

:.!pbcopy

Copy line under cursor

:.!pbcopy|pbpaste

Paste as line beneath cursor

:r !pbpaste
You can also use this if you've made a selection in Visual Mode:

Cut current selection

(warning: this grabs the whole lines of any lines within selection)
:'<,'>!pbcopy

Copy current selection

(warning: this grabs the whole lines of any lines within selection)
:'<,'>!pbcopy|pbpaste
Note: It seems for that one you should just select the first letter of each line. Not the entire block, then it just cuts... Update: It's much simpler:
"*yy
Yanks the current line to the clipboard. Similarly,
"*dd
cuts the current line and
"*p
pastes the clipboard below the current line.

Comments

February 28, 2011, Colin
How would you map these to 'x', 'c', 'v' (without the command key) in visual mode? vmap x :'!pbcopy vmap c :'!pbcopy|pbpaste ?