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

如何在vim脚本中将变量值写入当前编辑文件,vim,Vim,我在vim的脚本中得到一个变量的值,以及如何将其写入我正在编辑的文件中 e、 g 您可以使用:put将变量(或表达式)的内容放入当前缓冲区 :put =TodayDate 的帮助:h:put :pu :put :[line]pu[t] [x] Put the text [from register x] after [line] (default

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

e、 g


您可以使用
: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 =".


对于映射和编辑,
=
可能优于
:put
,因为它允许您使用表达式寄存器并在光标位置输出内容。(看一下
:h

下面的内容怎么样

execute "normal! i" . TodayDate

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

非常好!我读过LearnVI&vim编辑器,但没有找到这个!非常感谢。“效果很好。”弗迪诺夫我一定是在投票时投了反对票。除非你修改你的答案,否则我不能改变我的投票。如果你编辑,我会把它改为upvote。顺便说一下,谢谢!将文本置于光标位置的任何方法?要将vim选项值置于文本,您需要在选项之前添加
&
。请参阅相关帖子。要清楚,如果映射,建议将变量(TodayDate)保存到寄存器,然后保存到
:将
放入寄存器。是吗?+1。如果textwidth不为零(即inf),则下一步映射保存更改并最终重置它。如果字符串(TodayDate)传递tw字符,则不会出现问题<代码>nnoremap zzz \:让tw_ztc=&tw \:设置tw=0 \:执行“正常!”!哦。str_zzz_01 \:执行“正常!”!哦。str_zzz_02 \:set tw=&tw_ztc
这种方式比公认的方式更消耗计算机,但它保持自动识别。
execute "normal! i" . TodayDate