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

How do I move the current line up or down without cutting and pasting?

Answer

:m+1 / :m-2

Explanation

The :m (move) command relocates lines to a new position in the file without using registers. Use :m+1 to move the current line down one position or :m-2 to move it up one. This is cleaner than ddp or ddkP because it doesn't clobber your yank register.

How it works

  • :m takes a destination line number as its argument
  • :m+1 moves the current line to after the line below it (i.e., down one)
  • :m-2 moves the current line to after two lines above it (i.e., up one)
  • The offset is relative: +1 means one line after current position, -2 means two lines before

The reason "up one" is -2 and not -1: :m-1 moves the line after line current - 1, which is its current position — a no-op.

Example

Given the text with the cursor on line 2:

alpha
beta
gamma

Running :m+1 moves beta down:

alpha
gamma
beta

Or with the cursor on beta, running :m-2 moves it up:

beta
alpha
gamma

Tips

  • Map these for convenience in your vimrc:
nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
  • The == at the end re-indents the line after moving, which is essential for code
  • Move a visual selection with :'<,'>m'>+1 (down) or :'<,'>m'<-2 (up)
  • Move a range of lines: :3,5m10 moves lines 3 through 5 to after line 10
  • Unlike ddp, the :m command does not overwrite the unnamed register, so your last yank is preserved
  • Use :m0 to move a line to the top of the file or :m$ to move it to the bottom

Next

How do I edit multiple lines at once using multiple cursors in Vim?