TL;DR
Try using "*yy
or "+yy
to copy a line to your system's clipboard.
Full answer
Be aware that copying/pasting from the system clipboard will not work if :echo has('clipboard')
returns 0. In this case, Vim is not compiled with the +clipboard
feature and you'll have to install a different version or recompile it. Some Linux distros supply a minimal Vim installation by default, but if you install the vim-gtk
or vim-gtk3
package you can get the extra features nonetheless.
The "*
and "+
registers are for the system's clipboard (:help registers
). Depending on your system, they may do different things. For instance, on systems that don't use X11 like OS X or Windows, the "*
register is used to read from and write to the system clipboard. On X11 systems, both registers can be used. See :help x11-selection
for more details, but basically the "*
is analogous to X11's _PRIMARY_
selection (which usually copies things you select with the mouse and pastes with the middle mouse button) and "+
is analogous to X11's _CLIPBOARD_
selection (which is the clipboard proper).
If all that went over your head, try using "*yy
or "+yy
to copy a line to your system's clipboard. Assuming you have the appropriate compile options, one or the other should work.
You might like to remap this to something more convenient for you. For example, you could put vnoremap <C-c> "*y
in your ~/.vimrc
so that you can visually select and press Ctrl+c to yank to your system's clipboard.
You also may want to have a look at the 'clipboard'
option described in :help cb
. In this case, you can :set clipboard=unnamed
or :set clipboard=unnamedplus
to make all yanking/deleting operations automatically copy to the system clipboard. This could be an inconvenience in some cases where you are storing something else in the clipboard as it will overwrite it.
To paste you can use "+p
or "*p
(again, depending on your system and/or desired selection) or you can map these to something else. I type them explicitly, but I often find myself in insert mode. If you're in insert mode you can still paste them with proper indentation by using <C-r><C-p>*
or <C-r><C-p>+
. See :help i_CTRL-R_CTRL-P
.
Vim's paste
option (:help paste
) is also worth mentioning: This puts Vim into a special "paste mode" that disables several other options, allowing you to easily paste into Vim using your terminal emulator's or multiplexer's familiar paste shortcut. (Simply type :set paste
to enable it, paste your content and then type :set nopaste
to disable it.) Alternatively, you can use the pastetoggle
option to set a keycode that toggles the mode (:help pastetoggle
).
I recommend using registers instead of these options, but if they are still too scary, this can be a convenient workaround while you're perfecting your Vim chops.
See :help clipboard
for more detailed information.
sudo apt-get install vim-gnome
which will add that functionality to inbuilt vim of using system's clipboard. – Actvim-gnome
is probably overkill or simply unavailable in their OS/distro, whereas I suspectvim-gtk
and preferablyvim-gtk3
are more likely to exist and pull fewer dependencies, while still providing clipboard integration (at least for those still on X11; I'm not sure how this all interacts with Wayland). – Emersonemeryvim-gnome
package, which seems to be an old GNOME 2 thing. However, on Debian proper,vim-gnome
is just a dummy package that installsvim-gtk3
andvim-gui-common
. My guess is it's the latter that provides the clipboard enhancements here. – Emersonemeryvim
(even from a webpage), but problems arise when you want to copy text fromvim
to another application and you can't simply scroll down and highlight text, whilst using CTRL + SHIFT + C – Opsisgedit
method too, until I have found this solution below. – Oriel