我只知道使用寄存器的一个实例是通过CtrlR*,从中我从剪贴板粘贴文本。

寄存器的其他用途是什么?如何使用它们?

你所知道的关于VI寄存器的一切(让我们关注VI 7.2)——分享给我们。


当前回答

来自vim的帮助页面:

CTRL-R {0-9a-z"%#:-=.}                  *c_CTRL-R* *c_<C-R>*
        Insert the contents of a numbered or named register.  Between
        typing CTRL-R and the second character '"' will be displayed
        <...snip...>
        Special registers:
            '"' the unnamed register, containing the text of
                the last delete or yank
            '%' the current file name
            '#' the alternate file name
            '*' the clipboard contents (X11: primary selection)
            '+' the clipboard contents
            '/' the last search pattern
            ':' the last command-line
            '-' the last small (less than a line) delete
            '.' the last inserted text
                            *c_CTRL-R_=*
            '=' the expression register: you are prompted to
                enter an expression (see |expression|)
                (doesn't work at the expression prompt; some
                things such as changing the buffer or current
                window are not allowed to avoid side effects)
                When the result is a |List| the items are used
                as lines.  They can have line breaks inside
                too.
                When the result is a Float it's automatically
                converted to a String.
        See |registers| about registers.  {not in Vi}
        <...snip...>

其他回答

我使用默认寄存器在我的vim窗口中为文本grep,而不必伸手去拿鼠标。

把文本 :!grep " < CTRL-R > 0 " < CR >

一个很大的混淆来源是默认寄存器”。了解它的工作方式是很重要的。如果在大多数情况下避免使用默认寄存器,情况会好得多。来自Vim文档的解释:

Vim fills this register with text deleted with the "d", "c", "s", "x" commands
or copied with the yank "y" command, regardless of whether or not a specific
register was used (e.g.  "xdd).  This is like the unnamed register is pointing
to the last used register.

所以默认寄存器实际上是指向最后使用的寄存器的指针。当你删除或拉取某个东西时,这个寄存器将指向其他寄存器。您可以通过检查寄存器来验证这一点。总有另一个寄存器与默认寄存器完全相同:yank寄存器(“0”),第一个删除寄存器(“1”),小删除寄存器(“-”)或任何其他用于删除或删除的寄存器。

唯一的例外是黑洞寄存器。Vim doc说:

An exception is the '_' register: "_dd does not store the deleted text in any
register.

通常,直接使用“0”、“-”和“1-”9默认寄存器或命名寄存器会更好。

当我发现0寄存器时,我很高兴。如果你不将文本分配给特定的寄存器,那么它将被分配给0寄存器,并被保存在默认的“寄存器”中。0寄存器和' '寄存器之间的区别是0只填充被删除的文本,而默认寄存器也填充使用d/ d/ x/ x/ c/ c/ s/ s命令删除的文本。

当我想复制一些文本,删除一些东西并用复制的文本替换它时,我发现这很有用。举例如下:

Yank你想要复制的文本与y[动议]-这个文本保存在“和0寄存器 删除你想用d[动议]替换的文本-此文本保存在“寄存器 用“0p .”粘贴被拉拽的文本

其中“是为下一个命令使用寄存器的命令。

在最后一步,如果您要从默认寄存器粘贴(使用p),它将使用您刚刚删除的文本(可能不是您想要的)。

注意p或p是从默认寄存器中粘贴的。传统的等效方法是""p(或""p)和"0保存最后一次yank, "1保存最后一次删除或更改。

有关更多信息,请参阅:帮助寄存器。

在带@的命令中使用寄存器。例如:

echo @a
echo @0
echo @+

让他们来指挥:

let @a = 'abc'

现在ap将粘贴abc。

我最喜欢的特性是能够使用大写字母追加到寄存器中。例如,假设您想要将导入的一个子集从缓冲区X移动到缓冲区Y。

到缓冲区X中的x1行。 输入“ayy”以将寄存器a替换为行x1的内容。 到第x5行。 输入“Ayy(大写A)以在寄存器A的末尾追加行x5。 转到缓冲区Y,输入“ap”进行粘贴

<content of line x1>
<content of line x5>