Moving lines in Vim
I’m learning to move ranges of lines in Vim. What I usually did was jump to the starting line, enter Visual Mode, select the lines I want to move, cut, move to the desired location and finally paste.
But Vim has a more efficient, shorter and faster way to do this, no cursor moving at all.
- Move current line to line after 12:
:m 12
- Move current line to before first line
:m 0
- Move current line to after last tine
:m $
- Move current line to after line with mark
a
::m 'a
- Move range of lines:
5,7m 21
— will move lines 5-7 to after line 21. - Move 5 lines starting at current line to after line 21:
:.,.+4m 21
- Move current line 2 lines after current line:
:m .+2
And you can return to the line you were with g;
(and g,
is the opposite).