Using system clipboard in Vim on macOS
When you do copy, cut and paste text in Vim it goes into Vim’s own buffer (register). Actually it has many registers, but we are’re interested only in primary (*
or unnamed
).
More details on registers in Vim:
1
:h registers
But it’s possible to make it work with system clipboard too.
Pre requirements
Vim requires the +clipboard
feature flag to be set during compile. Let’s check if Vim has this feature:
1
2
3
$ vim --version | grep clipboard
+clipboard +keymap +printer +vertsplit
+emacs_tags -mouse_gpm -sun_workshop -xterm_clipboard
By default macOS Catalina ships without clipboard. But the good news we can easy install Vim with the +clipboard
feature from Homebrew for example:
1
$ brew install vim
Setup
We can set default register to unnamed
clipboard with command:
1
set clipboard=unnamed
More details on clipboard in Vim:
1
:h clipboard
I use shared .vimrc
configuration file across different machines, so first check if it’s supported by Vim:
1
2
3
4
5
6
7
" .vimrc
"
" Set default clipboard register to system's unnamed
"
if has('clipboard')
set clipboard=unnamed
endif
Now commands like y
and d
will put text into system clipboard as well as p
command will take text from clipboard.
Usage
Command | Action |
---|---|
yw | Copy word |
yy | Copy line |
gg"*yG | Copy entire file |
y$ | Copy till end of the line |
dw | Cut word |
dd | Cut line |
d$ | Cut till end of the line |
P | Paste before cursor |
p | Paste after cursor |