32
votes

I am using Ubuntu 12.04 Beta and Vim that has come with it. I am trying to use Vim to copy the content of a text file to Chrome browser. I have tried +, * y and all its variants. I have tried to :set clipboard=unnamed and :set clipboard=unnamedplus. Not working. I am not trying to use xclip, or GVim or any of that. I tried with xclip (not a standard package in Ubuntu 12.04), but that too does not work, also too much effort.

How do I copy the text to the clipboard and then paste anywhere, like Chrome?

8
What is the output of vim --version?David Pope
"+y works for me in Ubuntu (and all other platforms I have tried).Prince Goulash
Yes, that Beta tag made me curious.David Pope
What's the problem with xclip? cat file-to-copy.txt | xclip works quite well and is easy to remember.Danilo Bargen
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Mar 1 2012 22:09:36) Included patches: 1-429 Modified by [email protected] Compiled by buildd@ Huge 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 +digraphsGattoo

8 Answers

29
votes

Your version of Vim doesn't support X, which is required for clipboard access. By default, Ubuntu ships several builds of vim and only the GUI variant supports clipboard access. I always recompile vim from source so that a single vim (with symlinks for gvim etc) supports everything required (including :gui to switch from command line to GUI version). It's really very easy to do:

# Get the compile-dependencies of vim
sudo apt-get build-dep vim
# If you haven't got mercurial, get it
sudo apt-get install mercurial
# Get the source
hg clone https://vim.googlecode.com/hg/ vim_source
# Compile it
cd vim_source
./configure \
    --enable-perlinterp=dynamic \
    --enable-pythoninterp=dynamic \
    --enable-rubyinterp=dynamic \
    --enable-cscope \
    --enable-gui=auto \
    --enable-gtk2-check \
    --enable-gnome-check \
    --with-features=huge \
    --with-x \
    --with-compiledby="Your Name <[email protected]>" \
    --with-python-config-dir=/usr/lib/python2.7/config
make && sudo make install

That will install it in /usr/local, so make sure that's in your PATH before /usr and it will be used instead of the Ubuntu versions.

29
votes

The output from vim --version should show something like this:

Huge version with GTK2-GNOME GUI.  Features included (+) or not (-):

and further down in the output you should see stuff like +Xll:

+vreplace +wildignore +wildmenu +windows +writebackup +X11 -xfontset +xim 
+xsmp_interact +xterm_clipboard -xterm_save 

That means your console vim can copy/paste to/from the X11 clipboard.

Try apt-get install vim-gtk

8
votes

Install the package vim-gnome instead of vim. It comes with clipboard enabled.

5
votes

You can also add shortcuts to your vimrc

# Copy and paste
vmap <C-c> "+yi
vmap <C-x> "+c
vmap <C-v> c<ESC>"+p
imap <C-v> <ESC>"+pa

It will allow you to Copy by Ctrl + C and Paste by Ctrl + V

2
votes

If you have run configure as explained by @DrAl but you still can't get the GUI compiled in and you see this in the output of your ./configure

checking for X... (cached) no

Then you may have to delete the cache file that configure created.

find . -name config.cache -delete

Then re-run configure and make and check src/vim --version again - it should show that gui is included now.

2
votes

You can do it; just pipe to xclip.

gg
V
G
:'<,'>w !xclip

from here: in vim with xclip, yank to clipboard

0
votes

I would open the file in the browser using a file URL:

file:///home/dave/some-file

Not super elegant but it works.

0
votes

I really enjoy this set of shortcuts:

" Add shortcut for clipboard registers
noremap <leader>p "*p
noremap <leader>y "+y
noremap <leader>d "+d

It is just an easier way then type "* and "+ every time.