56

I'm running Vim 7.3 under Linux Mint 13 (using MATE) and I'm not able to save text to the system clipboard. I run Vim in the terminal and copy text from the terminal with CTRLINSERT. When I select text in Vim (either with the mouse or in visual mode), CTRLINSERT doesn't copy any text. In addition when I right-click, Copy is grayed out. Further, I can't write to the system buffer by yanking to the corresponding register using vim commands. However, I'm able to paste while in insert mode (using SHIFTINSERT or right-click paste). I'm also able to copy text directly from the terminal using the same technique, just not text from Vim.

Here is my current ~/.vimrc. The relevant part is most likely set clipboard=autoselect,unnamed,exclude:cons\|linux. If I put finish at the top of my ~/.vimrc, I have the same issue, so I think the line is wrong, but I've tried set clipboard=unnamed and had the same behavior.

Could there be another config file affecting Vim's behavior? How can I change my ~/.vimrc to allow me to copy text from Vim?

maksim
  • 671

10 Answers10

108

I believe your copy problem is caused by:

mouse=a 

in your .vimrc This sets your vim into visual mode whenever you select something with the mouse. And for some mad reason one is not allowed to copy when in visual mode. You can get around it by holding down shift when selecting text not to go into visual mode allowing you to use the copy menu.

or you could change to:

:set mouse=v

and copy will work again.

Add :set mouse=v to your ~/.vimrc file if you want it to be permanent (create ~/.vimrc if it does not exist yet).

Tms91
  • 208
21

sudo apt-get install vim-gtk will likely solve this problem for other Googlers.

Good ol' terminal-style vim doesn't have access to the clipboard, which is a part of the X windows system. Installing vim-gtk gives vim the access that it needs to modify and view the clipboard.

Cory Klein
  • 1,752
2

just gvim on arch linux if anyone wants to know. Also, have a look at neovim (and python-neovim && python2-neovim for You Complete Me support, etc).

dylnmc
  • 251
1

Using Pop!_OS 21.04 and ran into this issue.

Per this github comment, installing xclip worked fixed this for me.

sudo apt install xclip

0

I use this plugin. Works even if you have -xterm_cliboardor -clipboard. The mapping is also very simple, just cp and cv for copy and paste.

0

On the question text at first:
I do not believe that you cannot select and copy with the mouse "what you see" in the window since I have the same issue like you, it seems, and I can do so, but I cannot mark more than the window and copy that, only then, Copy is grayed out. Well, we might have different problems here. Still, this tool trick should help as well:

Alacritty

You need a terminal program that embeds vim into the terminal text, for example the "Alacritty" terminal. With that, you can just zoom out and copy with Ctrl+Shift+C or your clipboard key.

You can also try the vim mode: press Ctrl+Shift+Space, you can then move through the console history and mark what you need, if you need all, type the vim command ggVG, afterwards, you can paste it in a text file and then back in terminal, you get out of the vim mode by pressing Ctrl+Shift+Space again.

Tmux + clipboard (in Alacritty or another terminal)

If you need to copy large text that you cannot copy with the zoom out trick of Alacritty, install tmux and a clipboard like xclip or xsel. Tmux is not a terminal on its own. Try its commands in Alacritty or any other terminal.

sudo apt-get update -y 
sudo apt-get install -y xclip

And put this in your ~/.tmux.conf to bind y to the xclip copy:

bind -T copy-mode-vi y send -X copy-pipe "xclip -selection c"

With the tmux shortcut Ctrl+B-->[ you can start marking whatever you want with Space and then Arrows or picture up/down. After you marked all of the needed text, press y that you bound to "copy to xclip". That makes it possible to copy anything, no matter what size.

See also How to copy multi-page text from the terminal into the clipboard?.

questionto42
  • 2,691
0

I was having the same trouble in nvim on macOS, and got it working by running this (no need to modify it, just press : then run as-is):

set mouse=

After that I could copy/paste from nvim in the terminal as expected.

I found this information here

stevec
  • 973
0

I ran into this issue when sshing from tmux into an aws machine that was running a database - I wanted to get some query results out of psql and into my machine's vim (so I could pretty print and then further analyse).

I outputted a file from psql using \o filename ran the query and then \o again. It produced 200 odd lines of output, of which many of the lines would take multiple screens to display with :set wrap switched on. I was hoping to yank the contents of the file into the clipboard in the remote machine's vim using "*y or "+y but pressing the register part of either of these commands terminated the command.

Tbh I'm a bit of a fish out of water in unmodified vim as I have loads of plugins and settings which I couldn't use in this situation. I remembered the clipboard option but when trying to look at its value using :set clipboard? I got the message E519: Option not supported: clipboard?

After doing a bit of looking around I think this was because the vim in question showed -clipboard and '-xterm_clipboard' in the output of :version which I think is the issue

The help docs (:help 'clipboard') suggest that the xterm one is necessary to be able to use the clipboard setting:

'clipboard' 'cb'        string  (default "autoselect,exclude:cons\|linux"
                                                  for X-windows, "" otherwise)
                        global
                        {only in GUI versions or when the +xterm_clipboard
                        feature is included}

Anyway the workaround I used in this situation was to just cat the file to the terminal and then use tmux's copy and paste Ctrl-B + [ followed by space and selection of the lines followed by Enter and then after swapping to a more useful vim opening a new buffer/tab :set paste and entering insert mode and pressing Ctrl-B + ] to get tmux to dump the copied lines and finally :set nopaste to leave paste mode once back in normal mode

You could probably also transfer files in a similar situation via scp but I was two ssh hops in so this was much easier!

I thought I'd document this as when searching for E519: Option not supported: clipboard? I only got 1 unrelated hit on google! and in this case changing the value of 'mouse' wouldn't have helped because I couldn't fit all the output on one screen to use the mouse on it

As a bit of an aside you definitely can use the system clipboard in a more normal terminal vim - I use it within tmux and I think the incantation to get it to 'just work' as you'd expect (eg system clipboard can be pasted by just pressing 'p' and any yank/cut goes into the system clipboard) is

set clipboard=unnamedplus

Terminal vim comes in many different sizes and flavours and if you don't have the clipboard flags present in :version you might have vim-tiny or similar installed, I think I run a custom compiled version that is based on massive (or huge?! can't remember the adjective) but you'll definitely get that feature compiled in on in a lot of other stock versions.

JonnyRaa
  • 213
0

Probably you have set mouse=a enabled. This happens because Vim starts treating mouse interactions (like selecting text) as part of its internal selection mechanism rather than passing it to the terminal or system clipboard.

While it can be handy to use mouse to point to text, it's useless if you can't copy text.

I have this simple hack, where I press m and mouse mode is enabled and pressing m again disables it, so you can copy text.

On Linux/Mac:

  1. edit .vimrc using vim ~/.vimrc (don't worry it can be empty if you don't have previous configurations)
  2. paste this to the end of file
nnoremap m :call ToggleMouse()<CR>

function! ToggleMouse() if &mouse == 'a' set mouse= echo "Mouse mode OFF" else set mouse=a echo "Mouse mode ON" endif endfunction

  1. exit using :wq
himiko
  • 1
0

The terminal doesn't know anything about vim selections, so don't expect it to be any help. You will need vim to be built against X (e.g. gvim -v), and then yank to and put from the + register.