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

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

Answer

[e and ]e

Explanation

The [e and ]e mappings from Tim Pope's vim-unimpaired plugin exchange the current line (or a visual selection of lines) with the line above or below. This is much faster than the traditional ddkP (move up) or ddp (move down) approach, and works on ranges too.

How it works

  • ]e — exchange the current line with the next line (move down)
  • [e — exchange the current line with the previous line (move up)
  • Both accept a count: 3]e moves the current line down 3 positions
  • In visual line mode, ]e and [e move the entire selection up or down

Example

Given these lines with the cursor on b:

a
b
c
  • ]e results in: a, c, b (b moves down)
  • [e results in: b, a, c (b moves up)

With a count, 2]e moves the line down 2 positions.

Tips

  • In visual mode, select multiple lines and use ]e/[e to bubble them up or down as a block
  • This is equivalent to :m .+1 (move down) or :m .-2 (move up) but more ergonomic
  • vim-unimpaired provides many paired mappings: [b/]b for buffers, [q/]q for quickfix, [<Space>/]<Space> for blank lines
  • Available via tpope/vim-unimpaired

Next

How do I configure Vim's command-line tab completion to show all matches and complete to the longest common prefix?