← All tips

Reverse Lines In File

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:

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!