Flip or reverse line order in Notepad++

99

29

I want to flip line orders of a document with 500+ lines. The lines aren't just numbers, some include text and other characters. It's a mix.

Example:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6

Which I then want to flip, reverse and look like this from bottom to top:

Line 6
Line 5
Line 4
Line 3
Line 2
Line 1

Danpe

Posted 2011-09-02T12:19:53.610

Reputation: 1 131

Related (but not duplicate): How to reverse a text file on Windows

– Peter Mortensen – 2019-07-14T12:35:33.560

Meaning you want the numbers on the left margin to count down, or you want the actual content of your file to be reversed? – Maxpm – 2011-09-02T12:30:31.137

2@Maxpm Actual content of the file to be reversed. – Danpe – 2011-09-02T12:40:55.543

possible duplicate of Reverse line order in Notepad++

– Franck Dernoncourt – 2014-02-28T15:49:46.090

Answers

103

Solution not requiring other software except normally-included TextFX plugin:

  1. Edit > Select All
  2. TextFX > TextFX Tools > Insert Line Numbers
  3. If TextFX > TextFX Tools > +Sort ascending is checked, uncheck it
  4. TextFX > TextFX Tools > Sort lines case sensitive (at column)
  5. TextFX > TextFX Tools > Delete Line Numbers or First Word

Gnubie

Posted 2011-09-02T12:19:53.610

Reputation: 2 371

2

For those using Eclipse see this answer for doing this even more simply. (I know the original question asked for Notepad++ but thought this would be worth adding for those stumbling across this).

– Steve Chambers – 2015-06-25T15:38:06.273

@SteveChambers more simply is an understatement. You can highlight desired lines and reverse 'em in just two mouse clicks. Thanks a lot for pointing this out Steve, it made my life easier today. – b1nary.atr0phy – 2015-07-13T04:21:48.097

Anyone know if there is a way to macro this? My macro hotkey just prints junk when I try to use it on an arbitrary block of text. – Amalgovinus – 2017-02-24T18:29:02.973

2TextFX is not a supported plugin in the 64 bit version. – Rhyous – 2019-02-21T22:16:32.257

TextFX has been phased out - from TextFX's Future: "... bid farewell to an aging workhorse that has served the community well."

– Peter Mortensen – 2019-07-14T11:57:50.987

6

If TextFX is not found then go to: Plugins » Plugin Manager » Show Plugin Manager » Check "TextFX Characters", Click Install. See here: http://textfx.no-ip.com/textfx/

– Kalpesh Panchal – 2012-07-25T16:18:43.983

-what worked was "Sort lines case insensitive" in part 4) – Theta30 – 2014-04-01T23:43:11.897

@Theta30: Guess the UI text has changed since the version I used. – Gnubie – 2014-04-02T17:56:52.213

39

This can also be done in Notepad++ without the TextFX plugin. It follows the same strategy of that of the accepted answer, but using native functionality. It is done as follows:

  1. Edit > Select All
  2. Edit > Column Editor... > Select Number to Insert > Set Initial number to 1 > Set Increase by to 1 > Check Leading zeros > Click OK

enter image description here

  1. Edit > Line Operations > Sort Lines in Descending Order Edit: A recent update added extra sorting options, the option: Sort Lines Lexicographically Descending seems to do the job.

enter image description here

  1. Remove Line Numbers through either box selection (Alt+Left Click Drag or Alt+Shift Select) or Search/Replace

enter image description here

Trevor Young

Posted 2011-09-02T12:19:53.610

Reputation: 491

1It works, but there are two things to take in mind: 1- It inserts an extra number for last (empty) row, so you have to delete it before sorting. 2- if your list starts with numbers you could mess the inserted numbers with your numbers, because it doesn't insert an space after inserted numbers, so you should be careful in this case. – Francisco Alvarado – 2015-09-03T15:58:12.263

Can't reproduce step 3. The new version has changed the wording, and whatever option doesn't work for me

– Ooker – 2017-12-06T15:24:34.427

@Ooker I updated step 3. Let me know if that works. – Trevor Young – 2017-12-06T23:08:55.500

1

