Surround selection experiment

Mads Kristensen

You want to quickly select some text and surround it with quotation marks. So, you select your text and hit the quotation mark key on your keyboard, only to find that the selected text now has been replaced by a single “. What you hoped would happen was that the selected text would be surrounded by an opening and closing quotation mark like in the animation below. Let’s do something to fix this.

Image Surrounder2

In this experiment, we want to test if we can create a general feature for all languages that makes it easy to surround the selection. In fact, multiple developers have requested this feature, so remember to vote for it if you agree.

Our hypothesis is that a feature like this is language agnostic and doesn’t need to be customized per language. But we need your help to help test that hypothesis and, potentially, help improve the feature design. We’ve created an extension for Visual Studio called Surrounder that we’ll be using for this experiment.

The extension is very simple to use. Any time you have a selection and type one of the supported characters, the selection will be surrounded by that character and its closing equivalent, as shown in this table:

Opening Closing
` `
( )
[ ]
{ }
< >

 

To participate in this experiment, install the Surrounder extension and let us know how it works for you. Either by commenting in this blog post, or by opening issues or pull requests on the GitHub repo.

There is currently an open feature request for us to implement this behavior in Visual Studio, so make sure to go vote for it.

15 comments

Discussion is closed. Login to edit/delete existing comments.

  • Damian Wyka 0

    YES! Ever since i switched from monodevelop to visual studio i wished for something like this. That was about a decadde ago by then monodevelop had this already built-in. Yes VS has Surround With command but thats clunky to use compared to this. I tried to make custom key combo for this command but from what i remember (i attempted this almost decade ago so my memory is fuzzy) you couldnt bind this command to just two keys like Shift + ” it had to be 3 keys at once making it too annoying to use.

    BTW consider adding support for surround with multiline comment characters with Shift or Alt pressed. Eg for C# pressing Shift (or Alt) + / would produce /**/ (thats how it worked in monodevelop if memory serves right), for Python Shift + ” produces “”” “”” etc. I suggest adding extra key for comment characters specifically to disambiguate for languages that have such ambiguities like Python.

    I think out of all surround variants i would use comment one the most cause when im debugging i often want to temporarily disable multiline sections of code

    Edit:

    Or maybe generalize feature by always requiring selection + Shift + any character. When i say any character i mean it. Dont limit this extension to select few characters like now. Then producing multiline comments for C# would be as simple as making selection once, holding shift and typing /* for Python it would be 3x ” etc. Experience will be slighty worse than current one but generalization i believe is easily worth slight inconvenience of holding shift (or Alt maybe if Shift would be blocking too many combos)

  • MgSam 1

    This sounds like a cool feature. MS Word would benefit from this just as much as Visual Studio!

  • reduckted 2

    > Our hypothesis is that a feature like this is language agnostic and doesn’t need to be customized per language.

    Weird. I wonder why VS Code uses language-specific character pairs.

    Anyway, AutoSurround already does this and has virtually the same behavior as VS Code, including specific character pairs for each language.

  • Evgeny Vrublevsky (VEG) 6

    That’s a very irritating “feature”, I always look how to disable such stuff. If you selected some text and started to type anything else, it is expected that the selected text is replaced. Don’t need to add any magic here.

    • Fernando Borrego Polo 1

      Same here. If they do that, I will have to relearn years of using text editors and have to first delete the text and then start typing the quote I wanted to write in the first place.

  • Lumen Papulus 3

    > So, you select your text and hit the quotation mark key on your keyboard, only to find that the selected text now has been replaced by a single “

    No, we don’t, cause we use ReSharper!

    • Andreas Saurwein 0

      Or any other extension we payed for that does it for us since … ever.

  • Michael Taylor 0

    If the surround behavior is “simple” then I can see it being language agnostic as you literally just insert a character before and after the selection range. But I don’t know how terribly useful that is. A “smart” selection may be somewhat consistent between languages but I still think it needs some language knowledge to know how to proceed.

    For example in C# if I’m surrounding text with double quotes and it is already in a string of double quotes then a simple surround would mess up the string literal whereas a smart surround would escape the quotes. Same for C/C++/Java. However it wouldn’t matter for markdown. But if I’m using C# and raw string literals then a double quote would be fine. I imagine in HTML/XML could cause similar issues.

    I also have questions about the behavior when you have selected text across lines or are using rectangular select. I use this quite a bit.

  • Sergey Benzenko 1

    IDK, maybe it’s just me, but this feature more often than not just irriates me rather than helps.
    Specifically when I copy and paste SQL with parameters from web code in order to test it, and want to replace parameters with actual values in quotes. So I select the parameter name and instead of replacing it with input, it put it in quotes which is not what I expect.

    I’d prefer it via

    Alt+symbol

    rather than just

    symbol

    .

  • Viacheslav Ivanov 0

    Nice thing!

    I’m expecting an escaped double quote inside a string literal in C# so for me this feature should be language specific:

    var text = $"User {userName} not found.";

    “{userName}” is selected and double quote pressed

    var text = $"User \"{userName}\" not found.";

    Instead, it would make sense to allow pressing other characters, including backslash, while Alt or Shift pressed, so the user can type escape symbols.

  • Andreas Saurwein 0

    Great feature – at least 10 free extensions already do that. Is it really required to add this now to VS? It feels like VS is trying to catch up with VS Code. Adding more and more features that break existing ones.

    Besides, this kind of features have a negative influence on the extension economy. There are many extensions that are cheap but lost their right to exist because VS after 20 years finally can do it too.

    • Mads KristensenMicrosoft employee 2

      Features first found in extensions sometimes become industry standard in editors and IDEs. At this point, there is an expectation from users that these features are found natively in Visual Studio or whatever their tool of choice might be. We don’t want to discourage extension authors (believe me, I’m one myself), but expectations, use cases, and competitive landscapes change over time, and we must all adapt.

  • Eli Black 1

    As long as there’s a setting to enable/disable this, I’m all for it! 🙂

    I’m not a fan of this feature, but I know this has become standard in most other editors, so making it be the default in VS probably makes sense, too! 🙂

  • Joshua Laquai 0

    How is this an “experiment” when VSCode has had this feature since the beginning of time and!?
    And as @reduckted already mentioned, AutoSurround has been available for VS for ages AND it’s so far ahead of… whatever this is.
    Stop trying. Accept you’re years late to the party and stop trying.

    • Mads KristensenMicrosoft employee 0

      From the third paragraph:

      > Our hypothesis is that a feature like this is language agnostic and doesn’t need to be customized per language. But we need your help to help test that hypothesis and, potentially, help improve the feature design.

Feedback usabilla icon