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

时间:2013-08-13 04:00:59

标签: vim

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

e.g。

"=== get date
let TodayDate=system("date")

2 个答案:

答案 0 :(得分:28)

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

:put =TodayDate

: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 =".

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

答案 1 :(得分:1)

以下内容如何?

execute "normal! i" . TodayDate

这将使您进入插入模式,并将TodayDate的内容插入到光标位置。