32

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?

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Gattoo
  • 3,019
  • 7
  • 25
  • 21
  • 1
    What is the output of `vim --version`? – David Pope Apr 11 '12 at 07:22
  • 3
    `"+y` works for me in Ubuntu (and all other platforms I have tried). – Prince Goulash Apr 11 '12 at 07:30
  • Yes, that Beta tag made me curious. – David Pope Apr 11 '12 at 07:31
  • What's the problem with xclip? `cat file-to-copy.txt | xclip` works quite well and is easy to remember. – Danilo Bargen Apr 11 '12 at 07:52
  • VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Mar 1 2012 22:09:36) Included patches: 1-429 Modified by pkg-vim-maintainers@lists.alioth.debian.org 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 +digraphs – Gattoo Apr 11 '12 at 08:44
  • This is truncated because of message length limit – Gattoo Apr 11 '12 at 08:44
  • For Xclip, I used - http://stackoverflow.com/questions/5240473/in-vim-with-xclip-yank-to-clipboard – Gattoo Apr 11 '12 at 08:47
  • In response to the two "close as off topic" votes, this is NOT off-topic. Please read the FAQ before voting to close: "if your question generally covers ... software tools commonly used by programmers ... then you’re in the right place to ask your question!". Vim is commonly used by programmers, so this is an on-topic question. – DrAl Apr 13 '12 at 14:30
  • I discovered the easiest answer to this problem: Install gvim; run gvim -v on the command line to open gvim exactly as vim, with system clipboard functionality intact. – Gattoo Nov 28 '12 at 11:28
  • Installing Vim8 with clipboard support: https://askubuntu.com/questions/1005205/vim-8-with-system-clipboard-on-16-04-lts – icc97 Oct 18 '20 at 09:47
  • Also interesting is the differences between the vim versions: https://askubuntu.com/questions/281886/what-are-the-differences-between-the-different-vim-packages-available-in-ubuntu – icc97 Oct 18 '20 at 09:48

8 Answers8

29

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 <youremail@domain.com>" \
    --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.

