Vim: 如何将变量的内容追加到文件中?

Vim: how to append the content of a variable to a file?

我想知道:是否可以使用命令将变量的内容(在我的例子中是最后一次搜索)写入文件

我尝试了以下方法:

:echo @/ >> /tmp/foo.txt
:@/w /tmp/foo.txt

但这没有用。知道执行此操作的正确方法是什么吗?

romain 提议的另一种解决方案是使用 redir 命令将消息重定向到文件。如 :help redir

中所述

The messages which are the output of commands are written to that file, until redirection ends.

要追加搜索寄存器的内容,运行 以下命令序列:

redir >> /tmp/foo.txt
echo @/
redir END

这个序列可以变成一个函数and/or用作键映射。