Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How do I copy multiple lines and paste them as a block before a line? As an example I have the following code and I want to copy and paste the three lines after the if statement to after the else statement but before the line below it.

[row col] = find(H);  
if (nargin < 4)    

    delqmn = sparse(row, col, 0, M, N); % diff of msgs from bits to checks
    delrmn = sparse(row, col, 0, M, N);% diff of msgs from checks to bits
    rmn0 = sparse(row, col, 0, M, N);% msgs from checks to bits (p=0)
else

// Insert 3 lines after if statement here

qn0 = 1-r;% pseudoposterior probabilities
qn1 = r;% pseudoposterior probabilities

Thanks

share|improve this question
3  
This is basic stuff and is covered in the vimtutor. I suggest you run it if you haven't already. –  Heptite Dec 6 '12 at 5:03

2 Answers 2

up vote 2 down vote accepted

You can find a lot of useful information from vim itself with

:help registers

Briefly, to copy multiple lines use: #yy where # is the number of lines to copy

To paste those lines use P to paste above the cursor and p to paste below.

If you copy each line at a time (by doing yy three times), you will copy text into three registers. In order to paste the lines back in, you can paste from each of these registers with "#p (where # is the register number). This is a good method if the copied text isn't in a contiguous block.

share|improve this answer
    
Umm, I think you misunderstand how "p" works. If a register is not specified, it will always paste the unnamed register ("). So pressing p three times will give you three copies of the last line yanked, unless the user deliberately says otherwise. –  Heptite Dec 6 '12 at 5:07
    
oic. I was sure it cleared 0 and moved 1 into 0 and ". will edit –  mcalex Dec 6 '12 at 5:15
    
Almost there. You have to prefix the register name with a " character, otherwise it's treated as a count. For example, 5p will paste the unnamed register five times, while "5p will paste the numbered register 5. –  Heptite Dec 6 '12 at 5:20
    
As I recommended to the OP, you might want to run through the vimtutor if you haven't already. –  Heptite Dec 6 '12 at 5:21
    
@Heptite oops, typo. thx. where's your answer –  mcalex Dec 6 '12 at 5:41

As Heptite already wrote, this is basic stuff. vimtutor rules.

In case you are curious, here are a few slightly "advanced" ways to do what you want. Assuming your cursor is on the first line of the block you want to duplicate and you have set number, all of the methods below lead to the same result. It's of course possible to combine their individual parts to suit your needs, this is far from being an exhaustive list.

3yy/els<cr>p
    3yy      yank 3 lines starting with the current one
    /els<cr> position the cursor on else
    p        put the content of the default register after the cursor

y2j7Gp
    y2j      yank until 2 lines below
    7G       move the cursor to line 7
    p        put the content of the default register after the cursor

3yy}P
    3yy      yank 3 lines starting with the current one
    }        place the cursor on the next blank line        
    P        put the content of the default register before the cursor

V2j:t+3
    V2j      visually select linewise until 2 lines below
    :        enter command-line mode, a range is inserted for you
    t+3      copy the selected lines after line (current line + 3)

:4,6t7
    :        enter command-line mode
    4,6      the line numbers of the block you want to copy, see :help range
    t7       copy those lines after line 7
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.