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

How do I move lines from one location to another without yanking and pasting?

Answer

:{range}move {address} or :m

Explanation

:move (abbreviated :m) relocates lines from one position to another in a single command — no yank register involved, no cursor jumping. The lines are removed from their original position and placed after the target address. It is cleaner than dd + navigation + p and does not touch any registers.

Syntax

:{range}move {address}
:{range}m {address}

Examples

Move the current line to below line 20:

:m 20

Move the current line to the end of the file:

:m $

Move the current line up 3 lines:

:m -4

(Address -4 means 3 lines above, because the line is placed after the address.)

Move the current line down 2 lines:

:m +2

Move lines 5–10 to below the current line:

:5,10m .

Move the visual selection to the top of the file:

:'<,'>m 0

Handy mappings

Move current line up/down with Alt+j/k:

nnoremap <A-j> :m +1<CR>==
nnoremap <A-k> :m -2<CR>==
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv

Tips

  • :m does not touch any register — your yank stays intact
  • The == after the mapping re-indents the moved line; gv=gv re-indents and reselects in visual mode
  • :m is undoable with a single u — even when moving many lines
  • Combine with :g: :%g/TODO/m $ moves all TODO lines to the end of the file

Next

How do I run a search and replace only within a visually selected region?