daddel80 / notepadpp-multireplace

MultiReplace is a plugin for Notepad++ enabling multi-string replacement, allowing list saving and loading. It provides CSV syntax highlighting and enabling precise column targeting. Additionally, it introduces conditional and computational operations within the replacement string.
GNU General Public License v2.0
20 stars 7 forks source link

Regexp rename with a counter #31

Closed andry81 closed 1 year ago

andry81 commented 1 year ago

Is there a way?

I need to rename key names in an ini file:

[section1]
Buttoncount=2

button1=...
cmd1=...
menu1=...

button2=...
cmd2=...
menu2=...

I want to insert a button (block of keys) between 1 and 2 and call the replacer to rename all keys using the counter variable:

[section1]
Buttoncount=2

button1=...
cmd1=...
menu1=...

; inserted values
button=...
cmd=...
menu=...

button2=...
cmd2=...
menu2=...
daddel80 commented 1 year ago

Directly incrementing numeric values using regex in Notepad++ or MultiReplace isn't feasible. However, in MultiReplace, there's a workaround.You can predefine a list of replacements: start by replacing "button1" with "button2", then "button3" with "button4", and continue this pattern up to the maximum number of elements you anticipate having. Start with the highest numbers in the list to avoid overlap. For buttons without a number, simply replace button= with button1. For ease, you can prepare a CSV list in Excel and load it using the 'Load List' function in MultiReplace. I hope this helps!

Important: Due to an issue found in the Replace function, please download the latest build here , you find it on the right side at the bottom, unzip MultiReplace-Release-x64.zip and copy it into your Notepad++ plugin directory.

andry81 commented 1 year ago

You can predefine a list of replacements: start by replacing "button1" with "button2", then "button3" with "button4", and continue this pattern up to the maximum number of elements you anticipate having.

This won't work. I need to renumber the keys because of add/move/remove of them.

Enables scripted text replacements through bash script export.

Is there a possibility to run a shell script using some input variables like column number, field value or something like that?

daddel80 commented 1 year ago

Alright, I get what you're aiming for. Instead of implementing a direct counter, I gave it a go in MultiReplace with a different approach as an experimental feature, as I find it valuable to test its usefulness.

Here's a step-by-step guide:

  1. Remove Numeric Suffixes: Make sure the option Regular Expression is checked. Find Replace W C N E R
    (Button|cmd)\d+ $1 X

    This step is to clear all numbers after "Button" and "cmd". This operation should be done without activating the experimental feature.

  2. Prepare Your Replacement List: Make sure the option "Match whole word only" is checked. Find Replace W C N E R
    Button Button1 X X
    Button Button2 X X
    Button Button3 X X
    cmd cmd1 X X
    cmd cmd2 X X
    cmd cmd3 X X

    ... (continue this pattern up to your expected maximum count)

  3. Activate Experimental Feature: Before running the replacement list, activate an experimental feature. When active, each entry in the list will replace only one occurrence in the text, preventing overwriting already assigned numbers.

    • Select any entry in the list
    • Press F11. (works only if an entry is selcted) A window will pop up. This feature remains active until you press F11 again or restart N++.
  4. Run Your Replacement List: With the experimental feature activated and your replacement list ready, initiate the list. This will sequentially renumber all your "Button" and "cmd" entries.

    For efficient operations, you can activate or deactivate multiple entries in the list by selecting them and pressing 'Space'. Just be cautious about when the experimental feature is activated, as there isn't a visual indication yet.

Download the release containing this feature here.

I hope this helps!

If you encounter any issues, don't hesitate to reach out.

daddel80 commented 1 year ago

I'm planning to enhance the plugin by adding an 'Advanced Replacement' feature with support for counters, positions, line numbers, and dependencies in the next version.

andry81 commented 1 year ago

Thank you, it works!

I have add the list files into mine project https://github.com/andry81/tacklebar

daddel80 commented 1 year ago

Support of Counter is supported in next v. 2.0.0.7 coming in N++ 8.5.8 with option "Use Variables".