Visit Our UNIX and Linux User Community


Copy selected lines in vim


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copy selected lines in vim
# 1  
Old 09-12-2009
Copy selected lines in vim

Hi,

I am looking to copy selected lines from a file using the vim editor. I have looked up a few resources and they have suggested to use this-
  1. Type mk
  2. Type: "ay'k (double quotes, <register name from a-z>, <y-yank single quote, k
  3. You can paste those lines wherever you want with "ap
I tried this and it works great with in the same file but when I try to copy it in another file it says no lines in buffer.

It would be great if you explain this to me as to why this does not work. Also, please let me know what are the other ways to do this. I generally highlight the code and paste using p. But this time I have about 1650 lines to do so.

I am on an AIX box [ Version 5.3]. Any suggestions or links to resources would be greatly appreciated.

Thanks,
Coolavi
# 2  
Old 09-12-2009
HI coolavi,

You can go in this way.....


"a10yy (copy 10lines into buffer a)

:e another_file (switch to file another_file)

12G (move to line 12)
"ap ( put the 10 lines after 12 lines)

The only precaution you need to take here is that,on no account,should you exit vi to open a second file .The buffer contents are lost when you leave vi.


so do the above thing without quiting vi.(enjoy)


regards,
Sanjay
# 3  
Old 09-12-2009
Hey, Thanks Sanjay.

Dude! you were quick to pounce on this.

It seems to be working but please explain me one thing. When we use the :e file_name option in vim, does it not affect the original program ?? Does it do a :q on the original program ?

I did that and it seemed that it exited out of the original program. Please explain when you get a chance.

Thanks,
Coolavi
# 4  
Old 09-12-2009
I am just making it very clear.
you can edit multiple files without leaving the editor.While editing one file ,you can easily switch to another by using :e new_file
now you are under the file new_file and just do the editing upon it ,
if after all you want again to go back to the original file which you opened at the begining the siple use :e# (which means toggle between current and previous file.

and finally you can use :wq! or( esc then ZZ)


hope you can make use of it
# 5  
Old 09-12-2009
Yes, I am making use of the new concept I have learnt from you.

Thanks again Sanjay!
# 6  
Old 09-16-2009
Here is a way similar to Sanjay's method, but one that others may find easier...

TO COPY AND PASTE (in vi or vim)
Code:
While editing a file in vi,
1)  Go to the first line you want to copy and press
     ma                                   (meaning marker a)

2)  Go to last line you wish to copy and press
     mb                                   (meaning marker b)

3)  Now you will copy (or yank) the range 'a,'b  to buffer z by pressing
     :'a,'b y z

4)  Go to the line where you wish to paste what's in buffer z
     To paste below the current line press
     "zp
     To paste above the current line press
     "zP

TO CUT AND PASTE (in vi or vim)
Code:
While editing a file...
1)  Go to the first line you want to cut and press
     ma                                   (meaning marker a)

2)  Go to last line you wish to cut and press
     mb                                   (meaning marker b)

3)  Now you will cut (or delete) the range 'a,'b  into buffer z by pressing
     :'a,'b d z

4)  Go to the line where you wish to paste what's in buffer z
     To paste below the current line press
     "zp
     To paste above the current line press
     "zP

# 7  
Old 02-24-2010
MySQL

For copying n number of files from one file to another file
the following steps will we use.

In select Mode

Step1. in escape mode place your cursor in starting of the line
[ where you want ]

Step2. press Shift V then using "j" or "k" select the lines.

Step3. press 'y' for copy the selected lines.

Step4. if you want to past the copied content in same file then place your cursor where you want. then press "p" for paste the content.

if you want to paste your selected content in new file of another file.

in command use "e" then give the argument file name.
then the file is opened in same editor.

then press "p" The copied file paste in to another file.

if you want to toggle between two files press Ctrl+^

In escape mode:

Place your cursor in required line then press how many number of line you want to copy from the current line then press "y"

then given n number of line will be copied.

then see the Setp4.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #120
Difficulty: Easy
The UNIX certification mark is commercially licensed by AT&T.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selected matching lines

two files: one with the line number only, and the 2nd one with line number and content, as following: line_file.txt 1 3 5 9 23 30 content_file.txt 1|we are the world|good|great 2|easily do this by highlighting you|easily do this by highlighting you|easily do this by highlighting... (2 Replies)
Discussion started by: dtdt
2 Replies

2. Shell Programming and Scripting

Delete selected lines

hi Gurus, I have a source file with more than 10 columns ( not fixed ) I want to delete all the lines on the following condition 1) where i have first column as "UPDATE PLAN ADD RATE SCHEDULE" and fourth column as null awk '($1=="UPDATE PLAN ADD RATE SCHEDULE" && $4=="") {print $0}'... (5 Replies)
Discussion started by: r_t_1601
5 Replies

3. Shell Programming and Scripting

finding least out of selected lines

Hello, I have a file, which looks like: I want to print the row containg "PRO" in second column after comparing and finding the minimum value of fifth column present in all "PRO". and likewise for every other string present in second column. I am using : filename=list... (2 Replies)
Discussion started by: CAch
2 Replies

4. Shell Programming and Scripting

trying to print selected fields of selected lines by AWK

I am trying to print 1st, 2nd, 13th and 14th fields of a file of line numbers from 29 to 10029. I dont know how to put this in one code. Currently I am removing the selected lines by awk 'NR==29,NR==10029' File1 > File2 and then doing awk '{print $1, $2, $13, $14}' File2 > File3 Can... (3 Replies)
Discussion started by: ananyob
3 Replies

5. Shell Programming and Scripting

Process selected lines

I have an if statement where I state that if there are more than one records (lines) found containing a string in a file, then it enters into a while loop to use each line for as many lines as there are and then stop. Trouble is, I can't figure out how to move to the next instance of each line. ... (2 Replies)
Discussion started by: derekphl
2 Replies

6. UNIX for Dummies Questions & Answers

Find & Copy Selected files to another Directory

I am wanting to find files within a directory that are over a certain number of days old and copy them to another directory. And unfortunately not having much luck.......is someone able to help. Would also like to add that there are literally thousands of files that I am wanting to copy in one... (3 Replies)
Discussion started by: hellfyre
3 Replies

7. UNIX for Dummies Questions & Answers

Copy selected Directories

I wonder if someone would help me a little here. I have a directory (folder on a mac) with about 100 subfolders and sub-subfolders and files there in. All sub directories have the same name structure, "AAA Name". Like this: ISP CompanyName ITS CompanyName KEL CompanyName KRA CompanyName... (2 Replies)
Discussion started by: sigurarm
2 Replies

8. Shell Programming and Scripting

print selected lines

Hi everybody: I try to print in new file selected lines from another file wich depends on the first column. I have done a script like this: lines=( "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "21" "31" "41" "51" "55" "57" "58" ) ${lines} for lines in ${lines} do awk -v ... (6 Replies)
Discussion started by: tonet
6 Replies

9. Shell Programming and Scripting

Copy selected contents from file

I want to capture contents of a file between 2 strings into another file for eg all lines in between the keywords "start log" and "end log" should be copied into another file (4 Replies)
Discussion started by: misenkiser
4 Replies

10. Solaris

how to do selected copy

I need to copy the files from one dir to another dir based on sysdate, like cp -> (sysdate-n) filename -> to -> new dir n = 1,2,3.............. (3 Replies)
Discussion started by: dbasan
3 Replies

Featured Tech Videos