I can do it with marks for exemple
:'a,'b w! /tmp/myFile
But what is the syntaxt with yanked lines ?
Thanks
You can call vim functions in command mode.
The command below will write yanked lines to /tmp/myFile
.
:call writefile(split(getreg('"'), '\n'), '/tmp/myFile')
Note: Yanked lines are in unnamed register (""
, type :help registers
for help).
Why not do it visually. Just 3 basic commands everyone can understand:
:tabe
-- open new tabp
-- paste to buffer:w /tmp/myFile.txt
-- save fileYou could use :redir
(using register r
):
:redir! > /tmp/myFile | silent echon @r | redir END
If you wanted to append you could use redir >> /tmp/myFile
(note: no !
here, as "overwrite if exists" wouldn't make sense).
The other answers use special commands like :redir
or writefile()
to write to a new file. The most direct way is through opening a buffer for the file. This way, you can use the ordinary p
/ :put
. As a bonus, the file stays in the buffer list, so it's easy to recall / edit later.
:split /tmp/myFile | put! | write | bdelete
or shortened:
:sp /tmp/myFile|pu!|w|bd
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With