Home > OS >  VSCode: Toggle between defined texts
VSCode: Toggle between defined texts

Time:11-15

I have to toggle between two texts example ABC and MNO by any keyboard shortcut or mouse click.

  • Like if I double click and selected word is ABC, It should be changed to MNO or vice versa.
  • Or after selecting the word I can run some keyboard shortcut to toggle.
  • Or if current line has ABC it gets changed to MNO with a double click or keyboard shortcut.

I know it's a very weird request but I have a use case. Any sort of work around will work even if I have to write an extension.

CodePudding user response:

I have written an extension snippet toggle demo


This form works to toggle all occurrences in a line if you select the line first and then trigger the keybinding:

{
  "key": "alt t",
  "command": "editor.action.insertSnippet",
  "args": {
    // "snippet": "${TM_SELECTED_TEXT/(ABC)|(MNO)/${1: MNO}${2: ABC}/}"
    "snippet": "${TM_CURRENT_LINE/(.*?)((ABC)|(MNO))/$1${3: MNO}${4: ABC}/g}"
  },
  // "when": "editorTextFocus && editorHasSelection"
  "when": "editorTextFocus"
}

demo 2 toggling all occurrences in a line

  • Related