How do I move lines to a different position without using yank and paste?
Answer
:m +1
Explanation
The :move command (abbreviated :m) relocates lines to a new position in the buffer without touching any registers. Unlike dd followed by p, your yank register stays intact. You can move single lines, ranges, or visual selections to any line number.
How it works
:m +1— move the current line down one position (below the next line):m -2— move the current line up one position (above the previous line):m 0— move the current line to the very top of the file:m $— move the current line to the very bottom of the file
The argument is the destination line number. The moved line is placed after the specified line.
Example
Given this file with the cursor on line 2:
first
second
third
Running :m +1 moves "second" below "third":
first
third
second
Move a range of lines (lines 5-8 to after line 2):
:5,8m 2
Tips
- Works with visual selections: select lines, then type
:m '>+1to move the selection down - Unlike
dd/p, the move command preserves your unnamed register and all numbered registers - Combine with
:globalto collect matching lines::g/TODO/m $moves all TODO lines to the end of the file - The
:copy(:t) command works identically but duplicates instead of moving