• Login
Community
  • Login

Insert code before and after markdown selection

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
markdown
4 Posts 3 Posters 1.1k 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.
  • F
    fly-wire
    last edited by Oct 25, 2021, 12:16 PM

    I have a text code with many pieces of java code I’m converting to markdown. I can select the code block manually. How can I insert:

    ```java
    before selected text followed by
    ```

    P 1 Reply Last reply Oct 25, 2021, 12:56 PM Reply Quote 0
    • P
      PeterJones @fly-wire
      last edited by Oct 25, 2021, 12:56 PM

      @fly-wire ,

      If you’re going to be manually selecting each block of java code, then that sounds like a job for a macro:

      1. Select the first block of java text
      2. Macro > Start Recording
      3. Ctrl+X to cut the block of text
      4. Type ```java then hit ENTER
      5. Ctrl+V to paste the block of text
      6. Type ``` then hit ENTER
      7. Macro > Stop Recording

      At this point, you can select the next block of java text, and Macro > Playback to convert that one.

      If you want to save this macro for future use, and maybe assign a keyboard shortcut to it, Macro > Save Current Recorded Macro, give the macro a name, and optionally assign the keyboard shortcut.

      1 Reply Last reply Reply Quote 1
      • G
        guy038
        last edited by guy038 Oct 25, 2021, 3:27 PM Oct 25, 2021, 3:23 PM

        Hello, @fly-wire, @peterjones and All,

        I thought about a macro, too, but based upon a regular expression Search / Replacement !


        First, to test it :

        • Select your entire block to write with the Markdown syntax

        • Open the Replace dialog

        • SEARCH (?s-m).+(\r\n)$|.+[^\r\n]$

        • REPLACE ```java\r\n$0(?1:\r\n)```?1\1

        • Tick the In selection option ( VERY IMPORTANT )

        • Select the Regular expression search mode

        • Click ONLY on the Replace All button


        Now,

        • At the end of the <Macros>........</Macros> node of your active shortcuts.xml file, add this equivalent macro, below :
                <Macro name="MD Raw Java Block" 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="(?s-m).+(\r\n)$|.+[^\r\n]$" />
                    <Action type="3" message="1625" wParam="0" lParam="2" sParam="" />
                    <Action type="3" message="1602" wParam="0" lParam="0" sParam="```java\r\n$0(?1:\r\n)```?1\1" />
                    <Action type="3" message="1702" wParam="0" lParam="640" sParam="" />
                    <Action type="3" message="1701" wParam="0" lParam="1609" sParam="" />
                </Macro>
        
        • Stop and restart N++

        • Do a normal selection of any block of lines

        • Click on the entry Macro > MD RaW Java Block


        Notes :

        • This S/R does not mind if the final line-break of the selected block is included or not

        • This S/R does not mind too, about possible blank or empty lines , within the selection

        • Your selection can be all the contents of current file !

        • If you work with Unix Files, use rather this S/R ( adapt the macro, accordingly ! ) :

          • SEARCH (?s-m).+(\n)$|.+[^\r\n]$

          • REPLACE ```java\n$0(?1:\n)```?1\1

        • If you work with Mac Files, use rather this S/R ( adapt the macro, accordingly ! ) :

          • SEARCH (?s-m).+(\r)$|.+[^\r\n]$

          • REPLACE ```java\r$0(?1:\r)```?1\1

        Best Regards,

        guy038

        P 1 Reply Last reply Oct 25, 2021, 3:33 PM Reply Quote 1
        • P
          PeterJones @guy038
          last edited by Oct 25, 2021, 3:33 PM

          @guy038 ,

          Ah, yeah: I had avoided the S/R in the macro because I didn’t think of ticking the “in selection”. Good idea.

          @fly-wire ,

          My solution is okay; but if you want to preserve your clipboard you should use @guy038’s solution, because mine will replace the contents of the clipboard with your selected text, whereas the search-and-replace macro does not use the clipboard, so your old clipboard contents will still be available for pasting later.

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