0

Is there a way to have y/d/c/x/s output into multiple registers at the same time - for example, "a as well as the system clipboard "+? I can do "aY"+Y obviously, and with Y it's alright (only one extra keystroke), but with d/x it wouldn't delete the same text, or if I were doing a complex motion like "ad?hello <CR> I wouldn't want to have to type that all over again. Can I make it so that something like "a"+Y does the same?

I'm using Neovim v0.4.4.

6
  • Welcome to Vi and Vim! I don't see how "ax"+x would work, since each x would delete a different character of the buffer, no? You can set a register explicitly with :let @+ = @a (better is :call setreg(...)) and there's a TextYankPost event you can use where you could use to mirror named registers to the clipboard perhaps?
    – filbranden
    Dec 26 '20 at 12:03
  • 1
    @filbranden doh! I originally had Y instead of x but switched because I thought it was simpler, without realising that wouldn't work. I'll change it
    – pxeger
    Dec 26 '20 at 12:18
  • 1
    I think you might want to explain why you need to yank into multiple registers... Is it a named register + the clipboard register always? Would the trick with copying to the clipboard work?
    – filbranden
    Dec 26 '20 at 12:54
  • I want to make all my deletes go to register a by default, if I don't specify a register, and if I do, then go to a and whichever I specified.
    – pxeger
    Dec 26 '20 at 14:15
  • "I want to make all my deletes ... go to a and whichever I specified." Yeah, but why? I still don't get it... The register rules are quite complex, but in a way something like this already happens (with deletes, in specific line/multi-line deletes.) For example, "zdd goes to both "z and to numbered register "1 (and other numbered registers beyond 0 are shifted down one.)
    – filbranden
    Dec 26 '20 at 15:23
1

I haven't found some elegant way to do so bu you can create your own way to do so.

Code

  1. First you can create somme key bindings (work on visual mode only).
xnoremap <Leader>y y:MultiRegister<Space> 
xnoremap <Leader>d d:MultiRegister<Space> 
xnoremap <Leader>c c:MultiRegister<Space> 
xnoremap <Leader>x x:MultiRegister<Space> 
xnoremap <Leader>s s:MultiRegister 
  1. Secondly you can create your own custom command that will take the register name you gave:
command -nargs=+ MultiRegister call CopyToSelectedRegisters('<args>')

  1. Finally you create a function that copy the selected area to the rest of the given registers:
function CopyToSelectedRegisters(SelectedRegisters)
    "Split the given registers by space
    let tab= split(a:SelectedRegisters)
    "for each register
    for register in tab 
        echo "let @".register."= @\""
        "Copy the content of the default register
        execute "let @".register."= @\""
    endfor
endfunction

Usage

  1. In visual mode, select the area you want and press <leader> followed by y (or d,c,x,s).
  2. You will be prompted in the command mode (bellow) with the MultiRegister command followed by a white space.
  3. Enter the registers you want (for instance a b c +) and press enter.
  4. Vim will do the copy to the designated registers (you can check it with :reg).

I hope this will help you

2
  • Welcome to Vi and Vim! You might want to mention that your maps have a trailing space, or use <space> to avoid that. You may also want to use getreg/setreg or the info functions
    – D. Ben Knoble
    Dec 26 '20 at 22:58
  • @D.Ben Knoble thank you for your response! That's true, it's better to add a <space> in my maps. Also I didn't know about the getreg function. Normaly I prefer using a notation as close as possible to the commands we can make on the interface, but this will be really useful, thanks Dec 27 '20 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.