-2

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.

John Zhang
  • 381
  • 1
  • 3
  • 22
  • Does this answer your question? [Emacs replace regexp with incremental sequence](https://stackoverflow.com/questions/49519627/emacs-replace-regexp-with-incremental-sequence) – Biffen Mar 15 '23 at 08:48
  • @Biffen Emacs is a good editor. But it's not ease to use on windows or Mac. – John Zhang Mar 15 '23 at 15:01

2 Answers2

1

You can use the extension Regex Text Generator

The generate expression you want is: {{=i+1}}

rioV8
  • 24,506
  • 3
  • 32
  • 49
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.

increment hashes demo

Mark
  • 143,421
  • 24
  • 428
  • 436