TIL: How to move a line in Vim

For a text like the following:

This is the line I want to move.
This line should be the first line.

In order to move the first line down, in normal mode* (not edit or insert mode), follow these steps:

  • place the cursor at the beginning of the line you want to move – in this case, the first line
  • hit dd – the line will disappear (it’s the same as using “cut” on a regular text editor)
  • move the cursor to where the line should be – in this case, after the second line
  • hit p

Voilà!

This line should be the first line.
This is the line I want to move.

NOTES:

  • If you are in edit mode and want to go back to normal mode, just hit ESC
Tweet says: "I've been using Vim for about 2 years now, mostly because I can't figure out how to exit it."
Twitter profile mocking how non-intuitive is exiting the Vim editor.

For the sake of completeness, if you need to exit vi and save the changes, hit :, followed by w (meaning “write”), followed by q (meaning “quit”). And if you need to exit without saving, hit :, followed by q, followed by !

The post TIL: How to move a line in Vim was originally published at flaviabastos.ca