Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7989027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:37:55+00:00 2026-06-04T12:37:55+00:00

I’d like to merge two blocks of lines in Vim, i.e., take lines k

  • 0

I’d like to merge two blocks of lines in Vim, i.e., take lines k through l and append them to lines m through n. If you prefer a pseudocode explanation: [line[k+i] + line[m+i] for i in range(min(l-k, n-m)+1)].

For example,

abc
def
...

123
45
...

should become

abc123
def45

Is there a nice way to do this without copying and pasting manually line by line?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T12:37:57+00:00Added an answer on June 4, 2026 at 12:37 pm

    You can certainly do all this with a single copy/paste (using block-mode selection), but I’m guessing that’s not what you want.

    If you want to do this with just Ex commands

    :5,8del | let l=split(@") | 1,4s/$/\=remove(l,0)/
    

    will transform

    work it 
    make it 
    do it 
    makes us 
    harder
    better
    faster
    stronger
    ~
    

    into

    work it harder
    make it better
    do it faster
    makes us stronger
    ~
    

    UPDATE: An answer with this many upvotes deserves a more thorough explanation.

    In Vim, you can use the pipe character (|) to chain multiple Ex commands, so the above is equivalent to

    :5,8del
    :let l=split(@")
    :1,4s/$/\=remove(l,0)/
    

    Many Ex commands accept a range of lines as a prefix argument – in the above case the 5,8 before the del and the 1,4 before the s/// specify which lines the commands operate on.

    del deletes the given lines. It can take a register argument, but when one is not given, it dumps the lines to the unnamed register, @", just like deleting in normal mode does. let l=split(@") then splits the deleted lines into a list, using the default delimiter: whitespace. To work properly on input that had whitespace in the deleted lines, like:

    more than 
    hour 
    our 
    never 
    ever
    after
    work is
    over
    ~
    

    we’d need to specify a different delimiter, to prevent “work is” from being split into two list elements: let l=split(@","\n").

    Finally, in the substitution s/$/\=remove(l,0)/, we replace the end of each line ($) with the value of the expression remove(l,0). remove(l,0) alters the list l, deleting and returning its first element. This lets us replace the deleted lines in the order in which we read them. We could instead replace the deleted lines in reverse order by using remove(l,-1).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays. I'd like to merge them into one, but remove the
That one is a tricky one I believe. I would like to merge two
I would like to be able to merge two files into one during configure
I have two classes that I would like to merge into a composite. These
We would like to merge two of our servers together and in order to
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
I'd like to merge two arrays with each other: $filtered = array(1 => 'a',
I would like to merge two files by one column in unix. I have
I would like to merge two arrays containing a list of files plus their
I have an XML file and I would like to merge two different CONTACT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.