vimtricks.wiki Concise Vim tricks, one at a time.

How do I swap two adjacent lines?

Answer

ddp

Explanation

The ddp sequence swaps the current line with the line below it. It is the line-level equivalent of the xp character swap.

How it works

  • dd deletes the current line (saved to register)
  • p pastes the line below the new current line
  • The result is the two lines have swapped positions

Example

line B
line A

With the cursor on line B, pressing ddp gives:

line A
line B

Tips

  • ddkP swaps the current line with the line above
  • :m+1 moves the current line down one position (same effect)
  • :m-2 moves the current line up one position
  • In visual mode, you can use :m'>+1 to move selected lines down

Next

How do you yank a single word into a named register?