10

Currently, I have two similar methods to replace the entire buffer with some other text (say in clipboard).

Method #1:

  1. delete all lines: :%d.

  2. paste text: "+P ( or similar: :put+).

Method #2:

  1. visually select all lines: ggVG.

  2. paste text: "+P.

However I couldn't seem to find an one-command-fire way to do that. The most close one I found is :change command. But it doesn't seem to work well with registers. Of course I can create some function and/or map a key to do that. But before I could make all these customizations, I want to make sure:

Is there some built-in command to replace a range of lines with some other text? Like, you could :%replace+ to replace the entire buffer with text in clipboard?

  • 2
    I don't know of anything off the top of my head, but if you do this a lot, why not create a mapping/command? – Matt Boehm Feb 19 '15 at 16:55
  • 5
    Hmm, 1,$s/\_./\=@+ is not a solution :P, :%d|put+ is closer, but not quite there. – Runium Feb 19 '15 at 17:21
  • 1
    I forgot an asterisk after the dot ... 1,$s/\_.*/\=@+, where \_. matches any character including newline. – Runium Feb 19 '15 at 17:32
  • 2
    Do you have to reuse the buffer, or would it be acceptable to start a new buffer? – 200_success Feb 19 '15 at 21:54
  • 3
    I think Sukminder's :%d|put+ fits the bill. It's short and memorable. The other command is just too arcane to remember and type correctly. – Lie Ryan Feb 19 '15 at 22:43
2

You could do ggvG<CTRL-G> to visually select the whole buffer and enter Select-mode. This is the easiest way to replace the buffer with text you're planning on typing out. You may even be able to make a Select-mode mapping with :smap that solves your problem.

0

One way to replace the whole buffer with your clipboard can be via external command, for example:

:%!pbpaste
:%!echo my_text

See also: Mac OS X clipboard sharing

Or by using substitution:

:%s/\_.*/\=@+/
:%s/\_.*/my text/

Alternatively use :%d|put+ (as already suggested in the comments).

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.