@TrevorYoung - You are complaining about an image that was added by another user. I just fixed the formatting of the answer and approved the edit. If you find the image unhelpful, then remove it, you are the author of the answer. The addition of the image is an acceptable edit (i.e. the answer is better with the image than without it)

– Ramhound – 2017-12-07T22:08:29.907

1@Ramhound My apologies. Looking at the edit history, I thought that you had added the image, but that's not the case. I have no problem with the image being there, but was trying to give a suggestion to the person who added that it might be better if the given suggestion were done instead. – Trevor Young – 2017-12-08T17:10:02.387

1This is weird but it works... – kayleeFrye_onDeck – 2018-02-21T01:22:53.687

@TrevorYoung +1 for updating with Sort Lines Lexicographically Descending. Pasted in my lines and followed the single step you updated and the list was sorted. – id.ot – 2019-08-10T01:23:15.447

This is brilliant!! – CasperYC – 2020-01-09T22:29:18.630

6

Well, since we are giving code examples, If you are on Windows 7 or you have installed PowerShell on another version of Windows, then:

$foo = New-Object System.collections.arraylist;
$foo.AddRange($(Get-Content 'C:\Path\To\File.txt));
$foo.Reverse();
$foo | Out-File C:\Path\To\File.txt

Or for a non-coding answer, download gVim, open the file and type:

:g/^/m0

EBGreen

Posted 2011-09-02T12:19:53.610

Reputation: 7 834

How can this be integrated into Notepad++? (The title of the question is "Flip or reverse line order in Notepad++".) – Peter Mortensen – 2019-07-14T11:55:23.947

That is why it is a comment rather than an answer. It is an alternative way to accomplish what I thought the user wanted rather than an answer to the literal question. – EBGreen – 2019-08-23T04:45:41.893

I am on Windows 7, Where do i paste that code? this is really intresting. – Danpe – 2011-09-02T13:58:17.347

2Open the start menu and just type Powershell. Double click "Windows Powershell". It will open a command prompt. Run it there. There is also and IDE (although they call it an ISE - Integrated Script Editor), but I do most of my work at the command prompt and I use GVim for script editing. – EBGreen – 2011-09-02T14:04:28.970

5

If you're comfortable compiling C++, this should do the trick. Basically, I put each line of the file in a vector, and output it to a new file by using a reverse iterator.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    std::vector<std::string> fileLines;
    std::string              currLine;
    std::ifstream            inFile("input.txt");
    if (inFile.is_open())
    {
        while (inFile.good())
        {
            std::getline(inFile, currLine);
            fileLines.push_back(currLine);
        }
        inFile.close();
    }
    else
    {
        std::cout << "Error - could not open input file!\n";
        return 1;
    }

    std::ofstream outFile("output.txt");
    if (outFile.is_open())
    {
        std::vector<std::string>::reverse_iterator rIt;
        for (rIt = fileLines.rbegin(); rIt < fileLines.rend(); rIt++)
        {
            outFile << *rIt;
        }
        outFile.close();
    }
    else
    {
        std::cout << "Error - could not open output file!\n";
        return 1;
    }
    return 0;
}

If the output file is missing line breaks between the lines, then change the outFile << *rIt; to be outFile << *rIt << "\r\n"; so a line break is added (omit the \r if you're on Unix/Linux).

Disclaimer: I have not tested this code (I wrote it real quick in Notepad), but it looks viable.

Breakthrough

Posted 2011-09-02T12:19:53.610

Reputation: 32 927

How can this be integrated into Notepad++? (The title of the question is "Flip or reverse line order in Notepad++".) – Peter Mortensen – 2019-07-14T11:46:58.453

Thanks man :) but i allready used my code, but that can be very helpfull for others. Btw maybe i'll wrote a Notepad++ plugin for that. which langauge they use in notepad++ plugins ? – Danpe – 2011-09-02T13:17:36.867

@Danpe most Notepad++ plugins are written in C++, but technically can be extended to any other language capable of compiling a .DLL (and with the proper API bindings). – Breakthrough – 2011-09-02T13:44:06.040

So I think you just created a plugin :) – Danpe – 2011-09-02T13:59:01.840

4

Use online available tool that does it in a click

