How do I reverse selected lines order in Vim?
To reverse all the lines in a file,
:g/^/m0
For an explanation see
:help 12.4
which also shows how to reverse just a range of lines.
Select the desired lines, hit !
, and in the resulting prompt pipe the lines through tac
a la :'<,'>!tac
. See man tac
for more details.
On Mac OS X, tac
does not exist, but you can use tail -r
to the same effect:
:%!tail -r
This also works nicely for visual mode:
:'<,'>!tail -r
Excerpt from tail(1)
's manpage:
The -r option causes the input to be displayed in reverse order, by line. Additionally, this option changes the meaning of the -b, -c and -n options. When the -r option is specified, these options specify the number of bytes, lines or 512-byte blocks to display, instead of the bytes, lines or blocks from the beginning or end of the input from which to begin the display. The default for the -r option is to display all of the input.