Yash
Yash

Reputation: 312

how do you select only the entire string in vscode?

I tried looking up this questions but they were for the entire line.

My question is for example let url = "https://robohash.org/hello.png?set=set4";

here, is there any shortcut to select only the url or basically anything enclosed between "". Is it possible?

Upvotes: 11

Views: 10022

Answers (4)

Hamid
Hamid

Reputation: 163

I just found a magical shortcut :)

For expand selection we can use Shift + Alt +

See Basic Editing § Shrink/expand selection for more details and examples.

Upvotes: 14

gelbander
gelbander

Reputation: 1194

There is actually another extension called expand-region with the ability to expand_region and undo_expand_region. The default keybindings are ctrl+w and shift+ctrl+w.

I find this one to be more convenient. It stops before and after " and it will work for the url-fetch scenario. What it doesn't do is to stop at a complete line (with and without indentation) which smart select seems to do. There are probably more differences that I don't know about.

You have to press multiple times to expand further. For the example line in the original post this means 3 times. ctrl+w, ctrl+w, ctrl+w

let url = "https://robo[cursor-here]hash.org/hello.png?set=set4";
  1. https://robohash.org/hello.png?set
  2. https://robohash.org/hello.png?set=set4
  3. "https://robohash.org/hello.png?set=set4"

Upvotes: 0

Ehab Ibrahim
Ehab Ibrahim

Reputation: 569

AFAIK this is not possible without an extension. You can download Quick and Simple Text Selection, the use ctrl+k " shortcut.

If you're familiar with Vim, you can use the vim extension, and then click v i " to enter visual mode, and choose everything between double quotes


For the sake of completeness, VSCode also includes a smart select option, which has the keyboard shortcuts ctrl+shift+right_arrow and ctrl+shift+left_arrow (The shortcut has been changed to alt+shift+left/right_arrow). The problem with it is it doesn't specifically select everything between double quotes, and in the case of a URL, it doesn't simply work as intended. If it's only a simple string, it would also select the double quotes in addition to the string between them

Update 04/2024:

VSCode's smart select behavior differs depending on the language.

  • JavaScript: Selects the URL between double quotes (as OP intends).
  • Python: Selects the URL as well as the double quotes.
  • C: Selects the full line

To summarize, smart select is currently not a "one size fits all" solution to OP's question

Upvotes: 4

Julian K
Julian K

Reputation: 2001

For mac, the "expand selection" shortcut is control + shift +

Click anywhere in the link and expand the selection twice, that should do it.

Upvotes: 2

Related Questions