skip to Main Content

i have many pieces of code same for example like this

alt="Greece"
alt="Greece"
alt="Greece"
alt="Greece"

Can i somehow modify it to this? Is there any kind of function like in Excel or something like that please? Imagination write it manualy each is horrible

alt="Greece 1"
alt="Greece 2"
alt="Greece 3"
...
alt="Greece 200"

2

Answers


  1. You can use the extension Regex Text Generator

    • select the word Greece
    • select all cases you want with
      • Ctrl+D multiple times
      • Ctrl+Shift+L to select all
      • use Shift+Alt+Click to create multiple cursors
    • if needed RightArrow to get all cursors after the word Greece
    • execute command: Generate text based on Regular Expression (regex)
    • As Original Text Regex use: .*
    • As Generator Regex use: {{=i+1}} (watch the space as first char)
    • Look at the preview, use Esc to cancel and Enter to accept

    You can use any calculation based on i you want and you can also match number in the selected text and use that in the calculation N[...].

    You can also add the word Greece by using Greece {{=i+1}}

    Login or Signup to reply.
  2. You have a couple of options. First, using the extension, Find and Transform (disclaimer, I wrote that extension, this is very easy. Make this keybinding in your keybindings.json (after installing the extension):

    {
      "key": "alt+n",          // whatever keybinding you like
      "command": "findInCurrentFile",
      "args": {
        "find": "(alt="Greece")",
        "replace": "$1 ${matchNumber}",
        "isRegex": true
      }
    },
    

    Actually you can make it even easier if you first select what you want to modify (see the demo below). Then use this simple keybinding:

    {
      "key": "alt+n",
      "command": "findInCurrentFile",
      "args": {
        "replace": "$1 ${matchNumber}",  // what you select will be put into $1
        "isRegex": true
      }
    },
    

    add a matchIndex to a line


    Another option – not quite as easy

    Snippets have a variable $CURSOR_NUMBER which is useful here.

    Make this keybinding:

    {
      "key": "alt+n",
      "command": "editor.action.insertSnippet",
      "args": {
        "snippet": "$TM_SELECTED $CURSOR_NUMBER"
      },
      "when": "editorHasSelection"
    },
    
    1. Do a find on your desired text match: alt="Greece"
    2. Ctrl+Shift+L to select all occurrences of the find match.
    3. Trigger the snippet via its keybinding.

    Demo of this method:

    demo of using snippet with $CUROSR_NUMBER

    So this second method is more steps but doesn’t require an extension.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search