Insert code before and after markdown selection
-
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
``` -
If you’re going to be manually selecting each block of java code, then that sounds like a job for a macro:
- Select the first block of java text
- Macro > Start Recording
- Ctrl+X to cut the block of text
- Type
```java
then hit ENTER - Ctrl+V to paste the block of text
- Type
```
then hit ENTER - 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.
-
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 activeshortcuts.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
-
-
@guy038 ,
Ah, yeah: I had avoided the S/R in the macro because I didn’t think of ticking the “in selection”. Good idea.
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.