http://www.miniwebtool.com/reverse-lines/

user674167

Posted 2011-09-02T12:19:53.610

Reputation: 51

How can this be integrated into Notepad++? (The title of the question is "Flip or reverse line order in Notepad++".) – Peter Mortensen – 2019-07-14T11:54:15.813

2

You can do it online on the website http://textmechanic.co/Sort-Text-Lines.html

Franck Dernoncourt

Posted 2011-09-02T12:19:53.610

Reputation: 13 518

1

Website is broken though, it seems it had moved to http://textmechanic.co/Sort-Text-Lines.html

– George Chalhoub – 2015-05-04T18:00:49.470

1@georgechalhoub Thanks, I updated the answer. – Franck Dernoncourt – 2015-05-04T20:39:13.147

How can this be integrated into Notepad++? (The title of the question is "Flip or reverse line order in Notepad++".) – Peter Mortensen – 2019-07-14T11:55:03.770

1

Here is C# .NET code for it I just wrote :)

class Program
{
    static void Main(string[] args)
    {
        try
        {
            String line;
            Stack<String> lines = new Stack<string>();
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("test.txt"))
            {
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                    lines.Push(line);
            }

            // Create a writer and open the file
            TextWriter tw = new StreamWriter("test2.txt");
            // Write a line of text to the file
            while (lines.Count > 0)
                tw.WriteLine(lines.Pop());
            // close the stream
            tw.Close();
        }
        catch (Exception e)
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read/written:");
            Console.WriteLine(e.Message);
        }
    }
}

Danpe

Posted 2011-09-02T12:19:53.610

Reputation: 1 131

1How can we integrate this into notepad++ ? – Teoman shipahi – 2017-02-13T18:41:38.470

How can this be integrated into Notepad++? (The title of the question is "Flip or reverse line order in Notepad++".) – Peter Mortensen – 2019-07-14T11:55:36.787

Why do you need the unsafe keyword on Main here? – Breakthrough – 2011-09-06T19:05:00.967

@Break I dont know how it got there :O lol fixed. – Danpe – 2011-09-07T15:31:41.407

0

If you wish to automate this on Notepad++ with a single click:

  1. Get this Python script plugin
  2. add the code below and save it as .py file in the Python script folder (to know the location of this folder, click plugins tab, then Python Script and new scripts).

All the glory to Reck Dickhard!

To add it in the context menu:

If you click on the plugins tab → Python ScriptConfiguration, you can assign the script either to a toolbar icon, or to the Python Script menu itself. (If you assign a script to the menu, then it will appear immediately, but you will not be able to assign a shortcut to it until next time Notepad++ starts. If you assign it to a toolbar icon, then it will only appear on the next start of Notepad++.)

JinSnow

Posted 2011-09-02T12:19:53.610

Reputation: 486

0

Here is a non-coding way:

  1. Download/Install TextPad free trial
  2. Open a spreadsheet program (ie Excel) and create numbers 1000 through 1500 in column "A" by putting a "1000" in cell A1, then putting A1+1 in cell A2, then copy that down to A500.
  3. Open your text file in TextPad
  4. Change to "block mode" in TextPad
  5. Paste column A from the spreadsheet into TextPad (all will end up on the left margin due to block mode)
  6. Use TextPad sort feature, descending
  7. Use TextPad block mode delete to get rid of the numbers

Dale

Posted 2011-09-02T12:19:53.610

Reputation: 413

How can this be integrated into Notepad++? (The title of the question is "Flip or reverse line order in Notepad++".) – Peter Mortensen – 2019-07-14T11:55:11.520

@PeterMortensen, The original question said "If it's not available in Notepad++ then other software whould be good also :)" – Dale – 2019-07-22T15:19:22.483

2well if you are going to use excel anyway, just copy the file contents from any editor, paste to excel. Fill series the next column over with a series of numbers. Select both columns and sort descending on the number column. Copy the text column and paste back to the editor. – EBGreen – 2011-09-02T15:15:56.077

@EBGreen, you are correct. I'm an idiot. You just need to be careful because Excel will change your data (messes with numbers, dates and such). – Dale – 2011-09-05T12:27:06.310