• Login
Community
  • Login

Copying the current line number to the clipboard

Scheduled Pinned Locked Moved General Discussion
9 Posts 4 Posters 357 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
    last edited by Alan Kilborn Jan 27, 2025, 3:23 PM Jan 27, 2025, 12:18 PM

    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:

    M 1 Reply Last reply Jan 28, 2025, 10:01 PM Reply Quote 2
    • G
      guy038
      last edited by guy038 Jan 27, 2025, 2:35 PM Jan 27, 2025, 2:28 PM

      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

      A 1 Reply Last reply Jan 27, 2025, 3:14 PM Reply Quote 1
      • G guy038 referenced this topic on Jan 27, 2025, 2:28 PM
      • A
        Alan Kilborn @guy038
        last edited by Alan Kilborn Jan 27, 2025, 3:23 PM Jan 27, 2025, 3:14 PM

        @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
        • M
          mkupper @Alan Kilborn
          last edited by Jan 28, 2025, 10:01 PM

          @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.

          A 1 Reply Last reply Jan 29, 2025, 1:26 AM Reply Quote 2
          • A
            Alan Kilborn @mkupper
            last edited by Jan 29, 2025, 1:26 AM

            @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.

            M 1 Reply Last reply Jan 29, 2025, 3:30 AM Reply Quote 1
            • M
              mpheath @Alan Kilborn
              last edited by Jan 29, 2025, 3:30 AM

              @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.

              A 1 Reply Last reply Jan 29, 2025, 11:21 AM Reply Quote 4
              • A
                Alan Kilborn @mpheath
                last edited by Jan 29, 2025, 11:21 AM

                @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.

                M 1 Reply Last reply Jan 30, 2025, 6:11 PM Reply Quote 2
                • M
                  mkupper @Alan Kilborn
                  last edited by mkupper Jan 30, 2025, 6:15 PM Jan 30, 2025, 6:11 PM

                  @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++. :-)

                  A 1 Reply Last reply Jan 30, 2025, 7:01 PM Reply Quote 2
                  • A
                    Alan Kilborn @mkupper
                    last edited by Jan 30, 2025, 7:01 PM

                    @mkupper said :

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

                    It never left.

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