26

Is there any shortcut or extension for vscode which can help to remove surrounded quotes (single ' or double " ) around a selected text?
See example below

'hello' ==> hello

In other words, is it possible to have a feature which will toggle current selection between quotes and no quotes?

I have tried es quotes for vscode which is very nice while switching the quotes between single and double quotes.

Gama11
  • 31,714
  • 9
  • 78
  • 100
rahoolm
  • 733
  • 2
  • 12
  • 22
  • See this [vscode issue/feature request](https://github.com/Microsoft/vscode/issues/4039). If that does satisfy your need, please submit feature request. – CrnaStena Nov 16 '16 at 14:21

5 Answers5

22

Elaborating on Soldeplata's answer:

Before we start: Look up your shortcut for expanding selection by hitting Ctrl+k,Ctrl+s and search for smartselect (as it could differ with keyboard layout and installed keymap-extensions). I have two shortcuts to choose from: Shift+alt+ or Ctrl+w

The steps (not one shortcut but a small series):

  • Your starting point..................................... Some say: "hello";

  1. Put cursor any where in the word hello
    and use shortcut to expand selection....... Some say: "hello";
  2. Cut selection (Ctrl+x)......................... Some say: "";
  3. Hit backspace once
    (this removes both quotes)...................... Some say: ;
  4. Paste (Ctrl+v).................................... Some say: hello;
Community
  • 1
  • 1
Superole
  • 1,329
  • 21
  • 29
  • The command you're talking about is called "Expand Selection", right? (`editor.action.smartSelect.expand`?) It doesn't seem to work properly for multiple words. For example, I have this string in one of my config JSONs: `"Docstring - one-line"`. If I put the cursor anywhere after the second hyphen, what gets selected is `line"`, but otherwise it works as you describe. It also seems to depend on context. If I copy in a single-quoted string from elsewhere and do "Expand Selection" a few times, the single-quoted string never gets selected, always more or less. – wjandrea Jul 12 '22 at 19:51
  • 4
    Note that you must have `"editor.autoClosingDelete": "always"` in your settings.json, otherwise step 3 won't work as it should. – Alexander Wallace Matchneer Jul 15 '22 at 19:12
  • @wjandrea maybe there are more (new) settings that could affect this command? I tried your string "Docstring - one-line", and I have to expand twice if the cursor is on a separated word, but everything within the quotes is selected correctly in my VSCode from all cursor positions. If the string contains formatted words like camelCase or snake_case, you may have to expand up to three times to get the whole string. – Superole Aug 01 '22 at 12:56
  • @wjandrea ahh, but it does not work well for strings within strings :/ f.ex. if you wanted to remove the single-quotes from this string: "Docstring: 'one-line'" or the double-quotes from this one: someSay = 'Hello there, "stranger"'; – Superole Aug 01 '22 at 13:08
9

There is this extension that lets you do some tricks with selections between quotes. You can toggle between simple and double quotes, and you can select whatever is inside some quotes.

The most approximate solution which I found is to select all with the shortcut (Ctrl+k, `), then cut, erase the quotes and then paste the text.

That's the best I found

Soldeplata Saketos
  • 3,212
  • 1
  • 25
  • 38
  • 1
    Accepting this as answer since this is the most appropriate way to get the work done and the extensions mentioned is also of great use. Thanks – rahoolm Mar 25 '21 at 03:01
  • 3
    There is also [this extension](https://marketplace.visualstudio.com/items?itemName=BriteSnow.vscode-toggle-quotes), which _only_ toggles quotes. – Nicolai Weitkemper Dec 26 '21 at 20:59
4

Say you were given the following JSON and you want the researchers value to be an array instead of a string. We need to remove the quotes that are wrapping the array. In version 1.64 of VSCode, you can do this search and replace using the built-in search and replace regex capture groups (no extension needed).

[
  {
    "id": 2,
    "description": "Project 1",
    "researchers": "[12,5,22]"
  },
  {
    "id": 3,
    "description": "Project 2",
    "researchers": "[1,44,22]"
  }
]

screenshot of search string in VS Code

In the search field you will want to use a regular expression of "researchers": "\[(.+)\]", where the (.+) part of the expression being the first group we want to preserve or leave unchanged when we perform the replace. Then in the replace field we use "researchers": [$1], with $1 corresponding to the (.+) first group we are preserving in the search string. What this means is that we take the values that were found in the first search group (which in the first instance is 12,5,22, then wrap it with "researchers": [12,5,22]. The replaced JSON becomes:

[
  {
    "id": 2,
    "description": "Project 1",
    "researchers": [12,5,22]
  },
  {
    "id": 3,
    "description": "Project 2",
    "researchers": [1,44,22]
  }
]

The following video is also helpful. https://www.youtube.com/watch?v=6AsSfyHWWls

w. Patrick Gale
  • 1,643
  • 13
  • 22
0

I highlight the word with ctl+d and then type a quote. Same for parens or braces.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31601779) – jrswgtr Apr 29 '22 at 12:34
-5

This works for the example you provided.

  1. Select the text you want to remove the quotes from.

  2. Menu Edit | Replace

  3. Search for ' quote mark

  4. Replace with [space]

  5. Click the "Replace All" icon

  6. Edit | Undo will put the quotes back if you want.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
Tamitik_Tech
  • 151
  • 5
  • 1
    I understand that is possible using replace but this is not the query. I want a shortcut or plugin which can do this certain selected string within quotes or doubles and not the complete document or text file – rahoolm Nov 24 '16 at 13:36
  • Sorry this wouldn't work. My solution does meet your stated specifications. – Tamitik_Tech Nov 24 '16 at 17:27