12

I wanted to copy some code I have from Neovim to Firefox, but the stuff I yanked doesn't carry over outside Neovim.

After I checked :checkhealth, this what it said:

:checkhealth output

What should I do to handle this issue?

muru
  • 207,970
Vegeto.T
  • 135

4 Answers4

25

In order to be able to paste text copied from Neovim to any app, you have to copy the text to the system clipboard. You can do that as follows:

  1. Install a clipboard tool, because, as indicated in your screenshot, you don't have one installed.

    • If you are on an Xorg session, you may install either xsel or xclip by running:

      sudo apt install xsel
      

      or:

      sudo apt install xclip
      
    • If you are on a Wayland session, you may install wl-clipboard by running:

      sudo apt install wl-clipboard
      
  2. Select the text you wish to copy in Neovim.

  3. Press the following keys one at a time: "+y

    This copies (y) the selected text to the selection register ("+).

  4. Paste the selected text to the app you wish to using the usual paste shortcut Ctrl+V.

To make things easier, you can also have the text you copy in Neovim always copied to the system clipboard by adding the following in your ~/.config/nvim/init.vim file (create the file if it doesn't exist or go to the correct file location if you use a custom Neovim setup):

set clipboard=unnamedplus

If you are using the Lua API for configuring Neovim, you can achieve the same using the command in kkvamshee's answer.

  • this worked, thank you so much. – Vegeto.T Sep 24 '23 at 20:15
  • Thank you so much! This morning, I upgraded to Ubuntu 24.04 and to my greatest frustration I realized that Ctrl-C and Ctrl-Shift-C don't work anymore in WSL2 to copy from a nvim window to another termina. The "+y does work but the set clipboard=unnamedplus doesn't seem to. This is remarkable: in 2024, that application requires an obscure and unique key combination for something as necessary as copying text! Why was Ctrl-Shift-C working for copying text out of nvim before upgrading to Ubuntu 24.04? How can I get back to using Ctrl-C or Ctrl-Shift-C? – Come Raczy Apr 26 '24 at 16:12
  • 1
    Thanks for mentioning the alternative for Wayland! I like your hollistic inclusive approach :) – edison23 May 22 '25 at 09:31
  • thank to specify to install one (xclip in my case) but default set clipboard=unnamedplus doesn't work in my case (Ubuntu 24.04 with X11) E488: Trailing characters: et clipboard=: #let clipboard= … my solution : https://askubuntu.com/a/1555422/385361 – bcag2 Sep 03 '25 at 13:01
2

You can also achieve this by adding following line to your init.lua file, if you are using the Lua API to configure Neovim:

vim.api.nvim_set_option("clipboard", "unnamed")
  • 1
    For anyone looking for the bidirectional flow (system to Vim, Vim to system), use 'unnamedplus' argument. (e.g., vim.api.nvim_set_option("clipboard", "unnamedplus")). – Roman Mahotskyi Apr 30 '24 at 17:09
  • 1
    vim.api.nvim_set_option works, but is now being marked as "deprecated". What would be an alternative way to setup my init.lua file? – Caio Castro Jul 02 '24 at 00:32
  • doesn't work, still gives me no clipboard available error – Tony Jul 07 '24 at 13:51
1

I just tested my nvim and my Copy/Paste/Mouse related config options from (~/.config/nvim/init.vim) are:

    set mouse=nv                 " middle-click paste with 
    set hlsearch                " highlight search 
    set clipboard+=unnamedplus   " using system clipboard
    # with these plugins
    call plug#begin("~/.vim/plugged")
    " Plugin Section
    Plug 'dracula/vim'
    Plug 'ryanoasis/vim-devicons'
    " Plug 'SirVer/ultisnips'
    Plug 'honza/vim-snippets'
    Plug 'scrooloose/nerdtree'
    Plug 'preservim/nerdcommenter'
    Plug 'mhinz/vim-startify'
    Plug 'neoclide/coc.nvim', {'branch': 'release'}
    call plug#end()

Now Copy works by double-clicking or clicking+draging the mouse to highlight your text selection, right-click then brings up a mini-menu with Copy, Paste, Etc. Select Copy.

Now all of 1) middle-clicking, 2) right-clicking for the mini-menu again to select Paste & 3) the old staple option Ctrl+v will perform the Paste both inside another xfce4-terminal using nvim on a text file or using Firefox on Ask Ubuntu to do this.

And Yeah, I saw the above post, I have xsel 1.2.1-1 installed. If you need my entire nvim/init.vim config file just let me know. Cheers, Jono

jono
  • 11
1

I have stumbled upon the same issue and BeastOfCaerbannog's answer is what does the trick.

In Wayland I have the following keybinding in init.lua:

-- Copy/paste from system clipboard
vim.keymap.set({ 'n', 'x' }, 'cp', '"+y')
vim.keymap.set({ 'n', 'x' }, 'cv', '"+p')
-- Delete without changing the registers
vim.keymap.set({ 'n', 'x' }, 'x', '"_x')

This works by using visual mode to select the relevant block of code and using the cp keybing to copy it to system clipboard.

For pasting you need to be in normal mode, and press cv

This works for both X11 and Wayland, the respective clipboard managers just need to be installed (wl-clipboard in my case).

iosi
  • 36