I want to move a line to another specified in a variable.

So where I might do this to move the current line up seven lines:


I want to do it with a variable line destination. Something like this:

:let lineOffset=-7

Ultimately, I'm trying to append a collections of lines. So given this:


I want a simple way of getting this:

1 one
2 two
3 three

It seemed to me that the quickest way would be a macro. Something like this:

:let lineOffset+=1

I know you can't use shell variable syntax as shown above. But used here for purpose of comprehension.

Where lineOffset, using the example above, would be -3, so would move the current line 3 lines back when used as :m-3.

And where those statements

  1. Move current line to line number specified by lineOffset (move sends the cursor to that line as well);
  2. Move to line above moved line and join it with the moved line;
  3. Go to next line to be moved;
  4. Set the lineOffset to the next line down from the last.

1 Answer 1


to move the lines down by 2 and join with the next.

That applies the command to the whole document/buffer. As a side-effect, it leaves a search highlight on every line-start. :nohlsearch will clear the current search highlight. Additionally, a range can limit global to certain lines. So to apply it to only, say, lines 1 to 3, and remove the residual highlight:


And if you really want a variable to be involved:

  • 1
    What sort of magic is this? It worked! Oh I see: g applies command to every matching pattern, where: g/pattern/command. The command ^ selects every line-start, and m+2 moves each line down 2 lines. '|' tacks on another command, 'J', which joins the moved line with the one following.
    – markling
    Jan 30, 2022 at 9:43
  • But it's not J but j?
    – markling
    Jan 30, 2022 at 9:55

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.