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]emoves the current line down 3 positions - In visual line mode,
]eand[emove the entire selection up or down
Example
Given these lines with the cursor on b:
a
b
c
]eresults in:a,c,b(b moves down)[eresults 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/[eto 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/]bfor buffers,[q/]qfor quickfix,[<Space>/]<Space>for blank lines - Available via
tpope/vim-unimpaired