简体   繁体   中英

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. As described in :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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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