【问题标题】:Vim: Yank selected text into buffer and overwrite that bufferVim:将选定的文本拉入缓冲区并覆盖该缓冲区
【发布时间】:2022-01-12 20:58:27
【问题描述】:

我正在将出现的MyText 拉入缓冲区B

:g/MyText/yank B

但它总是附加到缓冲区而不是覆盖它。一段时间后,B 寄存器包含了我之前提取的所有行。我怎样才能覆盖它而不是附加它?

【问题讨论】:

    标签: regex vim copy vi yank


    【解决方案1】:

    在 vi 中,我们有“编辑缓冲区”,也就是 Vim 现在称为 缓冲区,以及“缓冲区”,Vim 现在称为“寄存器”。这样就更清楚了。

    注意寄存器是b,而不是B。在这种情况下,您使用寄存器的名称 b 将其拉入

    :yank b
    

    你使用大写变体追加到它:

    :yank B
    

    Vim 导师并没有真正提到寄存器,而且参考手册和用户手册都非常清楚地说明了这一切,所以我不确定你在哪里以及如何把它倒退。

    :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.
    

    【讨论】:

    • 拉动所有出现的东西时,这似乎不起作用。 :g/MyText/yank b 只是将最后一次出现在 b 中。
    • 一切都按设计进行:“抽出所有出现的东西”是用:yank B 完成的,因为每个抽出追加 注册b 并注册b 最终结束用你拉进去的每一行。当您执行 :yank b 时,每个 yank 都会覆盖前一个并注册 b 最终会以最后一个被拉出的文本结束。我怀疑你在表达你想要一个假想的中间立场时遇到了麻烦,第一个 yank 覆盖 b 并且随后的 yank 附加到它。如果是这样的话,很抱歉 Vim 无法读懂你的想法。
    • 您必须在 附加序列之前清除寄存器。最简单的方法是在所需寄存器中开始和停止一个空记录:qb 开始在寄存器b 中记录,然后立即q 停止记录,简称qbq,然后继续您的序列追加:qbq:g/pattern/y B。还有其他方法,但这是最简单、最快捷的方法。
    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 2021-03-17
    相关资源
    最近更新 更多