• Login
Community
  • Login

Insert sequential numbers at start of lines

Scheduled Pinned Locked Moved General Discussion
32 Posts 6 Posters 15.7k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A
    Alan Kilborn @deleelee
    last edited by Aug 22, 2022, 1:09 PM

    @deleelee said in Insert sequential numbers at start of lines:

    but exclude any selected lines that are empty?

    Sure, most anything is possible with programming.
    You’d want to look for a line-length of zero (e.g. len(line_content) == 0) and then NOT insert a line number at the start of such lines, by appending only the content to the second list. Of course, this would cause gaps in your line numbering any time a blank line occurs, so you might need to maintain the line number counter yourself.

    Since you’ve already made changes to the script, go ahead and make some more! :-)

    D 2 Replies Last reply Aug 24, 2022, 3:53 AM Reply Quote 0
    • D
      deleelee @Alan Kilborn
      last edited by Aug 24, 2022, 3:53 AM

      @Alan-Kilborn said in Insert sequential numbers at start of lines:

      You’d want to look for a line-length of zero (e.g. len(line_content) == 0) and then NOT insert a line number at the start of such lines, by appending only the content to the second list. Of course, this would cause gaps in your line numbering any time a blank line occurs, so you might need to maintain the line number counter yourself.

      Thanks yet again. I’ll check it out :-D

      1 Reply Last reply Reply Quote 1
      • I
        Ionut Dorin @Alan Kilborn
        last edited by Jun 2, 2024, 11:11 AM

        @Alan-Kilborn I know it’s years later, but i found myself using your script. I’m a total noob when it comes to programming, all i could do is modify what goes after the numbers.
        Now, how could i delete what the script just did (i.e delete the numbers, but leave the lines like they were before the script ran) ? I know i can use Undo to do it on the spot, but let’s say i wanna undo the numbering a day after.

        A 1 Reply Last reply Jun 2, 2024, 12:24 PM Reply Quote 0
        • A
          Alan Kilborn @Ionut Dorin
          last edited by Jun 2, 2024, 12:24 PM

          @Ionut-Dorin

          You don’t need programming to delete the line numbers that were inserted.

          Here’s how to do it:

          • Move the caret to line 1 and column 1
          • Press Shift+Alt+b to Begin Select in Column mode
          • Move the caret to the last line and put it right after the last digit of the line number (previously added by the script)
          • Press Shift+Alt+b again to End Select in Column mode

          At this point you should have the line numbers selected in a rectangular block so simply pressing Delete should eliminate them.

          I 1 Reply Last reply Jun 2, 2024, 4:00 PM Reply Quote 0
          • I
            Ionut Dorin @Alan Kilborn
            last edited by Jun 2, 2024, 4:00 PM

            @Alan-Kilborn Okay, i knew how to do that, but it does not help me.
            First, i’d have to do it twice, because i set the script up to put a number, then :, then a space (5: ), so till 9 its 3 characters long and after is 4 characters long.
            And again, i have to do this multiple times in a document, every day and using the column mode is a bit tedious. I was hoping that i could do it with a script.
            Now, i select the text i need, press a key and your script instantly does its job. I’d like to have the same thing with deleting, but i don’t know if it’s possible or not. :)

            A 1 Reply Last reply Jun 2, 2024, 5:27 PM Reply Quote 0
            • A
              Alan Kilborn @Ionut Dorin
              last edited by Jun 2, 2024, 5:27 PM

              @Ionut-Dorin said in Insert sequential numbers at start of lines:

              i set the script up to put a number, then :, then a space (5: ), so till 9 its 3 characters long and after is 4 characters long

              It would have been better to just show some of this text rather than describing it…

              But I think if you do a replacement operation it will be good?:

              Find: ^\d+:
              Replace: set to nothing
              Search mode: Regular expression
              Options: Wrap around
              Action: Replace all

              You can record this replacement op as a macro.

              I 1 Reply Last reply Jun 2, 2024, 5:40 PM Reply Quote 1
              • I
                Ionut Dorin @Alan Kilborn
                last edited by Ionut Dorin Jun 2, 2024, 5:42 PM Jun 2, 2024, 5:40 PM

                @Alan-Kilborn
                1: Line content
                2: Line content
                3: Line content
                4: Line content
                5: Line content
                6: Line content
                7: Line content
                8: Line content
                9: Line content
                10: Line content
                11: Line content
                12: Line content
                … and to become
                Line content
                Line content
                Line content
                Line content
                etc.
                Find: ^\d+: doesn’t work and plus, i don’t need it for the whole document, just for the lines i select.

                A 1 Reply Last reply Jun 2, 2024, 7:15 PM Reply Quote 0
                • A
                  Alan Kilborn @Ionut Dorin
                  last edited by Jun 2, 2024, 7:15 PM

                  @Ionut-Dorin said in Insert sequential numbers at start of lines:

                  Find: ^\d+: doesn’t work

                  If you did exactly what I said to do, I see no reason why it wouldn’t work on your sample data.

                  i don’t need it for the whole document, just for the lines i select.

                  Ok, so don’t checkmark Wrap around but make your selection first and then checkmark In selection.

                  I 1 Reply Last reply Jun 2, 2024, 9:02 PM Reply Quote 2
                  • I
                    Ionut Dorin @Alan Kilborn
                    last edited by Ionut Dorin Jun 2, 2024, 9:02 PM Jun 2, 2024, 9:02 PM

                    @Alan-Kilborn Yes, that did it, I think i read too fast and didn’t see that you said “Search mode: Regular expression” and i didn’t check that, so maybe that’s why it didnt work.
                    Also, i turned it into a macro for ease of use.
                    Thank you so much for your help and patience, you’re a lifesaver.

                    1 Reply Last reply Reply Quote 2
                    • D
                      deleelee @Alan Kilborn
                      last edited by Dec 11, 2024, 3:41 AM

                      @Alan-Kilborn said in Insert sequential numbers at start of lines:

                      Sure, most anything is possible with programming.

                      Hi Alan, the scripts have been working really well, thank you so much for your help. One thing I’ve noticed is that, when using the script for selected lines, it has to be a block selection for the script to work. I can’t select individual lines (eg: line 1, line 4, line 7) and insert numbers at that the start of only those lines.

                      I’ve tried:

                      1. Multi-selection using Ctrl + single-click at the start of each line.
                      2. Multi-selection using Ctrl + single-click at the start and end of each line.
                      3. Multi-selection using Ctrl + double-click to select the first word of each line.

                      Numbers 1 & 2 don’t actually select any text so when I run the script I get a “No text selected” message.
                      Number 3 only places a number at the start of the line for the last word I select.

                      Is it possible or am I expecting more than the script can achieve?

                      A 1 Reply Last reply Dec 11, 2024, 12:05 PM Reply Quote 0
                      • A
                        Alan Kilborn @deleelee
                        last edited by Alan Kilborn Dec 11, 2024, 12:49 PM Dec 11, 2024, 12:05 PM

                        @deleelee

                        You must be referring to the TextFxInsertLineNumbers.py script, because the other script discussed in this topic, InsertLineNumbersOnLines.py, doesn’t deal with any selected text at all.

                        Here’s a modified version of the script I’m calling TextFxInsertLineNumbers2.py :

                        # -*- coding: utf-8 -*-
                        from __future__ import print_function
                        
                        # references:
                        #  https://community.notepad-plus-plus.org/topic/23206/insert-sequential-numbers-at-start-of-lines
                        #  https://community.notepad-plus-plus.org/topic/23166/faq-desk-how-do-i-replicate-the-features-of-textfx
                        
                        from Npp import *
                        
                        #-------------------------------------------------------------------------------
                        
                        class TFILN2(object):
                        
                            def __init__(self):
                                handled_line_nbr_list = []
                                running_line_nbr = 1
                                line_tup_list = []
                                for s in range(editor.getSelections()):
                                    start_line = editor.lineFromPosition(editor.getSelectionNStart(s))
                                    end_line = editor.lineFromPosition(editor.getSelectionNEnd(s))
                                    line_tup_list.append((start_line, end_line))
                                    line_tup_list.sort()
                                for (start_line, end_line) in line_tup_list:
                                    for line_nbr in range(start_line, end_line + 1):
                                        if line_nbr not in handled_line_nbr_list:
                                            line_content = editor.getLine(line_nbr).rstrip('\n\r')
                                            editor.replaceLine(line_nbr, '{n:08} {c}'.format(n=running_line_nbr, c=line_content))
                                            running_line_nbr += 1
                                            handled_line_nbr_list.append(line_nbr)
                                editor.setSel(editor.getCurrentPos(), editor.getCurrentPos())
                        
                        #-------------------------------------------------------------------------------
                        
                        if __name__ == '__main__': TFILN2()
                        

                        This version handles all types of selections.

                        D 2 Replies Last reply Dec 13, 2024, 9:30 AM Reply Quote 2
                        • D
                          deleelee @Alan Kilborn
                          last edited by Dec 13, 2024, 9:30 AM

                          @Alan-Kilborn said in Insert sequential numbers at start of lines:

                          This version handles all types of selections.

                          Perfect!!! Thank you so much 😁👍

                          1 Reply Last reply Reply Quote 0
                          • D
                            deleelee @Alan Kilborn
                            last edited by deleelee May 16, 2025, 5:40 AM May 16, 2025, 5:39 AM

                            @Alan-Kilborn said in Insert sequential numbers at start of lines:

                            # -*- coding: utf-8 -*-
                            from __future__ import print_function
                            
                            # references:
                            #  https://community.notepad-plus-plus.org/topic/23206/insert-sequential-numbers-at-start-of-lines
                            #  https://community.notepad-plus-plus.org/topic/23166/faq-desk-how-do-i-replicate-the-features-of-textfx
                            
                            from Npp import *
                            
                            #-------------------------------------------------------------------------------
                            
                            class TFILN2(object):
                            
                                def __init__(self):
                                    handled_line_nbr_list = []
                                    running_line_nbr = 1
                                    line_tup_list = []
                                    for s in range(editor.getSelections()):
                                        start_line = editor.lineFromPosition(editor.getSelectionNStart(s))
                                        end_line = editor.lineFromPosition(editor.getSelectionNEnd(s))
                                        line_tup_list.append((start_line, end_line))
                                        line_tup_list.sort()
                                    for (start_line, end_line) in line_tup_list:
                                        for line_nbr in range(start_line, end_line + 1):
                                            if line_nbr not in handled_line_nbr_list:
                                                line_content = editor.getLine(line_nbr).rstrip('\n\r')
                                                editor.replaceLine(line_nbr, '{n:08} {c}'.format(n=running_line_nbr, c=line_content))
                                                running_line_nbr += 1
                                                handled_line_nbr_list.append(line_nbr)
                                    editor.setSel(editor.getCurrentPos(), editor.getCurrentPos())
                            
                            #-------------------------------------------------------------------------------
                            
                            if __name__ == '__main__': TFILN2()
                            

                            This version handles all types of selections.

                            Hi Alan, I absolutely love this script, thank you, and I use it regularly. How would I adapt it so that instead of inserting sequential numbers, it inserts sequential letters? ie: lines begin with a) b) c) etc.

                            A 1 Reply Last reply May 16, 2025, 12:01 PM Reply Quote 0
                            • A
                              Alan Kilborn @deleelee
                              last edited by May 16, 2025, 12:01 PM

                              @deleelee said in Insert sequential numbers at start of lines:

                              How would I adapt it so that instead of inserting sequential numbers, it inserts sequential letters? ie: lines begin with a) b) c) etc

                              Well, this is “dicey” as you quickly run out of letters.
                              Typically, you only have a-z, i.e., 26.

                              So, if you’re only going to run the script on very small files, you could hack the line_list2.append( line to be:

                              line_list2.append('{n}) {c}'.format(n=chr(97+line_nbr), c=line_content))
                              

                              But note, this is a real hack, as there’s no check for too many lines.

                              D 1 Reply Last reply May 19, 2025, 1:24 AM Reply Quote 2
                              • Mark OlsonM
                                Mark Olson
                                last edited by Mark Olson May 16, 2025, 9:00 PM May 16, 2025, 8:59 PM

                                If you wanted to not run out of letters, by simply increasing the number of letters by 1 every time to get to z (thus aa would follow z and aaa would follow zz), you could use the code:

                                num_chars, offset = divmod(line_nbr, 26)
                                line_list2.append( ( ( num_chars + 1 ) * chr(offset + 97) ) + line_content)
                                

                                Note that I haven’t actually executed this code as written.

                                1 Reply Last reply Reply Quote 2
                                • D
                                  deleelee @Alan Kilborn
                                  last edited by deleelee May 19, 2025, 1:33 AM May 19, 2025, 1:24 AM

                                  @Alan-Kilborn said in Insert sequential numbers at start of lines:

                                  you could hack the line_list2.append( line to be:

                                  line_list2.append('{n}) {c}'.format(n=chr(97+line_nbr), c=line_content))
                                  

                                  Thanks Alan. That had me confused until I realised you weren’t referring to the script I quoted which was TextFxInsertLineNumbers2.py and it doesn’t have line_list2.append. Is it possible to adapt that script? The one you’ve referred to seems to be TextFxInsertLineNumbers.py and I don’t use that.

                                  A 1 Reply Last reply May 19, 2025, 10:42 AM Reply Quote 0
                                  • A
                                    Alan Kilborn @deleelee
                                    last edited by May 19, 2025, 10:42 AM

                                    @deleelee

                                    the script I quoted which was TextFxInsertLineNumbers2.py

                                    Hmm, you’re right, I guess I ignored the script you quoted, and just looked in this thread for the first script presented. Sorry.

                                    Anyway, that script has a similar line:

                                    editor.replaceLine(line_nbr, '{n:08} {c}'.format(n=running_line_nbr, c=line_content))

                                    and I’d think it wouldn’t be that hard to adapt that line from what I gave before, into:

                                    editor.replaceLine(line_nbr, '{n}) {c}'.format(n=chr(96+running_line_nbr), c=line_content))

                                    D 1 Reply Last reply May 28, 2025, 1:41 AM Reply Quote 1
                                    • D
                                      deleelee @Alan Kilborn
                                      last edited by May 28, 2025, 1:41 AM

                                      @Alan-Kilborn said in Insert sequential numbers at start of lines:

                                      editor.replaceLine(line_nbr, '{n}) {c}'.format(n=chr(96+running_line_nbr), c=line_content))

                                      Thank you! I had tried editing that line myself but I didn’t quite get it right. It’s all good now thanks to you 😁

                                      1 Reply Last reply Reply Quote 1
                                      • First post
                                        Last post
                                      The Community of users of the Notepad++ text editor.
                                      Powered by NodeBB | Contributors