Vim: Yank selected text into buffer and overwrite that buffer

I'm yank-ing occurrences of MyText into a buffer B:

:g/MyText/yank B

But it always appends to the buffer instead of overwriting it. After a while the B register contains all the lines I've yanked previously. How can I overwrite it instead of appending it?


In vi, we had the "edit buffer", which is what Vim now calls buffer, and "buffers", which Vim now calls "registers". This is clearer that way.

Note that the register is b, not B. You use the name of the register, b in this case, to yank into it:

:yank b

and you use the uppercase variant to append to it:

:yank B

The Vim tutor doesn't really mention registers and both the reference manual and the user manual are pretty clear about all that so I'm not sure where and how you got it backward.

:help registers:

4. Named registers "a to "z or "A to "Z         *quote_alpha* *quotea*
Vim fills these registers only when you say so.  Specify them as lowercase
letters to replace their previous contents or as uppercase letters to append
to their previous contents.  When the '>' flag is present in 'cpoptions' then
a line break is inserted before the appended text.

:help 10.1:

APPENDING TO A REGISTER

So far we have used a lowercase letter for the register name.  To append to a
register, use an uppercase letter.
   Suppose you have recorded a command to change a word to register c.  It
works properly, but you would like to add a search for the next word to
change.  This can be done with:

    qC/word<Enter>q

You start with "qC", which records to the c register and appends.  Thus
writing to an uppercase register name means to append to the register with
the same letter, but lowercase.

This works both with recording and with yank and delete commands.  For
example, you want to collect a sequence of lines into the a register.  Yank
the first line with:

    "aY

Now move to the second line, and type:

    "AY

Repeat this command for all lines.  The a register now contains all those
lines, in the order you yanked them.