How do I reverse selected lines order in Vim?

For example, if I have four lines as follows:

the first line
the second line
the third line
the fourth line

I want to reverse them to

the fourth line
the third line
the second line
the first line

How could I do this in Vim?


Solution 1:

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.

Solution 2:

Select the desired lines, hit !, and in the resulting prompt pipe the lines through tac a la :'<,'>!tac. See man tac for more details.