For example suppose i have

1 the cat jumps
2 over the moon

I would like the result

1 the cat jumps over the moon

In general how can I move line X onto the end of line Y. A solution that would alllow something like

1 The fat cat   
2 jumps high
3 over the sky
4 is well behaved

Is there a command that takes line X line Y as input and appends the content of line Y onto line X. In this example if X=1 and Y =4 we get

command(1,4)

1 The fat cat is well behaved
2 jumps high
3 over the sky
  • 1
    For this specific case, with the cursor on line 1, press J. – DopeGhoti Aug 9 at 17:51

For the following, I am assuming that the numbers 1 to 4 indicate the line numbers.

I am not aware of a one-shot command of moving an arbitrary line to the end of another line. J can be used to join the next line to the current line. Else one can do something like this:

  1. 4G (move to line 4)
  2. dd (delete the line)
  3. 1G (move to line 1)
  4. pkJ (insert line and join them)

You can create a custom command that does this for you, similar to what you ask:

command! -nargs=* Join call Join(<f-args>)
function! Join(...)
  if a:0 != 2 | return | endif
  execute 'normal!' a:2 . 'Gdd'
  execute 'normal!' (a:2 > a:1 ? a:1 : a:1 - 1) . 'GpkJ'
endfunction

Now you can do things like this:

" Join line 4 to line 1
:Join 1 4

" Join line 2 to line 4
:Join 4 2

Note that the behaviour of J depends on your formatoptions setting, see :h formatoptions and :h fo-table.

  • Is there a more general way to take the contents of a particular line X and append it to the end of line Y. I have edited the question to provide an example of what i mean. – Matt Mclaughlin Aug 9 at 20:51

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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