Text & writing
Reverse Line Order in Excel, Google Sheets, and Editors
Spreadsheets and text editors are built around sorting, not flipping, so reversing the order of rows or lines usually takes a small trick. Here is how to do it in the programs people most often use, and when pasting into a dedicated tool is quicker.
Ready to try the tool this guide describes?
Excel
In Excel for Microsoft 365 and Excel 2021, a formula can do it without changing the original: =SORTBY(A2:A20, ROW(A2:A20), -1) returns the range in reverse order.
- In an empty column next to your data, number the rows 1, 2, 3 … down to the last row (type 1 and 2, select both, and drag the fill handle).
- Select the data together with the numbers and choose Data → Sort, sorting by the number column from largest to smallest.
- Delete the helper column.
Google Sheets
Use =SORT(A2:A20, ROW(A2:A20), FALSE). It sorts the rows by their row number, largest first, which is exactly the reverse order. Copy the result and use Paste special → Values only if you need plain values.
Command line and code editors
Many other editors only offer sort commands; check the command palette or an extension for a reverse-lines command.
- Linux: tac file.txt prints the lines of a file last-first.
- macOS: tail -r file.txt does the same.
- Windows PowerShell: $l = Get-Content file.txt; [array]::Reverse($l); $l
- Vim: the command :g/^/m0 moves every line to the top in turn, reversing the buffer.
- Emacs: select the lines and run M-x reverse-region.
When a text tool is quicker
For a one-off list copied from an email, a web page, or a chat, pasting it into a reverse-line-order tool avoids helper columns and formulas. A single column copied out of a spreadsheet pastes as one value per line, and the reversed lines paste back into cells the same way.
Reverse Lines in Excel and Editors FAQ
- Does Excel have a reverse button?
- No. Use a helper column of row numbers and sort it largest to smallest, or the SORTBY formula in newer versions.
- Which formula reverses a column in Google Sheets?
- =SORT(A2:A20, ROW(A2:A20), FALSE) returns the range with the last row first.
- How do I reverse a text file on a Mac?
- Run tail -r file.txt in Terminal. On Linux the equivalent is tac file.txt.
- Will reversing rows in Excel keep each row together?
- Yes, as long as you select all the columns before sorting by the helper column, so every row moves as a unit.
- Can I reverse lines in Vim?
- Yes. :g/^/m0 reverses the whole file; add a range to reverse only part of it.
Related guides
- Flip a Log to Newest FirstWhy logs, chat exports, and changelogs are written oldest-first, when newest-first reads better, and how multi-line entries and headers affect a reversal.
- Reversed, Mirrored, Upside-Down TextThe difference between reversed text, reverse line order, mirrored text, and upside-down text, and how each one is created.
Open the tool
Jump into Reverse Line Order when you are ready to process your files.
