简体   繁体   English

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

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

I was wondering: is it possible to write the content of a variable (in my case, the last search) to a file with a command ? 我想知道:是否有可能写一个变量的内容(在我的情况下,最后搜索) 与命令文件?

I tried the following: 我尝试了以下方法:

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

But that didn't work. 但这没有用。 Any idea on what is the correct way to do this? 知道什么是正确的方法吗?

An alternative solution to romain's proposal is to use the redir command which redirects messages to a file. romain提议的替代解决方案是使用redir命令,该命令将消息重定向到文件。 As described in :help redir , :help redir

The messages which are the output of commands are written to that file, until redirection ends. 作为命令输出的消息将写入该文件,直到重定向结束。

To append the contents of the search register, run the following sequence of commands: 要附加搜索寄存器的内容,请运行以下命令序列:

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

This sequence could be turned into a function and/or used as a key mapping. 该序列可以转换为功能和/或用作键映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM