Replace selected text or word under cursor when selection changes (immediate) or when calling a command (by key binding).
This extension looks a lot like Replace Rules by bhughes339.
The difference is the possibility of immediate replacement and change of word under cursor (next version).
The search must match the selected text (for a particular Multi Cursor) completely. If you want to search for a partial match you have to add (.*?)
to the start and end of the search string. If you don't need these strings in the replace string you can use .*?
or (?:.*?)
If a rule is an immediate
rule it is evaluated the moment you Double Click or Select Word or another means of modify the selection.
The rules not marked as immediate
are evaluated when you call the command replace-on.selection-changed
from the command menu as Replace On: Replace selection/word by defined rules or with a key binding. In a key binding you can add an args
property. The args
property has the same format as the replace-on.selection-changed
configuration setting.
If you have Multi Cursors each selection is treated separately.
Configuration
You specify the search/replace strings in the configuration setting replace-on.selection-changed
.
The properties of replace-on.selection-changed
are:
"all"
: an object with search/replace rules that are applied to all languageIds
"languageId"
: an object with search/replace rules that are applied only to files with the given languageId
The rules you add for a specific languageId replace rules from the all
section with the same search string.
Be aware that the settings from User, Workspace and Folder are merged. If you redefine a search string the properties are merged for that search string. You may need to add a property with its default value.
Object with search/replace rules
The object with search/replace rules has the following format:
cycle
property
If you define the "cycle"
property:
- only the
"immediate"
and "withKey"
properties are used.
- it implies:
"literal": true
The "cycle"
property is kind of syntax sugar. It generates a set of rules behind the scene you could have defined yourself.
The "withKey"
property determines if the object key is part of the cycle. If you start with a placeholder string but do not want that string in the cycle use:
"replace-on.selection-changed": {
"all": {
"foo_placeholer": {
"cycle": ["Foo", "Bar"],
"withKey": false
}
}
}
Is equivalent with:
"replace-on.selection-changed": {
"all": {
"foo_placeholer": {
"replace": "Foo",
"literal": true
},
"Foo": {
"replace": "Bar",
"literal": true
},
"Bar": {
"replace": "Foo",
"literal": true
}
}
}
Example
"replace-on.selection-changed": {
"all": {
"[ABC]": {
"replace": "[MNP]",
"literal": true,
"immediate": true
},
"A(D+)C": {
"replace": "M$1P",
"flags": "i",
"immediate": true
}
},
"javascript": {
"foo": {
"replace": "Bar",
"flags": "i"
}
}
}
TODO
- replace current "word" on command (use all rules)