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

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

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


当前回答

一个被忽视的寄存器是'。'点寄存器,它包含最后插入的文本,无论它是如何插入的,例如ct](更改直到])。然后你意识到你需要将它插入到其他地方,但不能使用点重复方法。

 :reg .
 :%s/fred/<C-R>./

其他回答

来自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...>

当我发现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保存最后一次删除或更改。

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

如果你想在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>

我认为秘密的大师寄存器是表达式= register。它可以用于创建映射。

:inoremap  \d The current date <c-r>=system("date")<cr>

您可以将它与您的系统结合使用,或者从自定义VimL函数等获得响应。

或者只是一些特别的东西

<c-r>=35+7<cr>

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

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