LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-23-2004, 11:14 AM   #1
p0rt
LQ Newbie
 
Registered: Jun 2003
Posts: 28

Rep: Reputation: 15
copying blocks of text using vim


Hi,

I am trying to copy certain line numbers to a file. I have done :set nu and then would normally, for just one block, do:
:1210,1410w filename
But how do i copy multiple line ranges to just one file?

ie

1210,1410
2000,2300
3000,3200

all to one file...

Any Help much appreciated

Cheers
 
Old 09-23-2004, 01:10 PM   #2
prell
Member
 
Registered: Jul 2004
Posts: 73

Rep: Reputation: 15
In vim, assuming your file is open, do the following:
[list=1][*]Hit ":"[*]Enter "1000,1450 y" (substitute the line numbers you want, the first one being the first and the second on being the last)[*]Hit ":"[*]Enter "sp lines_wanted.txt" (or whatever filename you want)[*]Hit "p"[/list=1]

Repeat, substituting the last step with "$ p" to append to the file.

And that's it! You could do it in ex (what vi is built on) as well. You could also do it in vim in visual mode, by hitting "V" and selecting the lines you want. When you have them selected, just hit "y" and put (by hitting "p") it in the new file. Tip: switch windows by hitting ^w followed by a direction (j, k, h, l, or one of the arrow keys).

If you wanted to retain the line numbers, I'd recommend you run "cat -n" on your file and redirect the output to a new file, and then follow the steps above for the new file. I don't know if vim can yank line numbers as well. It just might!

Edit: I notice that you might want a fancier way of doing this, and I messed around a bit and didn't come up with a perfect solution yet.

Last edited by prell; 09-23-2004 at 01:26 PM.
 
Old 09-23-2004, 03:05 PM   #3
p0rt
LQ Newbie
 
Registered: Jun 2003
Posts: 28

Original Poster
Rep: Reputation: 15
Thanks very much for your reply, it was very helpful..

Cheers,
Chris
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
VI : Copying Selected Text Chaitanyayardi Linux - Software 1 11-10-2005 07:40 AM
copying text from terminal Linuxuser_81 Linux - Newbie 4 09-30-2005 01:12 AM
Text Reflow in Vim k4zau Linux - Software 4 02-29-2004 10:32 AM
Script file to replace large text blocks in files? stodge Linux - Software 0 09-27-2003 10:53 AM
Copying Text albean Linux - Newbie 2 10-03-2002 03:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration