Community
    • Login

    posting code (by copy & paste) with line numbers

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    copy and pasteline number
    18 Posts 6 Posters 546 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.
    • goolyG
      gooly
      last edited by

      I want to publish code snippets in a forum and refer to them in the explanatory text. It would be nice if I could copy the code snippets with the respective line numbers - is that possible?
      If so, how?

      Alan KilbornA goolyG Lycan ThropeL Jim DaileyJ 4 Replies Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @gooly
        last edited by

        @gooly

        d3eda579-c51d-467e-9483-be5840fb17a5-image.png

        1 Reply Last reply Reply Quote 1
        • goolyG
          gooly @gooly
          last edited by

          @goolyThank you, but this does not work with copy (Ctrl+C) & paste (Ctrl+V) :(
          Any solution for this?

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

            @gooly said in posting code (by copy & paste) with line numbers:

            @goolyThank you, but this does not work with copy (Ctrl+C) & paste (Ctrl+V) :(

            The line numbers aren’t part of the text, so Notepad++ doesn’t have them available for copy/paste with the text normally.

            In theory, one could ask for that as a feature (but I doubt it’s likely to be implemented); as a second alternative, one could write a script in a scripting plugin (like PythonScript), which would do the copy then insert the line numbers, or someone in theory could write a full plugin to accomplish this (maybe some other plugin already has copy-with-line-number, but I don’t know of it); but if you are willing to do a multi-step process, it can be done in native Notepad++:

            1. One Time Setup: for a normal installation of Notepad++,
              1. File > Exit all copies of Notepad++, then run one copy of Notepad++.
              2. File > Open: %AppData%\Notepad++\shortcuts.xml
              3. Just before </Macros>, add:
                <Macro name="NumberLines" Ctrl="no" Alt="no" Shift="no" Key="0">
                    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1601" wParam="0" lParam="0" sParam="^" />
                    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                    <Action type="3" message="1602" wParam="0" lParam="0" sParam=" | " />
                    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
                    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                    <Action type="0" message="2316" wParam="0" lParam="0" sParam="" />
                    <Action type="2" message="0" wParam="42089" lParam="0" sParam="" />
                    <Action type="0" message="2318" wParam="0" lParam="0" sParam="" />
                    <Action type="0" message="2312" wParam="0" lParam="0" sParam="" />
                    <Action type="2" message="0" wParam="42089" lParam="0" sParam="" />
                    <Action type="2" message="0" wParam="42034" lParam="0" sParam="" />
                </Macro>
                <Macro name="UnNumber Lines" Ctrl="no" Alt="no" Shift="no" Key="0">
                    <Action type="3" message="1700" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1601" wParam="0" lParam="0" sParam="^\h*\d+ \| " />
                    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                    <Action type="3" message="1602" wParam="0" lParam="0" sParam="" />
                    <Action type="3" message="1702" wParam="0" lParam="768" sParam="" />
                    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                </Macro>
                
                and save
              4. File > Exit, then run one copy of Notepad++.

            From now on, you have two new macros in the Macro menu: Number Lines and UnNumber Lines (and, if you want, you can use Macro > Modify Shortcut/Delete Macro to launch the Shortcut Mapper to add a keyboard shortcut for running these macros)

            Every time you want to copy some or all of your code with the line number:

            1. Run Macro > Number Lines, which will add a | at the beginning of every line, then launch the Column Editor
            2. Fill out the Column Editor as follows:
              • ⦿ Number to Insert,
              • Initial Number: 1
              • Increase by: 1
              • **Repeat: ** (leave blank)
              • Leaing: Spaces
              • Format = ⦿ Dec
              • Click OK
                0488daf0-d3b0-4830-bf4b-0ba63190eab2-image.png
                (it will remember these values for next time)
            3. Do your copy of the code (some or all lines, as you desire)
            4. When done copying code-with-line-numbers, run Macro > UnNumber Lines to remove the line prefixes
            1 Reply Last reply Reply Quote 2
            • Lycan ThropeL
              Lycan Thrope @gooly
              last edited by Lycan Thrope

              @gooly ,
              The suggestion by @Alan-Kilborn is needed, but there is a simpler way other than what @PeterJones suggests. I just did it.

              Print to a PDF printer. I just tried both an old program of mine from Scansoft PDF Create!, and I also tried the stock Microsoft Print to PDF, and they printed to a file. I then copied and pasted the relevant code with numbers to a Notepad.exe file, and the numbers and code were there, or you could even just recopy into a .txt document in Notepad++. Either way, once you have the code and numbers in a pdf document, you can just Copy & Paste from that document as you need.

              Here’s an example of the text I copied from the .pdf file and pasted in Notepad++:

              PrintLineNumbersInDocument.PNG

              Just a caveat…if you code in Dark Mode, make sure your coloring is set for Light Mode, otherwise, it will be illegible on a white background in the .pdf file. As you can see, the syntax highlighting also seems to have been copied over from the .pdf file into Notepad++ as well in this default new1 document.

              Enjoy.

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

                @Lycan-Thrope

                Your solution comes with some caveats, at least for me.

                You implied after printing that the next step is to open the PDF document created. The ability to copy from this document will depend upon the program opening the PDF, but I suppose it is usually the case that copying will be allowed. (For me it wasn’t, but that’s because I use Irfanview as my PDF viewer; doing an “open-with” with Chrome did allow copy-from with that source.)

                Another thing I noticed is that any wrapped lines were rendered with hard line breaks at the wrap point. Not a problem per se, but something to note.

                I did not replicate your experience with syntax coloring being copied over.

                Lycan ThropeL 1 Reply Last reply Reply Quote 1
                • Lycan ThropeL
                  Lycan Thrope @Alan Kilborn
                  last edited by

                  @Alan-Kilborn said in posting code (by copy & paste) with line numbers:

                  Your solution comes with some caveats, at least for me.

                  You implied after printing that the next step is to open the PDF document created. The ability to copy from this document will depend upon the program opening the PDF, but I suppose it is usually the case that copying will be allowed. (For me it wasn’t, but that’s because I use Irfanview as my PDF viewer; doing an “open-with” with Chrome did allow copy-from with that source.)

                  Yeah, I guess I should have thought of that, as I’m using the Adobe Acrobat program, which is free, so I figured everyone must be using it, my bad.

                  Another thing I noticed is that any wrapped lines were rendered with hard line breaks at the wrap point. Not a problem per se, but something to note.

                  Good catch. I tend, as I’ve mentioned before, that I try to stay within the 80 character mark, which is why you see my screenshot showing my marker set at the 80 character mark. (Green line) Most of my text is under 80, or continued in dBASE Plus with ; semi-colons.

                  I did not replicate your experience with syntax coloring being copied over.

                  Good point also, and I just checked by duplicating that copy/paste and it appears it defaults to the D language color coding for some reason. Now we both know more. :-)

                  Alan KilbornA 1 Reply Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn @Lycan Thrope
                    last edited by

                    @Lycan-Thrope said:

                    I just checked by duplicating that copy/paste and it appears it defaults to the D language color coding

                    What are you pasting into?
                    For this testing I simply created a new tab by pressing Ctrl+n and pasting into that.
                    The default new document type is “Normal” language.

                    Lycan ThropeL 1 Reply Last reply Reply Quote 0
                    • Lycan ThropeL
                      Lycan Thrope @Alan Kilborn
                      last edited by Lycan Thrope

                      @Alan-Kilborn said in posting code (by copy & paste) with line numbers:

                      What are you pasting into?

                      After clicking the new file button, it opens a ‘new1’ document and I paste into that, and it appears at least for me, to pick the D Language. Not sure why, but I’ve noticed that I don’t default to None since I’ve been working on my different versions of dBASE UDL’s, and have had them listed in the UDL menu. I now seem to have to specifically choose None if I want to make a document display that way that I haven’t already saved with a .txt file extension.

                      Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
                      • Alan KilbornA
                        Alan Kilborn @Lycan Thrope
                        last edited by

                        @Lycan-Thrope said:

                        After clicking the new file button, it opens a ‘new1’ document and I paste into that, and it appears at least for me, to pick the D Language

                        This sounds wrong to me. If it is going to pick D for new document creation, it should do so as soon as File > New (equivalent to Ctrl+n) is executed, not when you paste content. Doing so at paste time would require N++ to be examining your content and making a guess based on it, which I doubt that it does as that would not be easy.

                        1 Reply Last reply Reply Quote 1
                        • PeterJonesP
                          PeterJones @Lycan Thrope
                          last edited by

                          @Lycan-Thrope ,

                          If it’s automatically picking the D language, have you looked at Settings > Preferences > New Document > Default Language – does it say D instead of None (Normal Text)? If so, change that preference back, and it will go back to defaulting to normal text. (That setting cannot be affected by UDL, so I don’t see how your dBASE UDLs could be doing anything to influence it…)

                          Lycan ThropeL 1 Reply Last reply Reply Quote 2
                          • Jim DaileyJ
                            Jim Dailey @gooly
                            last edited by

                            <OK_boomer_jest_mode>
                            TRIGGER WARNING: Non-Notepad++ solution here; forum purists should ignore this post!
                            </OK_boomer_jest_mode>

                            @gooly If your code snippet is in a file, say, snip.txt, then an AWK one-liner will easily handle this task (I use GAWK):

                            gawk "{print(FNR,$0)}" snip.txt > nsnip.txt
                            

                            Given this snip.txt file:

                            This is a simple bit
                              of text. It is not
                             much of an example,
                            but it will do.
                            

                            the resulting nsnip.txt file will be:

                            1 This is a simple bit
                            2   of text. It is not
                            3  much of an example,
                            4 but it will do.
                            

                            Anyone who writes code would be well-served by at least a cursory familiarity with some version of AWK.

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

                              Reminder: A decision by the owner of this forum: https://community.notepad-plus-plus.org/topic/26795

                              Jim DaileyJ PeterJonesP 2 Replies Last reply Reply Quote 2
                              • guy038G
                                guy038
                                last edited by guy038

                                Hi, @alan-kilborn,

                                Sorry, Alan, I did NOT notice this specific post from @donho, in order to place all alternate solutions, not related with N++ !

                                So, as an admin user, I was able to move my post to topic ID : 26795.

                                BR

                                guy038

                                1 Reply Last reply Reply Quote 2
                                • Jim DaileyJ
                                  Jim Dailey @Alan Kilborn
                                  last edited by

                                  @Alan-Kilborn If I only knew how to reply while moving the reply to another category.

                                  1 Reply Last reply Reply Quote 1
                                  • PeterJonesP
                                    PeterJones @Alan Kilborn
                                    last edited by PeterJones

                                    @Jim-Dailey said in posting code (by copy & paste) with line numbers:

                                    If I only knew how to reply while moving the reply to another category.

                                    Unfortunately, Reply ▼ > Reply as Topic doesn’t allow you to reply in an existing topic.

                                    So you have to use a workaround: When you click to Quote a reply, it will give you the @-mention and the link to the post you are replying to; you can then copy that and Discard the original reply. Then go to the other Topic, click the top-level Reply (so it’s not attached to a specific post), and paste the preamble into the other Topic – it will keep the @-mention, the link to the previous Topic, and any quoted text, and you can use that as the basis for your forked conversation, no admin/moderator powers required. HTH.

                                    Jim DaileyJ 1 Reply Last reply Reply Quote 2
                                    • Jim DaileyJ
                                      Jim Dailey @PeterJones
                                      last edited by

                                      @PeterJones Thanks for the info.

                                      I think it will be much easier for me to simply lurk and keep quiet about anything unrelated to Notepad++, which is, without a doubt, one of the best text editors around, free or otherwise.

                                      1 Reply Last reply Reply Quote 2
                                      • Lycan ThropeL
                                        Lycan Thrope @PeterJones
                                        last edited by Lycan Thrope

                                        @PeterJones said in posting code (by copy & paste) with line numbers:

                                        @Lycan-Thrope ,

                                        If it’s automatically picking the D language, have you looked at Settings > Preferences > New Document > Default Language – does it say D instead of None (Normal Text)? If so, change that preference back, and it will go back to defaulting to normal text. (That setting cannot be affected by UDL, so I don’t see how your dBASE UDLs could be doing anything to influence it…)

                                        @PeterJones ,
                                        Thanks, this was the cause.
                                        Now that I think back, I think I was doing a help in the forum and used that to show the language setting and take screenshots…and forgot to put it back. DOH!!
                                        Back to normal…thanks for curing my forgetfulness. :-)

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