To reverse the lines in a file, you can use the follow invocation of Vim’s global command:
:g/^/m0
Now, this is fairly obscure — here’s how to read it:
:gis saying ‘run the global command’/^/defines a regular expression that matches any linem0is an Ex command that means ‘move the current line to line 1’ (the:mcommand moves to one below the targeted line, hence we have to put0if we want the line to appear on line1)
Altogether, this is saying:
For each line in the file, move it to line 1
Since the global command works in a top-to-bottom manner, this has the effect of reversing the order of the lines in the file!
Give it a try in the editor!
If you want to learn more about the global command, check out our lesson!