How to use system clipboard in Vim

I know this has been asked before:
How to copy to clipboard in Vim? - Stack Overflow (13 years ago)
How to install vim with clipboard support on Fedora? - Super User (13 years ago)
Yanking from vim to Qt applications on Gnome (5 years ago)

But most questions were asked over 10 years ago and only 6 results come up in the Fedora Discussion search when entering “vim clipboard.” The more recent Fedora Discussion post linked above is a problem pasting into a specific application, not using the clipboard with Vim in general.

I find Fedora to be the best “plug-and-play” distro available, so when I stumbled on this basic system configuration problem and found no official documentation for it, I recognized this as an opportunity to fix that.

I’m asking this question here and now to document an up-to-date, Fedora-specific official solution.

How do you use the system clipboard when yanking and putting in Vim?

I know you can use Ctrl+Shift+v and even right-click, but I’m asking about interacting with your clipboard through the y and p commands.

The solution provided in the first link is still valid. The basics of vim havent changed much :slight_smile:

PS: Since vim isnt a Fedora specific tool, this isnt a Fedora specific issue and so it isnt documented in Fedora documentation.

1 Like

Note that vim specific clipboard commands use the vim clipboard and not the system/desktop clipboard.

Commands within vim such as yy to copy a line and p to paste that line work extremely well within vim, but the data is not available to paste anywhere else. If you wish to copy from vim and paste data to another location (or vice versa) you must use the desktop clipboard with the mouse or the Ctrl+Shift+v type commands as you noted to put the data into or paste from the desktop clipboard.

I am sure this is part of the reason so few people use vim any more – though I prefer vim over nano.

That makes sense, but it is relative to Fedora’s packages.

If we’re on the same page, the solution in the first link is to download gvim. On some systems (like Arch), gvim includes the GUI version and replaces the terminal version with one compiled with GTK support.

For Fedora, that does not seem to be the case. gvim maps to vim-X11, which is just the GUI version.

How can I get clipboard support in the CLI version, without compiling it myself?

vim has extensive help so you can just type in

:help clipboard

and get

There is a special register for storing this selection, it is the "*
register.  Nothing is put in here unless the information about what text is
selected is about to change (e.g. with a left mouse click somewhere), or when
another application wants to paste the selected text.  Then the text is put
in the "* register.  For example, to cut a line and make it the current
selection/put it on the clipboard:


and a bit more …

Also, you can copy-paste using the select buffer. To do that, you select the text to copy, and then move the cursor to where you want to paste and hit the mouse middle button. That works almost everywhere.

I don’t have any problems with vim and the clipboard.
The OP was asking about vim commands to copy & paste (yy and p) and how they related to the desktop clipboard. IME they do not cross over. Maybe with the gui version but not with the cli version of vim.

Using the mouse involves the desktop clipboard, not the internal vim clipboard.

You are right. You need vimx for clipboard support.

vek@newbox ~]$ vimx --version |grep clipboard
+clipboard         +keymap            +printer           +vertsplit
+ex_extra          +mouse_netterm     +syntax            +xterm_clipboard
[vek@newbox ~]$ vim --version |grep clipboard

As far as I know, you cannot. The system clipboards are the X or Wayland clipboards. There isn’t such a thing if you’re not running either. So you must use vim that is compiled with support for X or Wayland.

If you want to use this in the terminal, you can use vimx. man vim will tell you more about the different “flavours of vim”. Here’s what I have in my bashrc, also not specific to vim:

 # Vim with X support
 alias vim=vimx

so when I run vim in my terminal, I’m actually using vimx, which has clipboard support.

Edit: Villy beat me to this bit :slight_smile:

… and to make things more interesting:

  • vimx is a symbolic link to gvim
  • vimx starts in cli mode (not X11 or graphical)
  • gvim starts in graphical mode
  • gvim uses gtk-3

Consequently, vimx uses gtk-3, not X11 directly; on Wayland gtk uses that backend (unless overriden).

1 Like