 |
 |
 |
 |
| Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum. |

16th September 2011, 07:33 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 20

|
|
|
vim - copy and paste multiple lines in script
Hi guys,
Is there a quick way in vim to copy and paste multiple (e.g. 10 lines) in a script ? One of the guys at work said earlier it can be done by using the line numbers (:set nu) but I can't remember how! Any of you guys have any idea? Been wracking by brains but i'm at a loss!
Cheers
|

16th September 2011, 07:51 PM
|
 |
Registered User
|
|
Join Date: Jul 2009
Location: London,England
Posts: 1,095

|
|
|
Re: vim - copy and paste multiple lines in script
type '10y' with cursor on first line to copy (yank 10 lines, to delete them use 10dd)
then move to (just above) paste position and hit p
|

16th September 2011, 08:12 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 20

|
|
|
Re: vim - copy and paste multiple lines in script
Cheers, that helps a lot!
One question though, when I do '10y' it yanks 11 lines, any ideas why ?
|

17th September 2011, 09:54 PM
|
 |
Registered User
|
|
Join Date: Jul 2009
Location: London,England
Posts: 1,095

|
|
|
Re: vim - copy and paste multiple lines in script
Quote:
Originally Posted by jimbob01
Cheers, that helps a lot!
One question though, when I do '10y' it yanks 11 lines, any ideas why ?
|
You're right, that seems to be a bug.
|

17th September 2011, 11:04 PM
|
|
Registered User
|
|
Join Date: Jun 2011
Posts: 64

|
|
|
Re: vim - copy and paste multiple lines in script
Quote:
Originally Posted by jimbob01
Cheers, that helps a lot!
One question though, when I do '10y' it yanks 11 lines, any ideas why ?
|
The correct command would be '10yy'.
Quote:
Originally Posted by Gödel
You're right, that seems to be a bug.
|
Nope. '10y<enter>' has the same effect as '10yj' (or in other words y10j). This signifies a motion of 10 lines downwards, which puts the cursor on the 11th line. Hence why 11 lines are yanked.
|

17th September 2011, 11:16 PM
|
 |
Registered User
|
|
Join Date: Jul 2009
Location: London,England
Posts: 1,095

|
|
|
Re: vim - copy and paste multiple lines in script
Quote:
Originally Posted by jnguyen
The correct command would be '10yy'.
Nope. '10y<enter>' has the same effect as '10yj' (or in other words y10j). This signifies a motion of 10 lines downwards, which puts the cursor on the 11th line. Hence why 11 lines are yanked.
|
also the correct command would be '10Y' (shift+y = capital Y)
I just forgot uppercase Y, but you're right it's not a bug.
|

18th September 2011, 08:44 AM
|
 |
Registered User
|
|
Join Date: Oct 2006
Location: Singapore, 新加坡
Posts: 733

|
|
|
Re: vim - copy and paste multiple lines in script
I had used this counting the "number of lines" approach for quite a long time until I learn better approaches. I am sure you will agree that even with ":set nu" it is still unpractical to every time calculate the difference between end and start of lines that you want to yank or delete.
One of the easiest approach is to use the visual mode. Move the cursor to the start, then press "v" to switch on the visual mode, move the cursor to the end, then press "y" for yank command. Move the cursor position to where you want to paste, and as usual using "p" or "P" to paste the lines after or before the cursor position, respectively.
Another approach is to use "mark". Move the cursor to the start, then press "ma" to mark 'a' at current cursor position. Move the cursor to the end lines that you want to copy, then press "y'a" to yank all the lines between cursor to the mark 'a' into unnamed buffer. Then paste the yanked lines as per normal. I prefer this approach as I could define different marks (from 'a' to 'z') for different positions that I may want to jump back quickly. It can also be used in tandem with named buffer, etc.
__________________
YaoWT - Leave no window unbroken ^_^
|

18th September 2011, 04:28 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 20

|
|
|
Re: vim - copy and paste multiple lines in script
Thanks guys, helps a lot.
@weitjong - Yeah, your option does seem better, especially when I'll be counting a lot of lines to copy. Thanks again
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 00:46 (Sunday, 19-05-2013)
|
|
 |
 |
 |
 |
|
|