1

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
2
  • 1
    For this specific case, with the cursor on line 1, press J.
    – DopeGhoti
    Aug 9, 2018 at 17:51
  • Yeah, like you can simply do :m9 to move the current line to line 10 (line after line 9). Or :12m9 to move line 12 to line after line 9. When you do this, the cursor jumps to the new line, and you can kJ to join it (effectively append it) to line 9. Really, though you just want something like :12j9 ... surely something like this is possible.
    – markling
    Jan 29, 2022 at 12:53

1 Answer 1

1

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.

1
  • 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.
    – Adele
    Aug 9, 2018 at 20:51

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.