vimneovimwindows-terminal

How to copy on mouse selection in Neovim?


I use Neovim under WSL, and I set copy on selection in WindowsTerminal. However, with :set mouse=a in Neovim, copy on selection by mouse doesn't work in nvim.

How can I make this work in Neovim?


Solution

  • By using :set mouse=a you hand control of the mouse over to Neovim. This means your terminal never gets to see the mouse events and its settings don't matter.

    You could hand the mouse back to the underlying terminal with :set mouse= but that means you won't be able to use the mouse beyond the terminal's capabilities. Specifically, you won't be able to create mappings for mouse clicks.

    You can put Neovim in charge of the mouse for some modes only with e.g. :set mouse=nic as commented by romainl. See :help 'mouse' for an explanation of the flags.

    An alternative workaround would be to use the mappings suggested in Neovim's FAQ to automatically yank to clipboard when doing a mouse selection:

    :vnoremap <LeftRelease> "*ygv
    :vnoremap <2-LeftRelease> "*ygv
    

    As the question is tagged both Vim and Neovim and for sake of completeness: The Vim solution would be to :set clipboard+=autoselect as explained in :help clipboard-autoselect.