Remove surrounded quotes in vscode or toggle between quote and no quote
Asked Answered
S

6

27

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.

Skippet answered 16/11, 2016 at 13:51 Comment(1)
See this vscode issue/feature request. If that does satisfy your need, please submit feature request.Beyer
J
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

Jadotville answered 29/9, 2017 at 11:6 Comment(2)
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. ThanksSkippet
There is also this extension, which only toggles quotes.Aberrant
D
24

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;
Dimond answered 16/10, 2019 at 8:48 Comment(4)
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.Bader
Note that you must have "editor.autoClosingDelete": "always" in your settings.json, otherwise step 3 won't work as it should.Faveolate
@Bader 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.Dimond
@Bader 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"';Dimond
J
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

Jadotville answered 29/9, 2017 at 11:6 Comment(2)
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. ThanksSkippet
There is also this extension, which only toggles quotes.Aberrant
K
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

Kazue answered 14/2, 2022 at 2:16 Comment(1)
Thank you! $1 is the argument I was looking forPaleozoic
B
1

There is an extension called Bracketeer. Despite it's name it can not only change the bracket type (or remove them) but also change or remove quotes!

Bumboat answered 20/12, 2023 at 10:29 Comment(0)
L
0

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

Lactose answered 22/4, 2022 at 20:0 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewHorrid
A
-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.

Aegisthus answered 19/11, 2016 at 21:42 Comment(2)
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 fileSkippet
Sorry this wouldn't work. My solution does meet your stated specifications.Aegisthus

© 2022 - 2024 — McMap. All rights reserved.