0

Assume I've copied some text into the clipboard from another program. I'm on MacOS, so in Vim terms, this means the text is in the + and * registers (I have verified this using :registers). I use clipboard=unnamed because it makes it easier to copy text out of Vim. However, let's say I have this buffer:

someFunctionCall(blah, more blah, other blah)
   ^---- cursor position

Let's say I want to change all the text inside the parentheses to the contents of the clipboard (perhaps new blah, more new blah). I can type ci(, but this immediately puts blah, more blah, other blah into the clipboard instead, wiping out what's already in the clipboard.

Is there an elegant, or reasonably elegant, way to do this? I'd ideally like to do this without using plugins.

4
  • 2
    I'm sure this has been asked and answered here...more than once. – B Layer May 10 at 17:39
  • 4
    Simply: vibp. More opinionated: don’t sync the clipboard and the unnamed register :) "*y isn’t too hard to type, and I have it bound as an operator on C-y; plus there’s also :[range]yank *. – D. Ben Knoble May 10 at 19:00
  • I always use clipboard=unnamed in certain environments (e.g. Windows). Major convenience, IMO. What's the downside you have in mind? – B Layer May 11 at 17:02
  • @D.BenKnoble thanks, I think that's the answer I needed. Although I don't agree with the clipboard statement, but clearly that's just personal preference at the end of the day. If you turn this into an answer, I'll accept it. – Andrew Ferrier May 11 at 19:20
2

In this particular case, it’s easier to paste over a visual selection (vibp).

You could also move the clipboard contents to another register (e.g., :let @0 = @") before you do the dib"0p. Since register 0 is for yanks and is wiped out less frequently, I chose that.


My (very opinionated) comment on clipboards and the unnamed register is that, well—it’s unnamed for a reason. It gets manipulated all the time by a bunch of commands. When you want something more tangible/permanent, use a dedicated register. (And, personally, I don’t find the star or plus registers inconvenient.)

5
  • But with clipboard=unnamed nothing different happens to the unnamed register. Yanks, deletes, etc. from within Vim go there either way (clipboard=unnamed makes them also go to *). (And, obviously, copies/cuts external to Vim only go to *...regardless of clipboard setting.) – B Layer May 11 at 23:59
  • The fact that any yank/delete I do in Vim will be available in the clipboard without me having to think about it (if I even know...I might not realize I want that until after the action) is indispensable. (And I realize it's just your opinion...thought you'd like to hear a valid reason for doing otherwise.) – B Layer May 12 at 0:05
  • Thanks for the rationale @BLayer —I find that I don’t want that behavior. Aside from reasons of say, editing confidential data, I find I just don’t want or need every operation to end up on the clipboard. The unnamed register is really an ephemeral, temporary scratch space for me, useful mainly in short editing work. When I want something on the clipboard, I put it there (usually with ease; I mentioned my C-y bind in a comment on the Q, and :%y* is easy enough when I want the full file). – D. Ben Knoble May 12 at 1:28
  • Hmm. We must have fairly different workflows. I find myself pasting from Vim this way all the time! Cheers. – B Layer May 12 at 1:55
  • @D.BenKnoble thanks. I think that's a fair answer. I see clipboard=unnamed as a very reasonable setting, because I see the ephemeral nature of the unnamed register as being equivalent to the ephemeral nature of the clipboard, and because I think having multiple temporary clipboards (which is what registers are, really) is just confusing! But appreciate that's an opinion too. Thanks for vibp - agree I think that's the best 'workaround' for my issue. Now to de-train my fingers from ci(! – Andrew Ferrier May 13 at 9:10

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.