soulshined
soulshined

Reputation: 10622

vscode select first occurring string from cursor

Is there a way to select the word[s] boundary of the next most string. I'd be okay with an extension if that's needed. Of course, this would need to be dependent on the language grammar.

For example:

  co▮nst a = "a is a const";

Assuming the cursor is after the 'o' in const, doesn't really matter though, is there a keyboard shortcut, or a way to enable, navigating (and selecting) the next most string's content without the delimiters; a is a const in this example.

These are the only thing I could find, but vscode has some weird naming conventions for some settings. I also tried searching for 'navigate'.

enter image description here


Edit

While using SelectBy, I decided on the following for common web dev string delimiters:

"SelectString": {
    "forward": "(?<!\\\\)(['\"`])",
    "forwardNext": "(?<!\\\\){{1}})",
    "forwardInclude": false,
    "forwardNextInclude": false,
}

{
    "command": "selectby.regex",
    "when": "editorTextFocus && !editorHasSelection"
}

Takes escaped characters into account, but, disclaimer, if your cursor is currently in a string and you activate it, it will find a nested string, provided it's there of course.

Additionally, I decided to add !editorHasSelection to the when clause because otherwise it will consider the ending delimiter of a current selection, the new beginning. But that's too each their own type of thing.

Upvotes: 1

Views: 1172

Answers (2)

Mark
Mark

Reputation: 182791

Using an extension I wrote, Find and Transform, you can do this fairly easily with this keybinding:

{
  "key": "alt+y",                     // whatever keybinding you like
  "command": "findInCurrentFile",
  "args": {
    "find": "(?<=([\"'`]))([^\"'`]*)(?=\\1)",  
    "isRegex": true,
    "restrictFind": "nextSelect"   // select the next match, other options here too
  }
}

If you want, you could store those keybinding options in a setting.

demo of selecting the next string

As you can see from the demo, it is very hard (and probably not worth the trouble) to construct the correct regex if you trigger this while within a string. But just trigger it twice if you start within a string.

Upvotes: 2

rioV8
rioV8

Reputation: 28838

If you use Select By v0.7.0 you can select the next string with the following settings

    "selectby.regexes": {
      "stringContent": {
        "forward": "('''|\"\"\"|'|\")",
        "forwardNext": "{{1}}",
        "forwardInclude": false,
        "forwardNextInclude": false
      }
    }

Define a keybinding:

  {
    "key": "ctrl+shift+alt+f10",
    "when": "editorTextFocus",
    "command": "selectby.regex",
    "args": ["stringContent"]
  }

Upvotes: 3

Related Questions