我只知道使用寄存器的一个实例是通过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宏,您可以编辑它们,因为它们只是记录宏时使用的击键列表。因此,您可以将宏写入文本文件(使用“ap”来编写宏a)并编辑它们,并使用“ay$”将它们加载到寄存器中。存储有用宏的好方法。

来自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文档的解释:

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默认寄存器或命名寄存器会更好。

如果你想在ex-mode命令中粘贴寄存器的内容,点击<C-r><寄存器字母>。

你为什么要用这个?我想对一个较长的字符串进行搜索和替换,所以我在可视模式下选择了它,开始输入搜索/替换表达式:%s/[PASTE YANKED PHRASE]//g,然后继续我的一天。

如果你只想在ex模式下粘贴一个单词,可以在进入ex模式前确保光标在它上面,然后在ex模式下点击<C-r><C-w>来粘贴单词。

为了更方便:

cnoremap <c-g> <c-r>"
cnoremap <C-Right> <c-r><c-w>

我最喜欢的寄存器是':'寄存器。在正常模式下运行@:可以重复之前运行的ex命令。

所以我们可以用MY_commandXXXXX来验证一些命令,然后把MY_commandXXXXX放在vimrc中