I have a document with contains ## for list sign. I want to replace ## with order numbers.
first matched ## -> to 1.
second matched ## -> 2.
.... 3.
.... 4.
Is this possible to do in an editor, like VS Code etc.
I have a document with contains ## for list sign. I want to replace ## with order numbers.
first matched ## -> to 1.
second matched ## -> 2.
.... 3.
.... 4.
Is this possible to do in an editor, like VS Code etc.
You can use the extension Regex Text Generator
The generate expression you want is: {{=i+1}}
You can do this without an extension by first selecting all the ##
's you want to change. One way is by a Find: ##
and then Alt+Enter to select all those matches.
Then run this keybinding:
{
"key": "alt+w", // whatever keybinding you want
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${CURSOR_NUMBER}" // 1-based, CURSOR_INDEX is 0-based
},
"when": "editorHasSelection"
},
To combine the find, select all and replace in one step you can use the extension Find and Transform, which I wrote. Here is a sample keybinding that will do what you want (in your keybindings.json
):
{
"key": "alt+c", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
"description": "increment hashes",
"find": "##",
"replace": "${matchNumber}",
"postCommands": "cancelSelection"
}
}
${matchNumber}
is 1-based, ${matchIndex}
is 0-based.