329

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?

3
  • So...you want to join alternating lines? That is, you want to join line x with join x+2?
    – larsks
    May 25, 2012 at 19:36
  • 1
    No, I have two separate blocks. Pseudocode-ish: [a[i] + b[i] for i in min(len(a), len(b))] May 25, 2012 at 19:37
  • 2
    See a similar question (and answer!) here
    – NWS
    May 26, 2012 at 21:17

10 Answers 10

885
+100

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).

11
  • 1
    hmm... I only have to press enter once. And it won't insert a space between the two halves. If there's some trailing space on the lines (like in the "work it " example), that will still be there. You can get rid of any trailing space using s/\s*$/ instead of s/$/.
    – rampion
    May 25, 2012 at 20:04
  • 1
    Thanks for the explanation. :sil5,8del | let l=split(@") | sil1,4s/$/\=remove(l,0)/ | call histdel("/", -1) | nohls seems to be even better since it cleans up the search history after running. And it doesn't show the "x more/less lines" message requiring me to press enter. May 26, 2012 at 8:15
  • 12
    If you want full vim reference for that answer: :help range, :help :d, :help :let, :help split(), :help :s, :help :s\=, :help remove().
    – Benoit
    May 26, 2012 at 16:41
  • 17
    Making sure people like you want to post answers like this is why I became a moderator. Good show :)
    – user50049
    May 26, 2012 at 17:17
  • 1
    There is a problem if there is not a white space after the first 4 sentences.
    – Reman
    Jun 7, 2013 at 9:11
59
+150

An elegant and concise Ex command solving the issue can be obtained by combining the :global, :move, and :join commands. Assuming that the first block of lines starts on the first line of the buffer, and that the cursor is located on the line immediately preceding the first line of the second block, the command is as follows.

:1,g/^/''+m.|-j!

For detailed explanation of this technique, see my answer to an essentially the same question “How to achieve the “paste -d '␣'” behavior out of the box in Vim?”.

6
  • E16: Invalid Range - but it works anyway. When removing the 1, it works without the error. May 26, 2012 at 7:56
  • And too bad you cannot accept more than one answer - otherwise yours would have a green mark, too! May 26, 2012 at 8:20
  • 3
    Very nice! I didn't know about :move and :join!, nor what '' meant as a range argument (:help '') and what + and - meant as range modifiers (:help range). Thanks!
    – rampion
    May 26, 2012 at 14:10
  • @ThiefMaster: The command is designed to be used when the two range of lines to join are adjacent to each other, so that the cursor is initially located at the last line of the first block that immediately precedes the first line of the second block. (See the linked answer and question.) The command works as intended even if the number of lines in the blocks differs, although it gives an error message in that case. One can suppress error messages by prepending sil! to the command.
    – ib.
    May 28, 2012 at 11:00
  • 2
    @ib.: I think it would be a good idea to put the detailed explanation into this answer, too. May 29, 2012 at 18:16
46

To join blocks of line, you have to do the following steps:

  1. Go to the third line: jj
  2. Enter visual block mode: CTRL-v
  3. Anchor the cursor to the end of the line (important for lines of differing length): $
  4. Go to the end: CTRL-END
  5. Cut the block: x
  6. Go to the end of the first line: kk$
  7. Paste the block here: p

The movement is not the best one (I'm not an expert), but it works like you wanted. Hope there will be a shorter version of it.

Here are the prerequisits so this technique works well:

  • All lines of the starting block (in the example in the question abc and def) have the same length XOR
  • the first line of the starting block is the longest, and you don't care about the additional spaces in between) XOR
  • The first line of the starting block is not the longest, and you additional spaces to the end.
5
  • Woah, that's interesting! I never would have thought that it would work that way.
    – voithos
    May 25, 2012 at 19:55
  • 13
    This works as wanted only because abc and def are the same length. Block selection will keep the indents of the deleted text, so if the cursor is on a short line when putting the text, the lines get inserted between letters on the longer ones - and spaces appended to the shorter ones if the cursor was on a longer one.
    – Izkata
    May 26, 2012 at 23:02
  • Indeed... Is there a way to do it properly using block copy&paste? This is the easiest way to do it after all (especially if you cannot lookup the more complicated ways here for some reason). May 28, 2012 at 9:19
  • 2
    Like @Izkata says you will run into the problem of text getting inserted between the longer lines. To workaround that, just add more spaces at the end of the first line to make it the longest and then paste your block of text. Once that's done, compressing multiple spaces to one is as simple as :%s/ \+/ /g May 30, 2012 at 5:41
  • 1
    set ve=all should help, see vimdoc.sourceforge.net/htmldoc/options.html#'virtualedit'
    – Ben
    Aug 13, 2014 at 12:08
19

Here's how I'd do it (with the cursor on the first line):

qama:5<CR>y$'a$p:5<CR>dd'ajq3@a

You need to know two things:

  • The line number on which the first line of the second group starts (5 in my case), and
  • the number of lines in each group (3 in my example).

Here's what's going on:

  • qa records everything up to the next q into a "buffer" in a.
  • ma creates a mark on the current line.
  • :5<CR> goes to the next group.
  • y$ yanks the rest of the line.
  • 'a returns to the mark, set earlier.
  • $p pastes at the end of the line.
  • :5<CR> returns to the second group's first line.
  • dd deletes it.
  • 'a returns to the mark.
  • jq goes down one line, and stops recording.
  • 3@a repeats the action for each line (3 in my case)
3
  • 1
    You have to press [Enter] after :5 both times you type it or this won't work. May 26, 2012 at 4:14
  • 1
    I'm on gvim. Is there some way to copy&paste that command in GVim? ^V automatically pastes in insert mode (which makes sense, that's what people usually want) even if i'm currently in normal(?) mode. I tried :norm qama:5<CR>y$'a$p:5<CR>dd'ajq3@a but that seems to execute only q. May 26, 2012 at 8:04
  • 1
    ThiefMaster: Try :let @a="ma:5^My$'a$p:5^Mdd'aj" | normal 4@a, where the ^M characters are typed by hitting CTRL-V then Enter.
    – rampion
    May 26, 2012 at 14:07
