This is mostly a note to my future self who has by now forgotten how to get the system clipboard working with Vim. Maybe it'll help someone else as well.
Ensure your version of Vim has clipboard support
Check your Vim version with:
$ vim --version VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Dec 19 2013 15:19:49) Compiled by [email protected] Normal version without GUI. Features included (+) or not (-): -arabic +autocmd -balloon_eval -browse +builtin_terms +byte_offset +cindent -clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments -conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs -dnd -ebcdic -emacs_tags +eval +ex_extra +extra_search -farsi +file_in_path +find_in_path +float +folding -footer +fork() -gettext -hangul_input +iconv +insert_expand +jumplist -keymap -langmap +libcall +linebreak +lispindent +listcmds +localmap -lua +menu +mksession +modify_fname +mouse -mouseshape -mouse_dec -mouse_gpm -mouse_jsbterm -mouse_netterm -mouse_sysmouse +mouse_xterm +multi_byte +multi_lang -mzscheme +netbeans_intg -osfiletype +path_extra -perl +persistent_undo +postscript +printer -profile +python/dyn -python3 +quickfix +reltime -rightleft +ruby/dyn +scrollbind +signs +smartindent -sniff +startuptime +statusline -sun_workshop +syntax +tag_binary +tag_old_static -tag_any_white -tcl +terminfo +termresponse +textobjects +title -toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo +vreplace +wildignore +wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp -xterm_clipboard -xterm_save system vimrc file: "$VIM/vimrc" user vimrc file: "$HOME/.vimrc" user exrc file: "$HOME/.exrc" fall-back for $VIM: "/usr/share/vim" Compilation: gcc -c -I. -D_FORTIFY_SOURCE=0 -Iproto -DHAVE_CONFIG_H -arch i386 -arch x86_64 -g -Os -pipe Linking: gcc -arch i386 -arch x86_64 -o vim -lncurses
This is the version of Vim that ships with OS X 10.9.5 Mavericks. Notice the line that says
-clipboard. This means that this version of vim was compiled without clipboard support.
We can get a version that does have clipboard support with homebrew:
$ brew install vim
vim --version again. It might still show you the same version. If this is the case, rename the system installed Vim binary so that the one installed by homebrew will be used instead.
You can find out where the vim that's currently being run is located with:
$ which vim /usr/bin/vim
And rename it with:
$ sudo mv /usr/bin/vim /usr/bin/vim73
We should now see the homebrew version of Vim being used:
$ which vim /usr/local/bin/vim
And when we run
vim --version, we should see that this version has clipboard support:
$ vim --version VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug 15 2014 11:34:56) MacOS X (unix) version Included patches: 1-335 Compiled by Homebrew Huge version without GUI. Features included (+) or not (-): +acl +farsi +mouse_netterm +syntax +arabic +file_in_path +mouse_sgr +tag_binary +autocmd +find_in_path -mouse_sysmouse +tag_old_static -balloon_eval +float +mouse_urxvt -tag_any_white -browse +folding +mouse_xterm -tcl ++builtin_terms -footer +multi_byte +terminfo +byte_offset +fork() +multi_lang +termresponse +cindent -gettext -mzscheme +textobjects -clientserver -hangul_input +netbeans_intg +title +clipboard +iconv +path_extra -toolbar +cmdline_compl +insert_expand +perl +user_commands +cmdline_hist +jumplist +persistent_undo +vertsplit +cmdline_info +keymap +postscript +virtualedit +comments +langmap +printer +visual +conceal +libcall +profile +visualextra +cryptv +linebreak +python +viminfo +cscope +lispindent -python3 +vreplace +cursorbind +listcmds +quickfix +wildignore +cursorshape +localmap +reltime +wildmenu +dialog_con -lua +rightleft +windows +diff +menu +ruby +writebackup +digraphs +mksession +scrollbind -X11 -dnd +modify_fname +signs -xfontset -ebcdic +mouse +smartindent -xim +emacs_tags -mouseshape -sniff -xsmp +eval +mouse_dec +startuptime -xterm_clipboard +ex_extra -mouse_gpm +statusline -xterm_save +extra_search -mouse_jsbterm -sun_workshop -xpm system vimrc file: "$VIM/vimrc" user vimrc file: "$HOME/.vimrc" 2nd user vimrc file: "~/.vim/vimrc" user exrc file: "$HOME/.exrc" fall-back for $VIM: "/usr/local/share/vim" Compilation: /usr/bin/clang -c -I. -Iproto -DHAVE_CONFIG_H -DMACOS_X_UNIX -Os -w -pipe -march=native -mmacosx-version-min=10.9 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 Linking: /usr/bin/clang -L. -L/usr/local/lib -L/usr/local/lib -Wl,-headerpad_max_install_names -o vim -lm -lncurses -liconv -framework Cocoa -fstack-protector -L/usr/local/lib -L/System/Library/Perl/5.16/darwin-thread-multi-2level/CORE -lperl -framework Python -lruby.2.0.0 -lobjc
Accessing the clipboard from Vim
We now have access to the
+ register, which works just like any other Vim register, except it also doubles as the system clipboard.
So to copy a block of text from Vim to the clipboard, we might enter visual mode and then use
"+y, that's double quotation followed by plus to get access to the clipboard register, and then
y to yank the current selection into it.
We can now go into any other application and paste like normal.
Additionally, we can paste from the system clipboard into Vim. This tends to produce better results than
cmd-v. To do this is as simple as:
That's it! Vim can now communicate with OS X via the clipboard.