Is this possible, suffix text based on a list.
-
I’m new to Notepad++ and I’m wondering if this task is possible with a pluggin or a macro etc.
I’d like to open a file and within the file there would be specific code/groups of text such as G21 as an example. What I would like to do is within the file after the text is suffix with another piece of text based on a list i would populate within either the pluggin or macro.
An example of this would be
Initial lines of code
G90
G21 G40 G80After a scan and inserted additional text it would read like this.
G90 ;Absolute programming
G21 ;Programming in millimeters (mm) G40 ;Tool radius compensation off G80 ;Cancel canned cycle -
Natively to Notepad++, no. And I don’t know of any plugin that has that specific feature.
However, the PythonScript plugin (and other scripting plugins) allow you to use a common scripting language to write scripts to manipulate Notepad++ and the contents of the open files.
For example, the following should do what you want. The installation instructions are found in the black box of text.
addSuffixes.py
# encoding=utf-8 """https://community.notepad-plus-plus.org/topic/22028/is-this-possible-suffix-text-based-on-a-list Enter your list of suffixes here: """ suffixes = { # your search string and suffix go here, using the same format as these examples, including colons and commas 'G90': "Absolute programming", 'G21': "Programming in millimeters (mm)", 'G40': "Tool radius compensation off", 'G80': "Cancel canned cycle", # leave this one alone None: None } """ INSTRUCTIONS: 1. Install PythonScript Plugin * Plugins > Plugins Admin * Click ☑ PythonScript checkbox until checked * click INSTALL * restart Notepad++ as necessary 2. Create new PythonScript script * Plugins > PythonScript > New Script * Give it a reasonable name, like addSuffixes.py * Paste in all the code from this black box * Save 3. Switch to the document where you want to search/replace 4. Plugins > PythonScript > Scripts > addSuffixes To assign a keyboard shortcut: 1. Plugins > PythonScript > Configuration 2. In the User Scripts, click on addSuffixes.py 3. Click the left ADD button (above Menu Items) * This adds "addSuffixes" to the main Plugins > PythonScript menu, rather than requiring you to dig down into the Scripts submenu 4. Exit Notepad++ and restart * This is required for ShortcutMapper to be able to see the new entry in the PythonScript menu 5. Settings > ShortcutMapper > Plugin Commands 6. Filter by addSuffixes 7. Click on addSuffixes, then click the MODIFY button and set the keyboard shortcut in the dialog """ from Npp import * editor.beginUndoAction() for search,suffix in suffixes.items(): if search is None: continue search_re = r'\b\Q' + str(search) + r'\E\b' editor.rereplace(search_re, '$0 ;{}'.format(suffix)) editor.endUndoAction()
-
Thank you for the quick reply, I will give this a try now.
-
It hasn’t worked Peter, is this likely because the file is a .nc file rather than a .txt file ?
-
Hello, @mark-littlefair, @peterjones and All,
Of course, the Peter’s method, with a script language, is very powerful and universal !
However, if your number of spotted items is not important, a simple regex S/R could be enough !
In your case, the regex S/R would be :
SEARCH
(G90)|(G21)|(G40)|(G80)
REPLACE
$0 ;(?1Absolute programming)(?2Programming in millimeters \(mm\))(?3Tool radius compensation off)(?4Cancel canned cycle)
Notes :
-
The logic of this regex S/R is almost obvious !
-
In search, it looks for, either, each
Gxx
item described -
In replacement, the
$0
syntax represents the current search value (Gxx
) which is rewritten, followed with a space char and a semicolon -
Then, depending on the
Gxx
matched ( group1
to4
), the corresponding comment part is just added -
Note that the parentheses around mm must be escaped as they have a special meaning in replacement
-
IMPORTANT : Do not exceed
2,048
characters in the replacement zone !
Best Regards,
guy038
-
-
It hasn’t worked Peter, is this likely because the file is a .nc file rather than a .txt file ?
If it failed, then it has nothing to do with the extension of the file. Unless the script specifically calls out certain extensions (this script didn’t), a PythonScript script will act the same regardless of file extension.
I created a file that had the two lines from your example,
G90 G21 G40 G80
and after running the script, I get:
G90 ;Absolute programming G21 ;Programming in millimeters mm G40 ;Tool radius compensation off G80 ;Cancel canned cycle
I did just notice a bug – the parentheses around
(mm)
in the replacement were lost… I’ll fix that in a future post.But in general, the script works for me.
Debugging steps:
- Does the menu entry Plugins > Python Script exist? If no, then revisit the INSTRUCTIONS
- Does the menu entry Plugins > Python Script > Scripts > addSuffixes exist? If no, then revisit the INSTRUCTIONS
- Try running Plugins > Python Script > Show Console. Does a new console window show up in Notepad++?
- Select the .nc file and then run Plugins > Python Script > Scripts > addSuffixes . If there is any output in the Python Script console, copy/paste or screenshot it and include it in your reply
BTW: @guy038’s regex solution will also work for a small number of replacements, like in your example.
But it will get unwieldy if you have 10 or more suffix replacements to do, whereas this script should handle any number of search/suffix pairs you want to throw at it. -
@PeterJones said in Is this possible, suffix text based on a list.:
I’ll fix that in a future post.
The easiest fix is to always use \( and \) for parentheses in the suffixes map
'G21': "Programming in millimeters \(mm\)",
-
@PeterJones
1,2 and 3 all okHere is the screenshot.
-
Sorry I’ve just seen it myself now, not sure how that 1 got in there sorry again :)
-
Your screenshot didn’t show line 59 of
addSuffixes.py
, but that’s where the error lies.If you really have the same input as mine, line 59 was the last line of the file, beyond
editor.endUndoAction()
… if so, just make sure there are no characters on the last line of the file (make it an empty line, without any spaces).Looking at the error message, you appear to have an
l
or a1
or something at the end of the file, which may or may not be indented. There shouldn’t be. -
all good
-
Sorry to be a pain Peter,
My mistake the semi colon should be open bracket then the text suffix then close bracket
I’ve tried to edit the code where i thought it should work and failed.
-
It’s probably failing because in the replacement, parentheses are a special character, so to get a literal paren wrapped around the replacement, you will need to use the \( and \) … so to replicate the final line on the left in your screenshot, you would want
editor.rereplace(search_re, '$0 \({}\)'.format(suffix))
… yes, with that line in the script, the output for your original example input yielded:
G90 (Absolute programming) G21 (Programming in millimeters (mm)) G40 (Tool radius compensation off) G80 (Cancel canned cycle)
The PythonScript plugin uses the boost regex engine (the same that Notepad++ uses) for the
editor.research()
calls, so the substitution follows all the same rules as for Notepad++ replacements, as described here -
Hi, @mark-littlefair, @peterjones and All,
Given your new conditions about the ouptut text, the regex S/R, provided in my last post, becomes :
SEARCH
(G90)|(G21)|(G40)|(G80)
REPLACE
$0 \((?1Absolute programming)(?2Programming in millimeters \(mm\))(?3Tool radius compensation off)(?4Cancel canned cycle)\)
BR
guy038
-
Thank you Guy, this is the problem with your method though if I’m limited in the replacement zone to 2048 characters
-
That’s run through great now, thank you. :)