Community
    • Login

    Copying the current line number to the clipboard

    Scheduled Pinned Locked Moved General Discussion
    9 Posts 4 Posters 350 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.
    • Alan KilbornA
      Alan Kilborn
      last edited by Alan Kilborn

      Notepad++ 8.7.6 introduces a way to copy the currently active line number to the clipboard, see some discussion HERE, but it might be considered a bit “klunky” for those that need to do this often.

      As a faster alternative, I’ll offer up this method:

      Create a .bat file in a reasonable place in your file system. I named mine copy_current_line_number.bat and for purposes of the rest of this technique I’ll use C:\batch\copy_current_line_number.bat as its location.

      The batch file contents should be:

      :: see https://community.notepad-plus-plus.org/topic/26585
      :: Notepad++ Run menu > Run... > The Program to Run: entry:
      :: cmd /c C:\batch\copy_current_line_number.bat $(CURRENT_LINE)
      @echo off
      set CURR_LINE_MINUS_1=%1
      set /a CURR_LINE = %CURR_LINE_MINUS_1% + 1
      :: echo the variable without putting a line-ending on it:
      echo|set /p=%CURR_LINE%|clip
      

      As the comments in the script indicate, go to the Notepad++ Run menu and choose Run… and then put the following in The Program to Run: box: cmd /c C:\batch\copy_current_line_number.bat $(CURRENT_LINE) (changing your pathname to the batch file as appropriate for you).
      Then press Run.

      Save the entry (go back in like you want to run it again but choose the Save button this time) so that it appears on the Run menu in the future; I called mine Copy current line number, but… as you like.
      Optionally (but recommended) assign a shortcut keycombo to it, for fast access.


      LATER EDIT: I added a comment into the script:
      :: echo the variable without putting a line-ending on it:

      mkupperM 1 Reply Last reply Reply Quote 2
      • guy038G
        guy038
        last edited by guy038

        Hello @alan-kilborn and All,

        Here is a variant of your batch script which should work, too :

        :: see https://community.notepad-plus-plus.org/topic/26585
        :: Notepad++ Run menu > Run... > The Program to Run: entry:
        :: cmd /c C:\batch\copy_current_line_number.bat $(CURRENT_LINE)
        
        @echo OFF
        set LINE=%1
        set /a LINE += 1
        echo %LINE% | clip
        

        Best Regards,

        guy038

        Alan KilbornA 1 Reply Last reply Reply Quote 1
        • guy038G guy038 referenced this topic on
        • Alan KilbornA
          Alan Kilborn @guy038
          last edited by Alan Kilborn

          @guy038 said:

          variant of your batch script

          Comments on the variant:

          • the original emphasizes that what N++ sends is one less than the current line value; the variant deemphasizes that nuance by reusing the same variable name for a different purpose
          • the variant copies the current line value PLUS a CRLF to the clipboard; the original copies only the text of the line number (no trailing CRLF) – probably the original could have had a comment about this and the seemingly odd set /p command (but it is a common batch idiom to me, so I didn’t add a comment originally – I did with a “LATER EDIT”)
          1 Reply Last reply Reply Quote 3
          • mkupperM
            mkupper @Alan Kilborn
            last edited by

            @Alan-Kilborn said in Copying the current line number to the clipboard:

            Notepad++ 8.7.6 introduces a way to copy the currently active line number to the clipboard, see some discussion HERE, but it might be considered a bit “klunky” for those that need to do this often.

            I do it often via

            #
            # This is used to fetch the current line number into the copy/paste buffer
            #
            # See https://community.notepad-plus-plus.org/topic/20224
            #
            editor.copyText(str(editor.lineFromPosition(editor.getCurrentPos())+1))
            

            As Ctrl+G is Notepad++'s goto-line thing I added a shortcut for Ctrl+Shift+G that is mapped to that script.

            The source for this script plus notes on how to add it to Notepad++ are in the Is there a way to get the current line number into the copy/paste buffer? forum thread.

            Alan KilbornA 1 Reply Last reply Reply Quote 2
            • Alan KilbornA
              Alan Kilborn @mkupper
              last edited by

              @mkupper

              Of course it is easily scriptable, but it doesn’t have to be, as I show with the batch file approach.

              I believe readers that aren’t already into scripting are reluctant to get involved for one reason or another.

              The batch file also has some baggage as it requires a separate file to be kept somewhere in the file system, but it doesn’t require a plugin.

              mpheathM 1 Reply Last reply Reply Quote 1
              • mpheathM
                mpheath @Alan Kilborn
                last edited by

                @Alan-Kilborn

                Another alternative is to use only the commandline:

                cmd /v:on /s /c "set /a line=$(CURRENT_LINE)+1 & <nul set /p "=!line!"|clip"
                

                Add as a Run entry. No batch file dependency.

                Alan KilbornA 1 Reply Last reply Reply Quote 4
                • Alan KilbornA
                  Alan Kilborn @mpheath
                  last edited by

                  @mpheath said:

                  Add as a Run entry. No batch file dependency.

                  Well…nice.
                  A bit too much Batch file “wizardry” there for me, but OK.
                  I like wizardry (of course) but only to a point. :-)

                  I’m OK with having an extra file kicking around, as I have a directory with several N++ related batch files already.

                  mkupperM 1 Reply Last reply Reply Quote 2
                  • mkupperM
                    mkupper @Alan Kilborn
                    last edited by mkupper

                    @Alan-Kilborn said in Copying the current line number to the clipboard:

                    A bit too much Batch file “wizardry” there for me, but OK.
                    I like wizardry (of course) but only to a point. :-)

                    It’s much like regexp wizardry.

                    Something I learned from @mpheath’s batch file wizardry is that I had not known that when in ENABLEDELAYEDEXPANSION mode that you are allowed to set or change variable and that the new value for that variable is available later in the same line.

                    As a standalone SET /A outputs the result of the expression we can shorten it up to

                    cmd /c "set /a line=$(CURRENT_LINE)+1 | clip"
                    

                    Which also gets the topic of this thread back onto Notepad++. :-)

                    Alan KilbornA 1 Reply Last reply Reply Quote 2
                    • Alan KilbornA
                      Alan Kilborn @mkupper
                      last edited by

                      @mkupper said :

                      Which also gets the topic of this thread back onto Notepad++

                      It never left.

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