Frequently use 'Find and Replace' combinations
Siak Hooi Ng
Is there a way to save frequently use ‘Find and Replace’ combinations for later use?
probably able to give them names.
for example, i may frequently do this:
- add double quotes to all strings
- remove duplicate lines
- concatenate all lines, ie: remove all \r\n
is good if i can save them with a name, and call them out when needed, without retyping them, especially regexpr, again and again
Yes, it’s called ‘Macro’. On the menu select under Macro, “start recording”, then go through the steps you’d take to type in the find and replace expressions. Complete the replacement and then select the Macro menu option Stop Recording.
Now, and this is very important, another menu option is ‘Save Current Recorded Macro’. You give it a name and you could also select a hot key with which to run it in future, although the hot key isn’t necessary.
Now there is a problem with these macros, it’s that once saved it is very difficult to edit them as they are saved as a bunch of pseudo code. The code is saved within the shortcuts.xml file, generally under C:\Users\username\AppData\Roaming\Notepad++ (username dependent on account used) but this depends on the preferences for Notepad++ and of course what OS you are using. You CANNOT edit this file from Notepad++ as it will overwrite it when Notepad++ exits. Thus, if wanting to alter the Find or Replace string, exit Notepad++, edit the file with ordinary Notepad (or other text editor), save then reload Notepad++. Suggest save an old copy of the file before editing just in case you screw up the Macro pseudo code.
Have a search around this site, you will plenty of other seekers of the truth about Macros, there’s lots of good info around.
You CANNOT edit this file from Notepad++ as it will overwrite it when Notepad++ exits
Not strictly true. It is totally safe to edit shortcuts.xml with Notepad++…as long as you know that Notepad++ won’t overwrite it when it exits…which it will only do if it NEEDS to, i.e., you’ve changed something (via the UI) that belongs in it during the current run.
So if you record a macro, change a key shortcut assignment, etc. during the current run, DON’T also edit shortcuts.xml manually during the same run. Quit N++, restart it, THEN edit the file, save and close it, and quit N++.
if wanting to alter the Find or Replace string
Look for the string(s) you recorded in your macro directly in shortcuts.xml, or to find all of the search/replace/mark operations, search for
"1700"as this appears at the start of any macro operation that involves these functions.
very difficult to edit them as they are saved as a bunch of pseudo code
Difficult but not impossible…and often useful…see:
and especially relevant to this thread, here:
Suggest save an old copy of the file before editing just in case you screw up the Macro pseudo code.
This is always good advice!
Thanks Scott for the correction. My CANNOT I think should have been SHOULD NOT. I refer to:
The wiki says that there are a number of workarounds. The 1st workaround is using an external editor.
Of course if you are a seasoned Notepad++ user with advanced knowledge of how it works then you’d be safe. Otherwise I think the safest route is to use an external editor.
I used plain old vanilla Notepad just recently to edit a bunch of macros and it worked very well, I only needed basic editing functions.
I used plain old vanilla Notepad just recently
There’s just something dirty and disgusting about resorting to that!
And as an alternative to that heresy… If you’re uncomfortable or unsure of the restrictions: copy the shortcuts.xml somewhere else; edit it as normal in Notepad++; save and exit Notepad++; copy the shortcuts.xml back to the
%AppData%\Notepad++(or the local folder for portable/local-config); re-run Notepad++, and your macros are updated.
I agree Scott that it does somewhat seem to be a failure on Notepad++ behalf that you can’t do the macro edits in a ‘proper’ manner within itself.
To offset that, Notepad++ isn’t on it’s own in this. There are other situations where external editors are used when the app itself doesn’t perform to users expectations (slow or unwieldy).
And Peter, agreed that is another option. The thing is though that all these ideas become messy or complicated. Maybe Notepad++ needs to have a “pretty-up” to support this functionality internally and in a correct manner, otherwise each user will resort to whatever measures they think appropriate. For me it’s using that ‘dirty and disgusting’ plain old vanilla Notepad app.
Do you think a “I want this” question to enhance Notepad++ in this manner would get any traction. Concept of breaking the shortcuts.xml file apart and having a separate macros.xml file. That way you would only have 1 situation where the file could be overwritten, and even that could be tempered with a flag so that if you did create a macro or even edit it’s shortcut in the current session you wouldn’t be allowed to open the macro.xml file to edit.
We can all wish!