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

How do I move a line to a specific line number?

Answer

:m {line-number}

Explanation

The :m (move) command moves the current line to after the specified line number. It is the cleanest way to reposition a line without yanking and pasting.

How it works

  • :m 0 moves the current line to the top of the file
  • :m $ moves it to the end
  • :m 10 moves it to after line 10
  • :m +1 moves it down one line
  • :m -2 moves it up one line

Example

With the cursor on a misplaced import line, :m 0 moves it to the top of the file.

Tips

  • :m+1 and :m-2 are useful for nudging lines up/down
  • :'<,'>m $ moves the visual selection to end of file
  • :m can use marks as targets: :m 'a
  • :t (copy) is similar but copies instead of moving
  • Map these for quick line movement: nnoremap <A-j> :m+1<CR>

Next

How do you yank a single word into a named register?