Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy from Vim to system clipboard using wayland and without compiled vim `+clipboard` feature flag

I cannot copy into the + or * register.

:echo has('clipboard') from within Vim returns 0 meaning I don't have that feature flag, I don't want to recompile.

I'm running wayland so I cannot use X11 based solutions

like image 666
brother-bilo Avatar asked Apr 23 '20 04:04

brother-bilo


People also ask

How do I copy and paste from clipboard in vim?

Press v to select characters, or uppercase V to select whole lines, or Ctrl-v to select rectangular blocks (use Ctrl-q if Ctrl-v is mapped to paste). Move the cursor to the end of what you want to cut. Press d to cut (or y to copy). Move to where you would like to paste.

How can I copy text to the system clipboard from vim?

In vim command mode press v , this will switch you to VISUAL mode. Move the cursor around to select the text or lines you need to copy. Press y , this will copy the selected text to clipboard. Go to any external application and CMD + v to paste.

How do I copy and paste outside of vim?

To copy text from outside applications into Vim editor, first copy the text using the usual Ctrl-C command then go to Vim editor and type "+p in Normal Mode. I find the above commands very tedious to type every time I copy-paste from outside Vim, so I mapped the Ctrl-y to copy and the Ctrl-p to paste in Vim.

How do I copy text from vi editor to clipboard?

You can use a movement command or up, down, right, and left arrow keys. Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.


1 Answers

I had trouble finding resources so here is what ended up working by adding in ~/.vimrc.

nnoremap <C-@> :call system("wl-copy", @")<CR>

wl-copy is a Command-line copy/paste utilities for Wayland and it will copy the piped content you give it to system clipboard.

What mapping above achieves is

  1. Activate the mapping with Ctrl + @. or choose any convenient key combo
    • nnoremap <C-@>
  2. take the contents of the " register,

    • denoted by the @" argument
  3. and pipe contents of @" as an argument to the system wl-copy function

    • shown by :call system("wl-copy", @").

Alternatively

Assuming you only want to copy line sections of the file, do shift+v to go into visual mode and only highlight the lines I want to copy. Then do.

:'<,'>w !wl-copy

where

  • '<,'> - means you used visual mode to select a range (you don't type this)
  • w !{cmd} - write the range to the stdin of cmd, see more at :help w_c

You can map that with

xnoremap <silent> <C-@> :w !wl-copy<CR><CR>
  • xnoremap: mapping will work in visual mode only
  • <silent>: mapping which will not be echoed on the command line
  • <C-@>: desired key combination
  • :w !{cmd}: write the range to the stdin of cmd
  • <CR><CR>: two enters are needed otherwise command line waits for another command
like image 175
brother-bilo Avatar answered Oct 07 '22 18:10

brother-bilo