Help with a couple of Macros
-
Hi all,
Newbie here.
I am using Notepad++ called from inside a console application. I write text to a file, then open it in Notepad++ for editing, then read the saved text and store it elsewhere.
2 macros I would like are:
-
<Alt+Enter> Move to the end of the next non-blank line. If the next line is blank, move down a line and test again. If line is not blank, move to end. I realize that this might not be possible because the macro language is NOT a programming language.
-
<Alt+X> Save file and quit
Any help would be appreciated.
Mike
Notepad++ v8.7 (64-bit)
Build time : Sep 17 2024 - 17:06:31
Path : C:\Program Files\Notepad++\notepad++.exe
Command Line : “Fubar.txt”
Admin mode : OFF
Local Conf mode : OFF
Cloud Config : OFF
Periodic Backup : OFF
OS Name : Windows 10 Pro (64-bit)
OS Version : 22H2
OS Build : 19045.4894
Current ANSI codepage : 1252
Plugins :
mimeTools (3.1)
NppConverter (4.6)
NppExport (0.4) -
-
@Darkwood-Consulting said in Help with a couple of Macros:
Move to the end of the next non-blank line
This is a work in progress, but using this regex (regular expression) in Find puts the cursor at the position you want:
Find What:(?-s).+\R.((\R)++)?.+\R
I deliberately didn’t escape the selection as I wanted to see what it encompasses. Possibly more to come. This can be recorded as a macro.
Terry
EDIT, just realised it doesn’t need 1 period, so amended regex is
(?-s).+\R((\R)++)?.+\R
. Again, still a work in progress. -
@Darkwood-Consulting said in Help with a couple of Macros:
<Alt+X> Save file and quit
That’s going to be more tricky than just recording the regex that @Terry-R is developing for you: the “Save file and quit” it’s actually two simple commands, both of which can be played by a macro… but the macro-recorder won’t be able to record the “quit” command (because it will try to record it, then the application will exit before you have saved the macro you are trying to record, and the macro won’t be saved).
Fortunately, you can edit the
shortcuts.xml
file (usually in%AppData%\Notepad++
🛈), as described in the Online User Manual > Config Files Details > Macros section – and by learning from that section of the manual, you can manually craft a macro by hand.Following the advice in the manual, the File > Save command can be determined to be command-ID
41006
, and File > Exit is41011
. Using that, you can create the macro as follows:- follow the advice in Editing Configuration Files and exit Notepad++ completely, open just
%AppData%\Notepad++\shortcuts.xml
(or your activeshortcuts.xml
if you don’t have a normal installation) - In
shortcuts.xml
, go to the</Macros>
line, and just before that insert:<Macro name="FORUM: Save and Exit" Ctrl="no" Alt="yes" Shift="no" Key="88"> <Action type="2" message="0" wParam="41006" lParam="0" sParam="" /> <Action type="2" message="0" wParam="41011" lParam="0" sParam="" /> </Macro>
- save
shortcuts.xml
and exit Notepad++. Restart Notepad++ - the
FORUM: Save and Exit
will be available in your Macros menu, and will show the shortcutAlt+X
—
I realize that this might not be possible because the macro language is NOT a programming language.
As you can see from Terry, there are ways around that in your specific task.
But in general, you are right: the macro language can only do certain things, and you don’t get things like user input, variables, and loops/if-then-else-logic. You can read more about Macros and other alternatives to Macros in our FAQ entry “Automating Notepad++”
- follow the advice in Editing Configuration Files and exit Notepad++ completely, open just
-
@Darkwood-Consulting said in Help with a couple of Macros:
Move to the end of the next non-blank line
I’ve come up with a revised version as the previous one looks like it failed when there weren’t any more “non-blank” lines in the file. So now the regex is:
(?-s)(.+)?(\R+)?(.+)?\K
So this is used in the Find function. Copy the red text above (the actual regex) and paste into the “Find What” field of the Find function. Have one of your files open and do a few tests by putting the cursor in various locations and clicking on “Find Next”. You need to test all possible scenarios you might encounter to be sure this will meet your needs.
So once a regex has been found to work, next comes the step of recording the macro. Whilst I’m aware you already have some knowledge of how to record a macro, I’m not sure if you’ve got past the basics of recording a shortcut key combo. To record this macro, you would start the recording as you normally do, then either use the mouse to click on the “Search” menu item, then “Find” or use the appropriate shortcut key for “Find” of which the default is “Ctrl F”. You will see this alongside the menu option. If you have already done some tests a working regex will already be available. Just make sure it is listed in the Find What field. Click on “Find Next” and then click on close. At this point you can stop recording the macro, save and name it.
To give a bit of background to the regex:
(?-s) - DOT characters do not include carriage return/line feeds
(.+)? - consume as many characters as possible on the current line, ? allows for an empty line
(\R+)? - consume as many carriage return/line feeds as possible from the current position (which is the end of a line), ? allows for a non-empty line. After this the cursor will be at the start of non-empty line or end of file
(.+)? - consume as many characters as possible on the current line, ? allows for an empty line/end of file position
\K - forget everything selected and leave the cursor in the new positionTerry
PS, although you refer to a blank line, are you sure it is truly empty, not just containing some blank characters as that changes the solution. Good luck.