Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim : how to write to another file my lines yanked?

Tags:

vim

I can do it with marks for exemple

:'a,'b w! /tmp/myFile

But what is the syntaxt with yanked lines ?

Thanks

like image 538
Minstrel Avatar asked Jan 14 '16 09:01

Minstrel


3 Answers

Hard Way

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).


Easy Way

Why not do it visually. Just 3 basic commands everyone can understand:

  • :tabe -- open new tab
  • p -- paste to buffer
  • :w /tmp/myFile.txt -- save file
like image 166
kev Avatar answered Oct 23 '22 02:10

kev


You 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).

like image 39
Marth Avatar answered Oct 23 '22 03:10

Marth


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
like image 41
Ingo Karkat Avatar answered Oct 23 '22 01:10

Ingo Karkat