Home Using system clipboard in Vim on macOS
Post
Cancel

Using system clipboard in Vim on macOS

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

CommandAction
ywCopy word
yyCopy line
gg"*yGCopy entire file
y$Copy till end of the line
dwCut word
ddCut line
d$Cut till end of the line
PPaste before cursor
pPaste after cursor
This post is licensed under CC BY 4.0 by the author.