Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Insert sequential numbers at start of lines

    General Discussion
    4
    12
    290
    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.
    • deleelee
      deleelee last edited by

      I know how to use the Column Editor to insert sequential numbers at the start of lines but is it possible to create a macro for this? I’ve tried doing the following:

      1. Move cursor to start position.
      2. Start Recording macro.
      3. Press Alt+C to open Column Editor.
      4. Enter 1 for initial number, 1 for increase, click OK.
      5. Stop Recording macro.

      The macro records but when I select Playback nothing happens.

      PeterJones Alan Kilborn 2 Replies Last reply Reply Quote 0
      • PeterJones
        PeterJones @deleelee last edited by

        @deleelee ,

        The macro language is not all-capable, unfortunately. The only dialog-based action which it can record is search/replace – and since the Column Editor is a dialog that isn’t the search/replace, the macro cannot record/replicate its action

        deleelee 1 Reply Last reply Reply Quote 2
        • deleelee
          deleelee @PeterJones last edited by

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

          @deleelee ,

          The macro language is not all-capable, unfortunately. The only dialog-based action which it can record is search/replace – and since the Column Editor is a dialog that isn’t the search/replace, the macro cannot record/replicate its action

          I thought that might be the case so thanks for confirming it for me :-)

          1 Reply Last reply Reply Quote 0
          • Simran Kaur
            Simran Kaur last edited by

            Follow this tutorial: https://community.notepad-plus-plus.org/topic/16311/how-to-add-numbers-to-a-text-with-notepad

            deleelee 1 Reply Last reply Reply Quote 0
            • Alan Kilborn
              Alan Kilborn @deleelee last edited by

              @deleelee

              While it may not be possible as a macro, it could certainly be “scripted” if you are interested in that type of solution.

              deleelee 1 Reply Last reply Reply Quote 0
              • deleelee
                deleelee @Simran Kaur last edited by

                @Simran-Kaur said in Insert sequential numbers at start of lines:

                Follow this tutorial: https://community.notepad-plus-plus.org/topic/16311/how-to-add-numbers-to-a-text-with-notepad

                I don’t think you read my post because I’ve already said I know how to do that.

                1 Reply Last reply Reply Quote 0
                • deleelee
                  deleelee @Alan Kilborn last edited by

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

                  @deleelee

                  While it may not be possible as a macro, it could certainly be “scripted” if you are interested in that type of solution.

                  Yes I would be interested in that type of solution. Do you have a script in mind?

                  Alan Kilborn 1 Reply Last reply Reply Quote 0
                  • Alan Kilborn
                    Alan Kilborn @deleelee last edited by Alan Kilborn

                    @deleelee

                    You didn’t really say a whole lot about what you want to end up with, but here’s a script that demos some capability. I call it InsertLineNumbersOnLines.py:

                    # -*- coding: utf-8 -*-
                    from __future__ import print_function
                    
                    from Npp import *
                    import math
                    
                    #-------------------------------------------------------------------------------
                    
                    class ILNOL(object):
                    
                        def __init__(self):
                            num_digits_in_line_num = int(math.log(editor.getLineCount(), 10)) + 1
                            line_list = editor.getText().splitlines()
                            line_list2 = []
                            for (line_nbr, line_content) in enumerate(line_list):
                                line_list2.append('{n:>{d}}:{c}'.format(n=line_nbr+1, c=line_content, d=num_digits_in_line_num))
                            eol = ['\r\n', '\r', '\n'][editor.getEOLMode()]
                            editor.setText(eol.join(line_list2))
                    
                    #-------------------------------------------------------------------------------
                    
                    if __name__ == '__main__': ILNOL()
                    
                    

                    The script will put line numbers at the start of every line, example:

                    9ad2d72d-ec3d-4394-b6f0-2878cdc1800e-image.png

                    1 Reply Last reply Reply Quote 1
                    • Alan Kilborn
                      Alan Kilborn last edited by Alan Kilborn

                      It may be worth noting that the script above is similar to the functionality of the deprecated TextFX plugin’s function: TextFX > TextFX Tools > Insert Line Numbers.

                      Here’s a slightly modified version that is even more similar, I call this one TextFxInsertLineNumbers.py:

                      # -*- coding: utf-8 -*-
                      from __future__ import print_function
                      
                      from Npp import *
                      import inspect
                      import os
                      
                      #-------------------------------------------------------------------------------
                      
                      class TFILN(object):
                      
                          def __init__(self):
                              self.this_script_name = inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
                              if editor.getSelectionEmpty():
                                  self.mb('No text selected')
                                  return
                              (start_line, end_line) = editor.getUserLineSelection()
                              start_pos = editor.positionFromLine(start_line)
                              end_pos = editor.getLineEndPosition(end_line)
                              line_list = editor.getRangePointer(start_pos, end_pos - start_pos).splitlines()
                              line_list2 = []
                              for (line_nbr, line_content) in enumerate(line_list):
                                  line_list2.append('{n:08} {c}'.format(n=line_nbr+1, c=line_content))
                              eol = ['\r\n', '\r', '\n'][editor.getEOLMode()]
                              editor.setTarget(start_pos, end_pos)
                              editor.beginUndoAction()
                              editor.replaceTarget(eol.join(line_list2))
                              editor.endUndoAction()
                      
                          def mb(self, msg, flags=0, title=''):  # a message-box function
                              return notepad.messageBox(msg, title if title else self.this_script_name, flags)
                      
                      #-------------------------------------------------------------------------------
                      
                      if __name__ == '__main__': TFILN()
                      
                      deleelee 1 Reply Last reply Reply Quote 0
                      • deleelee
                        deleelee @Alan Kilborn last edited by

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

                        It may be worth noting that the script above is similar to the functionality of the deprecated TextFX plugin’s function: TextFX > TextFX Tools > Insert Line Numbers.

                        Here’s a slightly modified version that is even more similar, I call this one TextFxInsertLineNumbers.py:

                        # -*- coding: utf-8 -*-
                        from __future__ import print_function
                        
                        from Npp import *
                        import inspect
                        import os
                        
                        #-------------------------------------------------------------------------------
                        
                        class TFILN(object):
                        
                            def __init__(self):
                                self.this_script_name = inspect.getframeinfo(inspect.currentframe()).filename.split(os.sep)[-1].rsplit('.', 1)[0]
                                if editor.getSelectionEmpty():
                                    self.mb('No text selected')
                                    return
                                (start_line, end_line) = editor.getUserLineSelection()
                                start_pos = editor.positionFromLine(start_line)
                                end_pos = editor.getLineEndPosition(end_line)
                                line_list = editor.getRangePointer(start_pos, end_pos - start_pos).splitlines()
                                line_list2 = []
                                for (line_nbr, line_content) in enumerate(line_list):
                                    line_list2.append('{n:08} {c}'.format(n=line_nbr+1, c=line_content))
                                eol = ['\r\n', '\r', '\n'][editor.getEOLMode()]
                                editor.setTarget(start_pos, end_pos)
                                editor.beginUndoAction()
                                editor.replaceTarget(eol.join(line_list2))
                                editor.endUndoAction()
                        
                            def mb(self, msg, flags=0, title=''):  # a message-box function
                                return notepad.messageBox(msg, title if title else self.this_script_name, flags)
                        
                        #-------------------------------------------------------------------------------
                        
                        if __name__ == '__main__': TFILN()
                        

                        Thanks for both of these. Sorry I wasn’t more specific. What would be great would be a script where I just select the lines I want numbered and insert sequential numbers starting from one followed by a closing parentheses ie: 1) 2) 3)

                        Now I’m not exactly a coding newbie but I’ve not really dealt with Python before. If I have Python installed on my PC do I just run the script or do I load it into Npp (as a macro or some other method)?

                        PeterJones 1 Reply Last reply Reply Quote 0
                        • PeterJones
                          PeterJones @deleelee last edited by

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

                          f I have Python installed on my PC do I just run the script or do I load it into Npp

                          No. You install the PythonScript plugin, which comes with an embedded Python interpreter, and follow the instructions in the FAQ on how to run a script using PythonScript

                          deleelee 1 Reply Last reply Reply Quote 0
                          • deleelee
                            deleelee @PeterJones last edited by

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

                            No. You install the PythonScript plugin, which comes with an embedded Python interpreter, and follow the instructions in the FAQ on how to run a script using PythonScript

                            Thank you so much, Peter! :-D

                            1 Reply Last reply Reply Quote 0
                            • Referenced by  PeterJones PeterJones 
                            • First post
                              Last post
                            Copyright © 2014 NodeBB Forums | Contributors