How to bind a key to issue two commands in visual mode?

1

This question is more general than the problem I am currently faced with, and may not have a solution. But my real problem may have a solution if there is no general solution.

What I want to do is if I use y from visual mode, I want Vim to yank both to the + register and to the regular " register.

I don't care about clobbering the system clipboard, because there are tools I use to record past clipboard entries. I just want a way to yank to clipboard which is easy enough to perform that I may actually make use of it.

Possible approaches

  • bind key to function which somehow hopefully can grab the range of the selection, and then stick it into both registers -- i think this can be done, i have a magic snippet in my vimrc that does this. I have no idea how it works. Looks like this: vnoremap <silent> x :<C-u>execute 'normal! vlF' . nr2char(getchar()) . 'of' . nr2char(getchar())<CR>
  • yank and then call function that explicitly copies " register to + register. Not sure how to do a yank and then call some function. might be easier
  • ???
  • no direct profit likely

Steven Lu

Posted 2014-12-10T05:06:21.057

Reputation: 2 818

Answers

2

set clipboard^=unnamedplus

See :help 'clipboard'.

I don't care about clobbering the system clipboard, because there are tools I use to record past clipboard entries. I just want a way to yank to clipboard which is easy enough to perform that I may actually make use of it.

Clobbering the system clipboard is precisely a rather big issue with synchronizing the unnamed register and the clipboard register when you use a system-wide clipboard manager. It becomes hard to find what you are looking for, which is probably buried below dozens upon dozens of meaningless entries. Think about that.

Another option could be to remap y in visual mode to yank to the system clipboard:

 xnoremap y "+y

Since all yanks are sent to the unnamed register no matter what, the command above will effectively yank the visual selection to the unnamed register and the clipboard register. This is in my opinion a much cleaner solution than using 'clipboard'.

romainl

Posted 2014-12-10T05:06:21.057

Reputation: 19 227

Yeah i wanted to only have visual mode y to yank to system clipboard so that i wont end up with a ton of useless entries. I guess you brought up a really good point at the end there, which is... all yanks always go into " so I really didnt need to even find or write a way to bind two actions to the key. – Steven Lu – 2014-12-10T08:12:11.297

This works well. I didn't mention... I can't actually even type "+y in visual mode, because my " is bound to surround.vim to surround selection with double-quotes. – Steven Lu – 2014-12-10T08:16:19.763