How do I move the current line up or down using an Ex command?
Answer
:m.+1 and :m.-2
Explanation
The :move (:m) command relocates a line to a new position without cutting and pasting. :m.+1 moves the current line down one line, and :m.-2 moves it up one line. This is faster and more composable than ddp/ddkP because it works on ranges, accepts counts, and can be combined with mappings for a true "move line" experience.
How it works
:mis the:movecommand — it moves the addressed line(s) to after the target address.refers to the current line+1means "one line below the current position" (so the current line ends up one lower).-2means "two lines above the current position" — the destination is one above the current line, effectively moving it up
The address arithmetic can feel counterintuitive: to move down by one, the target is +1 (after the next line); to move up by one, the target is .-2 (before the line above).
Example
Before (cursor on "banana"):
apple
banana
cherry
After :m.+1:
apple
cherry
banana
After :m.-2:
banana
apple
cherry
Tips
- Works on ranges:
:'<,'>m.moves the visual selection after the current line - Add convenient mappings:
nnoremap <A-j> :m.+1<CR>==andnnoremap <A-k> :m.-2<CR>==— the==re-indents after moving :m 0moves the current line to the very top of the file;:m $moves it to the bottom