Community
    • Login

    Insert code before and after markdown selection

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    markdown
    4 Posts 3 Posters 1.5k Views 2 Watching
    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.
    • fly-wireF Offline
      fly-wire
      last edited by

      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
      ```

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP Online
        PeterJones @fly-wire
        last edited by

        @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
        • guy038G Offline
          guy038
          last edited by guy038

          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

          PeterJonesP 1 Reply Last reply Reply Quote 1
          • PeterJonesP Online
            PeterJones @guy038
            last edited by

            @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

            Hello! It looks like you're interested in this conversation, but you don't have an account yet.

            Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

            With your input, this post could be even better 💗

            Register Login
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors