I have a bunch of lines that I want to write out to a different file.
How can I do that?
You can do
Of course 100,200 is the range of lines you want to write.
The most general:
Move cursor to first line of the group you want to write. Hit
asequentiall. That's "set mark named 'a'".
Move cursor to last line of the group, hit 'm' and 'b'.'
Change over to command mode hit: as a sequence do
:'a,'b w filenamethen hit return.
That will work in vi, nvi and vim.
Another method, works in more modern vim:
Put cursor on first line of the group of lines you want to write out. Hit
V, for "start visual block of lines".
Move cursor to the bottom of the group of lines. Vim will highlight with reverse video each line.
Write out. Hit
:to go into command line mode. That will give you a prompt at the bottom of the scren that looks like:
:'<,'>That means from the start of the visual block to the end. Type
wand the filename and hit return.
You can add singled lines to the end of the file as you need to by putting your cursor on the line you want to write out then typing:
:.w >> filename and hitting return.
The "write a group of lines" commands also work with ">> filename" instead of just "filename". The ">>" causes vi or vim to append to the file, rather than just writing it.
To add to Bernhard's answer, you can also select a region using visual mode and then enter
If you know the line numbers that you want to write, you can do it from the command line:
vim -c "100,200w new_file.txt" -c wq original_file.txt
one small trick is using relative line number. For example: :+0,+99w filename write the next 100 lines to filename.