如何在vim脚本中将变量值写入当前编辑文件

son*_*hir 16 vim

我在vim的脚本中得到一个变量的值,以及如何将它写入我正在编辑的文件中.

例如

"=== get date
let TodayDate=system("date")
Run Code Online (Sandbox Code Playgroud)

FDi*_*off 29

您可以使用:put将变量(或表达式)的内容放入当前缓冲区

:put =TodayDate
Run Code Online (Sandbox Code Playgroud)

帮助 :h :put

                                                        :pu :put
:[line]pu[t] [x]        Put the text [from register x] after [line] (default
                        current line).  This always works linewise, thus
                        this command can be used to put a yanked block as new
                        lines.
                        The cursor is left on the first non-blank in the last
                        new line.
                        The register can also be '=' followed by an optional
                        expression.  The expression continues until the end of
                        the command.  You need to escape the '|' and '"'
                        characters to prevent them from terminating the
                        command.  Example: 
                                :put ='path' . \",/test\"
                        If there is no expression after '=', Vim uses the
                        previous expression.  You can see it with ":dis =".
Run Code Online (Sandbox Code Playgroud)

对于映射和编辑<C-R>=可能更好,:put因为它允许您使用表达式寄存器并输出光标位置的内容.(看一看:h <C-R>)

  • 要将 vim 选项值放入文本,您需要在选项前添加 `&amp;`。请参阅相关帖子 [此处](/sf/ask/1652307191/)。 (3认同)