Skip to content

Commit 9b11fed

Browse files
committed
Merge branch 'feature/replace-nth-match' of https://github.com/daddel80/notepadpp-multireplace into feature/replace-nth-match
2 parents eb0f9e8 + 5d7aa9e commit 9b11fed

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

README.md

+25-11
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ MultiReplace is a Notepad++ plugin that allows users to create, store, and manag
2222
- [lvars](#lvarsfilepath)
2323
- [lkp](#lkpkey-hpath-inner)
2424
- [fmtN](#fmtnnum-maxdecimals-fixeddecimals)
25+
- [Preloading Variables](#preloading-variables)
2526
- [Operators](#operators)
2627
- [If-Then Logic](#if-then-logic)
2728
- [DEBUG option](#debug-option)
@@ -174,19 +175,13 @@ Initializes custom variables for use in various commands, extending beyond stand
174175

175176
Custom variables maintain their values throughout a single Replace-All or within a list of multiple Replace operations. Thus, they can transfer values from one list entry to subsequent ones. They reset at the start of each new document in **'Replace All in All Open Documents'**.
176177

178+
> **Tip**: To learn how to preload variables using an empty Find field before the main replacement process starts, see [Preloading Variables](#preloading-variables).
179+
177180
| **Find** | **Replace** | **Before** | **After** | **Regex** | **Scope CSV** | **Description** |
178181
|-----------------|----------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------|--------------------------------------------------------|----------|--------------|------------------------------------------------------------------------------------------------------------------------------|
179182
| `(\d+)` | `vars({COL2=0,COL4=0}); cond(LCNT==4, COL2+COL4);`<br>`if COL==2 then COL2=CAP1 end;`<br>`if COL==4 then COL4=CAP1 end;` | `1,20,text,2,0`<br>`2,30,text,3,0`<br>`3,40,text,4,0` | `1,20,text,2,22.0`<br>`2,30,text,3,33.0`<br>`3,40,text,4,44.0` | Yes | Yes | Tracks values from columns 2 and 4, sums them, and updates the result for the 4th match in the current line. |
180183
| `\d{2}-[A-Z]{3}`| `vars({MATCH_PREV=''}); cond(LCNT==1,'Moved', MATCH_PREV); MATCH_PREV=MATCH;` | `12-POV,00-PLC`<br>`65-SUB,00-PLC`<br>`43-VOL,00-PLC` | `Moved,12-POV`<br>`Moved,65-SUB`<br>`Moved,43-VOL` | Yes | No | Uses `MATCH_PREV` to track the first match in the line and shift it to the 2nd (`LCNT`) match during replacements. |
181184

182-
An empty Find string (`*(empty)*`) can be used to set variables for the entire Find and Replace list without being tied to a specific Find action. This entry does **not** match any text but is executed once at the beginning of the **'Replace'** or **'Replace All'** process when **'Use List'** is enabled. It allows the Replace field to run initialization commands like `vars()` for the entire operation. This entry is always executed first, regardless of its position in the list.
183-
184-
| **Find** | **Replace** | **Description/Expected Output** |
185-
|--------------------|-------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
186-
| *(empty)* | `vars({ `<br>`VpersonName = FNAME:sub(1, (FNAME:find(" - ", 1, true) or 0) - 1),`<br>`Vdepartment = FNAME:sub((FNAME:find(" - ", 1, true) or #FNAME + 1) + 3, (FNAME:find(".", 1, true) or 0) - 1) })` | Extracts `VpersonName` and `Vdepartment` from the active document’s filename in the format `<Name> - <Department>.xml` using the `vars` action. Triggered only once at the start of the replace process when **Find** is empty. |
187-
| `personname` | `set(VpersonName)` | Replaces `personname` with the content of the variable `VpersonName`, previously initialized by the `vars` action. |
188-
| `department` | `set(Vdepartment)` | Replaces `department` with the content of the variable `Vdepartment`, previously initialized by the `vars` action. |
189-
190185
<br>
191186

192187
#### **lvars(filePath)**
@@ -198,10 +193,10 @@ The parameter **filePath** must specify a valid path to a file. Supported path f
198193
- Forward Slashes: `"C:/path/to/file.vars"`
199194
- Long Bracket String: `[[C:\path\to\file.vars]]`
200195

201-
**Example File:**
196+
** File:**
202197
```lua
203198
-- Local variables remain private
204-
local PATH = [[C:\Data\Projects\Example\]]
199+
local PATH = [[C:\Data\Projects\\]]
205200

206201
-- Only the returned variables are accessible in Replace operations
207202
return {
@@ -212,14 +207,15 @@ return {
212207
}
213208
```
214209

210+
> **Tip**: To learn how to preload variables using an empty Find field before the main replacement process starts, see [Preloading Variables](#preloading-variables).
211+
215212
| Find | Replace | Regex | Scope CSV | Description |
216213
|---------------|-------------------------------------------------------------------------------|-------|-----------|------------------------------------------------------------------------------------------------------|
217214
| *(empty)* | `lvars([[C:\tmp\m\Vars.vars]])` | No | No | Loads variables such as `userName = "Alice"` and `threshold = 10` from `myVars.vars`. |
218215
| `Hello` | `set(userName)` | No | No | Replaces `Hello` with the value of the variable `userName`, e.g., `"Alice"`. |
219216
| `(\d+)` | `cond(threshold > 5, "Above", "Below")` | Yes | No | Replaces the match based on the condition evaluated using the variable `threshold`. |
220217

221218
**Key Points**
222-
- **Initialization**: An empty Find string (*(empty)*) initializes variables globally at the start of the Replace or Replace All process when "Use List" is enabled. This initialization occurs only once, regardless of its position in the list.
223219
- **Conditional Loading**: Variables can be loaded conditionally by placing lvars(filePath) alongside a specific Find pattern. In this case, the variables are only initialized when the pattern matches.
224220
- **Local vs. Returned Variables**: Only variables explicitly included in the return table of the .vars file are available for use. Any local variables remain private to the file.
225221

@@ -290,6 +286,24 @@ Formats numbers based on precision (maxDecimals) and whether the number of decim
290286

291287
<br>
292288

289+
### **Preloading Variables**
290+
MultiReplace supports **predefining or loading variables** before any replacements occur. By separating initialization from the actual replacements, operations stay clean and maintainable.
291+
292+
#### 🔹 **How it works:**
293+
- **Place `vars()` or `lvars()` next to an empty Find field.**
294+
- This entry does **not** search for matches but runs before replacements begin.
295+
- It ensures that **variables are loaded once**, regardless of their position in the list.
296+
297+
**Examples**
298+
299+
| **Find** | **Replace** | **Description** |
300+
|--------------|------------------------------------------------------------|----------------|
301+
| *(empty)* | `vars({prefix = "ID_"})` | Sets `prefix = "ID_"` before replacements. |
302+
| *(empty)* | `lvars([[C:\path\to\myVars.vars]])` | Loads external variables from a file. |
303+
| `(\d+)` | `set(prefix .. CAP1)` | Uses `prefix` from initialization (e.g., `123``ID_123`). |
304+
305+
<br>
306+
293307
### Operators
294308
| Type | Operators |
295309
|-------------|-------------------------------|

0 commit comments

Comments
 (0)