DrAl
  • 70,428
  • 10
  • 106
  • 108
  • good suggestion, @DrAl. Will it affect Vim that is already working? I don't want to break Vim. I can lose clipboard function for other benefits of Vim. – Gattoo Apr 12 '12 at 06:17
  • 1
    You'll end up with two vim's: `/usr/bin/vim` and `/usr/local/bin/vim`. You can always run the old one with the explicit path. I'm not sure what you think is likely to be broken though: you can compile in any features that you need, but this configure script puts most stuff in. See `./configure --help` for more options. – DrAl Apr 12 '12 at 09:19
  • I should clarify my comment: you'll also end up with two gvims: `/usr/bin/gvim` (from vim-gtk) and `/usr/local/bin/gvim` (a symlink to `/usr/local/bin/vim` that will open in GUI mode by default rather than the one names 'vim' that will open in console mode). It will support clipboards whether or not you're in GUI mode (and even do so over ssh if you `ssh -X` with an X-server on the ssh client machine). – DrAl Apr 12 '12 at 11:21
  • ummm. that makes the things difficult. At the same time, I realized that the reverse works fine. When I am scrapping some content from, say, Chrome, I do a CR-SHIFT-V and content comes to Vim -console mode. It misses some characters at the top. But that still works. The reverse does not work. – Gattoo Apr 13 '12 at 06:43
  • I'm not sure I follow you. I have an X server (actually Cygwin-X11) on the machine I'm sitting in front of. I do "ssh -X remote-server" and then (on the remote server) run vim. If I do `"+y` I can then switch to Windows notepad (or whatever) and hit paste and the text copied in remote vim appears in local notepad. If I copy some to the clipboard in notepad, I can switch to remote-vim and do `"+p` and it'll be pasted into remote vim. Obviously you can still use the copy/paste provided by your terminal (putty or whatever) if you prefer, but it gives you extra options. – DrAl Apr 13 '12 at 13:17
  • This is a good suggestion @DrAl. And it seems to work though I have -X11 in the Vim settings. I needed to choose the visual mode and copy the required part. I think we should conclude with the solution that says: Go to Visual Mode and select the text. "+y or "*y to yank those lines to the system clipboard Paste it in X11 all. To paste in Vim, use "+p. NO COMPILATION REQUIRED for Ubuntu12.04. No Beta bug. I sincerely thank everyone who supported me on this. – Gattoo Apr 18 '12 at 06:44
  • Please note that the repo URL should be changed. If you still want to use Mercurial, use `https://bitbucket.org/vim-mirror/vim`. If you'd prefer to change to git, use `https://github.com/vim/vim.git`. – csi Nov 25 '15 at 17:00
  • 1
    [The repo URL and build instructions have since changed on Ubuntu 16.04.](https://stackoverflow.com/questions/37079424/ubuntu-16-04-lts-cant-enable-xterm-clipboard-in-vim/37079474#37079474). – Cloud May 06 '16 at 18:52
29

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

sashang
  • 11,704
  • 6
  • 44
  • 58
  • 2
    This. You should use vim-gtk, and perhaps consider putting `set clipboard=unnamedplus` or set `clipboard=unnamed` into your vimrc, so that the default register is also used for clipboard operations. – ldx Apr 11 '12 at 19:08
  • I have -x11 -xim -xsmp -xterm_clipboard -xterm_save. Does it mean that I would never get clipboard functionality? How would I compile vim to have all good features? Is it to do with beta and will change when the final version is released? I also want to use vim in ssh, so no to vim-gtk – Gattoo Apr 12 '12 at 06:12
  • Did you try what I suggest? If you did you'd find that you can still use it in the console over ssh. – sashang Apr 12 '12 at 11:01
  • @sashang as I said, I have -X11, so I don't have the clipboard. i would like to use pure vim. I will surely go for the DrAl solution. Let's see. – Gattoo Apr 12 '12 at 11:17
  • 2
    @Gatoo: Ok so basically you want to build a version of vim from the source code that's going to have the same or close to the same features as what Ubuntu provides for you via an 'apt-get install vim-gtk'? – sashang Apr 13 '12 at 08:13
  • +1 I had the same exact same issue as the op minus the Beta part. As per this post I installed vim-gtk using `sudo apt-get install vim-gtk`. My original version of vim was not disturbed by this. It accepted the new installation with great courtesy. vim --version now shows all the additional required compilation options (+X11 +clipboard etc.). I had to restart my computer to get "+yy working. This apt-get approach ensures that we are notified of any updates by the update manager. Compile from source and you must check for updates yourself. – AJ Dhaliwal Jun 26 '13 at 18:23
  • cloud-testing-ci-130cloud-testing-ci-130 – Aubergine Nov 11 '13 at 14:03
  • Note that this answer is 8 years old now, so things have shifted slightly. To get the latest Vim 8 in Ubuntu 16 and 18, you should use the `jonathonf/vim` repository. Then install the `vim-gtk3` version which is for GNOME3. `sudo add-apt-repository ppa:jonathonf/vim`, `sudo apt update`, `sudo apt install vim-gtk3`. See this question and the accepted answer for more details: https://askubuntu.com/questions/1005205/vim-8-with-system-clipboard-on-16-04-lts – icc97 Oct 18 '20 at 09:46
8

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

dan-klasson
  • 13,734
  • 14
  • 63
  • 101
  • 1
    I have not been looking at a Windows based model when using Vim. The first requirement is to work on a non-GUI box. Does it support non-GUI operation? – Gattoo Nov 28 '12 at 07:48
  • `vim-gnome` is for Linux. And I am not sure if it works on a non-GUI box. I haven't tried it on a server. – dan-klasson Nov 30 '12 at 09:25
  • @dan-klasson, I've just tried it on 12.04. It does work. – mcmlxxxiii Nov 08 '14 at 06:54
4

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

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
lis2
  • 4,234
  • 1
  • 17
  • 9
2

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.

Cory Klein
  • 51,188
  • 43
  • 183
  • 243
2

You can do it; just pipe to xclip.

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

from here: in vim with xclip, yank to clipboard

Community
  • 1
  • 1
ati
  • 307
  • 3
  • 10
0

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

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

Not super elegant but it works.

Big McLargeHuge
  • 14,841
  • 10
  • 80
  • 108
-1

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.

caiolopes
  • 561
  • 8
  • 14