How do I move a line or selection up or down in Vim?
:move +1 / :move -2
The :move command relocates lines to a specific position without using delete and paste.
:move +1 / :move -2
The :move command relocates lines to a specific position without using delete and paste.
:%!tac
Vim doesn't have a built-in reverse command, but you can pipe the buffer through tac (reverse of cat) to flip line order.
:10,20d
Specify a line range before a command.
:10,15m 25
Use :m (move) with a range and destination.
:10,15t 25
Use :t (copy) with a range and destination.
:'<,'>!nl -ba
Select lines visually, then run :'!nl -ba to pipe through the nl command which prepends line numbers to each selected line.
qaddpq
Record a macro that deletes the current line with dd and pastes it below the next line with p.
qaJjq
Record a macro that joins the current line with the next using J, then moves down one line with j.
:g/^/m0
The :g/^/m0 command is a clever use of Vim's global command to reverse every line in the file.
:m+1 / :m-2
The :m (move) command relocates lines to a new position in the file without using registers.
editing #editing #ex-commands #lines #productivity #mappings
:'<,'>s/\n/, /g
Vim's J command joins lines with a single space, but sometimes you need a custom separator like a comma, pipe, or semicolon.
editing #editing #ex-commands #visual-mode #substitution #lines
:10,20t30
The :t command (short for :copy) duplicates lines from one location to another without touching any registers.
command-line #editing #ex-commands #lines #productivity #ranges