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

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

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


当前回答

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

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

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

或者只是一些特别的东西

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

其他回答

一个很酷的技巧是使用“1p粘贴最后删除/更改(,然后使用。重复粘贴后续删除的内容。换句话说,“1p…基本上相当于"1p"2p"3p"4p。

你可以用它来倒序一些行: dddddddddd“1 p…

黑洞寄存器_是寄存器中的/dev/null。

我在我的vimrc中使用它来允许删除单个字符而不更新默认寄存器:

noremap x "_x

并且在不更新默认寄存器的情况下以可视模式粘贴:

vnoremap p "_dP

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

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

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

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

echo @a
echo @0
echo @+

让他们来指挥:

let @a = 'abc'

现在ap将粘贴abc。