While in an active Vim buffer, how can I write out a specific range of lines to a new file without closing the current buffer first?
4 Answers
You can do
:100,200w filename
Of course 100,200 is the range of lines you want to write.
-
1Are the addresses inclusive or exclusive?Marcin– Marcin2012-07-19 15:34:16 +00:00Commented Jul 19, 2012 at 15:34
-
4Inclusive. Also inclusive if you use patterns: :/^something/,/^else/ w filename, or marks: 'a,'b w filename includes the lines marked 'a' and 'b'.user732– user7322012-07-19 15:46:16 +00:00Commented Jul 19, 2012 at 15:46
-
Hi @Bernhard, what if i want to cut lines from current file.Ravi Sevta– Ravi Sevta2018-03-24 19:20:10 +00:00Commented Mar 24, 2018 at 19:20
-
Is there a notation that means "this line until the end of the file"? Or even: "the cursor's present position to the end of the file"?felwithe– felwithe2023-02-17 16:32:32 +00:00Commented Feb 17, 2023 at 16:32
-
1@felwithe - yes, for example :.,$ w file.txt That is "." for current line and "$" for "end of file". You can swap out line numbers for either "." or "$" as needed.raindog308– raindog3082023-02-28 15:10:04 +00:00Commented Feb 28, 2023 at 15:10
The most general:
Move cursor to first line of the group you want to write. Hit
mandasequentiall. 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. Typewand 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.
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 to use relative line numbers. For example:
:+0,+99w filename
writes the next 100 lines to filename.
-
Won't that overwrite the original file? The OP would like to write out to a different file.SabreWolfy– SabreWolfy2018-06-20 07:59:36 +00:00Commented Jun 20, 2018 at 7:59
-
1@SabreWolfy No; maybe you missed 'filename' after the line numbers. I just tested his version and it worked.felwithe– felwithe2019-10-12 12:40:50 +00:00Commented Oct 12, 2019 at 12:40
-
You could also achieve this if you know how many lines you want to save beginning at the current line: just type blindly - i.e. without getting visual feedback - the number of lines you want to save and then the colon
:- that leads for e.g. 10 (lines) to a prompt like:.,.+9to which you can appendw filenamejust like shown above.Gerd– Gerd2025-02-11 07:25:52 +00:00Commented Feb 11 at 7:25