8

As mentioned elsewhere, block selection is the way to go. But you can also use any variant of:

:!tail -n -6 % | paste -d '\0' % - | head -n 5

This method relies on the UNIX command line. The paste utility was created to handle this sort of line merging.

PASTE(1)                  BSD General Commands Manual                 PASTE(1)

NAME
     paste -- merge corresponding or subsequent lines of files

SYNOPSIS
     paste [-s] [-d list] file ...

DESCRIPTION
     The paste utility concatenates the corresponding lines of the given input files, replacing all but the last file's newline characters with a single tab character,
     and writes the resulting lines to standard output.  If end-of-file is reached on an input file while other input files still contain data, the file is treated as if
     it were an endless source of empty lines.
2
  • Using block selection is not the only way to go. Nor is it the simplest one. The desired (paste -d-like) behavior can be implemented by means of short Vim command, as it shown in my answer.
    – ib.
    May 26, 2012 at 7:18
  • 3
    Besides that, I'm on windows so that solution would involve opening a SSH connection to my linux machine and pasting from the editor into the terminal and back. May 26, 2012 at 7:57
3

Sample data is the same as rampion's.

:1,4s/$/\=getline(line('.')+4)/ | 5,8d
3

I wouldn't think make it too complicated. I would just set virtualedit on
(:set virtualedit=all)
Select block 123 and all below.
Put it after the first column:

abc    123
def    45
...    ...

and remove the multiple space between to 1 space:

:%s/\s\{2,}/ /g
1
  • The question actually asks for no spaces, I would do something like gvV:'<,'>s/\s+//g (vim should automatically insert the '<,'> for you so you don't need to manually type it).
    – Ben
    Aug 13, 2014 at 13:34
2

I would use complex repeats :)

Given this:

aaa
bbb
ccc

AAA
BBB
CCC

With the cursor on the first line, press the following:

qa}jdd''pkJxjq

and then press @a (and you may subsequently use @@) as many times as needed.

You should end up with:

aaaAAA
bbbBBB
cccCCC

(Plus a newline.)

Explaination:

  • qa starts recording a complex repeat in a

  • } jumps to the next empty line

  • jdd deletes the next line

  • '' goes back to the position before the last jump

  • p paste the deleted line under the current one

  • kJ append the current line to the end of the previous one

  • x delete the space that J adds between the combined lines; you can omit this if you want the space

  • j go to the next line

  • q end the complex repeat recording

After that you'd use @a to run the complex repeat stored in a, and then you can use @@ to rerun the last ran complex repeat.

1

There can be many number of ways to accomplish this. I will merge two blocks of text using any of the following two methods.

suppose first block is at line 1 and 2nd block starts from line 10 with the cursor's initial position at line number 1.

(\n means pressing the enter key.)

1. abc
   def
   ghi        

10. 123
    456
    789

with a macro using the commands: copy,paste and join.

qaqqa:+9y\npkJjq2@a10G3dd

with a macro using the commands move a line at nth line number and join.

qcqqc:10m .\nkJjq2@c

0

@rampion's answer inspired me, I make a keymap to merge two blocks

" paste copied block(already stored in register) to the end of current area
function block_paste(direct)
    let current_line = line('.')
    let copied_contents = split(@", "\n")

    for index in range(0, len(copied_contents) - 1)
        if a:direct == 'head' " head
            let res = copied_contents[index] . getline(current_line + index)
        elseif a:direct == 'tail' " tail
            let res = getline(current_line + index) . copied_contents[index]
        else
            let res = ''
        endif

        let target_line = current_line + index
        if target_line <= line('$')
            call setline(target_line, res)
        else
            call append(target_line - 1, res)
        endif
    endfor
endfunction


nnoremap <Leader>bp :call block_paste('tail')<CR>
nnoremap <Leader>bP :call block_paste('head')<CR>

keymap bp can append copied block to the end of current block, and keymap bP can insert copied block to the beginning of current block

If you don't like bp / bP, you can bind to any keymap you like

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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