1

I'm trying to find the right ex command to overwrite the current vim buffer with the system clipboard.

The relevant system clipboard register is "+ in this case.

My first guess was to use :%+p; i.e. select the whole buffer % and then paste p the "+ register to the current buffer. But this doesn't work.

The other way around, yanking the current buffer into the system clipboard is accomplished by :%y+; % select the whole buffer; y copy; + to the system clipboard register.

  • Based on the answer of @statox , youve got the put syntax wrong. Register comes after. – D. Ben Knoble Jun 18 '18 at 12:43
  • @D.BenKnoble Nope, the put syntax isn't right if it's put afterwards either. Both syntaxes are wrong. That is, if you mean :%p+, by the registers come after; it results in an error: E488: Trailing characters. – Mussé Redi Jun 18 '18 at 13:23
  • Darn. I’ll do some reading later this evening. But statox has the right idea (two commands, delete then put) – D. Ben Knoble Jun 18 '18 at 13:24
  • @D.BenKnoble Alright! Well, :%d | put + doesn't work either, obviously since it's the same command. But, I'll be digging into this too; two commands seem like the right amount. :) – Mussé Redi Jun 18 '18 at 13:26
  • @MusséRedi p+ doesn't work because p is the print command, :pu is the shortest form of put (:%pu+ is valid but doesn't quite do what you want). – Mass Jun 18 '18 at 23:28
1

You could add the following in your vimrc:

command! ReplaceWithClipboard %d | put +

This way when you call the command :ReplaceWithClipboard vim will

  • %d delete all the lines in the buffer
  • put + put your clipboard register

You can have a look at

  • This does not work. Even when I tried populating the system clipboard from within vim itself. The message in the header below just reads --No lines in buffer--. – Mussé Redi Jun 18 '18 at 13:17
  • 1
    @MusséRedi That's weird it works fine on my setup, maybe try to start Vim without configuration vim -u NONE – statox Jun 18 '18 at 13:26
  • Well, it seems that you're right; it works when starting vim without configuration. What kind of configuration settings could be interfering with these two simple commands? – Mussé Redi Jun 18 '18 at 13:36
  • @MusséRedi Take a look at our famous question How do I debug my vimrc to find out ;) – statox Jun 18 '18 at 14:17
  • might want to use sil %d _ to hide the "no lines in buffer" message and to prevent that the deleted stuff ends up in the clipboard (:h clipboard-unnamed). Might as well need 0put +|$d _ to remove the trailing new-line that will be added (without the 0put it will appear in line 1) – Christian Brabandt Jun 18 '18 at 14:30
0

By visual selection, one can (1) select the whole buffer by moving the start of the buffer, with the motion gg; (2) pressing v, to transfer to visual mode; (3) executing the motion GG, to select the whole buffer; (4) accessing the system clipboard register +, by pressing "+; and finally, (5) pasting the register into the current buffer, by pressing p. :)

I still don't know how to do this in the form of a self-contained ex-command though, in which I'm interested.

  • is :0norm!VG"+p cheating? – Mass Jun 18 '18 at 23:36
  • @Mass Could you elaborate? – Mussé Redi Jun 19 '18 at 10:48

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.