Vscode: Surround a text/word with special characters

Created on 14 Dec 2015  Â·  12Comments  Â·  Source: microsoft/vscode

Surround a text/word with special characters by selecting it and pressing the special character.

example:

feature-request markdown verified

Most helpful comment

I just pushed 8fbad6e which adds this feature for markdown. To use it, select some text in markdown and trigger the insert snippet command. The bold, Italic, and inline code snippets for markdown all have this enabled now.

Here's the reason a snipped based approach was selected and how other extensions/languages can implement this sort of functionality:

For basic surround logic, extensions can use the surroundWith language configuration option. Adding * for the surroundWith in the markdown language configuration allows the behavior that @danielschmitz originally described. surroundWith can also be configured separately from autoClosingPairs.

However, in testing, I was concerned that surroundWith for * and other special characters may get in the way of too many users. That's why I went with a more explicit snippet based approach. This makes use of the TM_SELECTED_TEXT value present in snippets:

    "Insert bold text": {
        "prefix": "bold",
        "body": "**${1:${TM_SELECTED_TEXT}}**$0",
        "description": "Insert bold text"
    },

Any extension can contribute snippets that make use of this pattern. Feel free to submit a PR if you would like any of the other markdown snippets to use the surround logic as well


Please let me know if you have any questions or concerns about the current approach

All 12 comments

Woah!!! We need this, but this looks to be better suited as an extension really. It should have some kind of modifier key though because I sometimes would like to replace a word with a special character.

Maybe Alt + special character?

@bpasero Ben, start with you here since this sounds like a mark down feature. I think there is no underlying support needed in the editor to make this happen.

Hello guys, its a mark down "native" feature.

Thanks

Yes, this is similar to the plugin for the brackets IDE.

Very useful for wrapping html elements around selected text.

http://www.granneman.com/webdev/editors/brackets/extensions/surround/
https://github.com/pedelman/brackets-surround

+1 from me!

Great in any language for quotes/brackets/parens. User expects to be able to overwrite text by highlighting and typing, so @hashhar's suggestion is on the right track. Alt might not work right on Mac, since it's already used by the system to insert special characters (like ÷«“‘ªº). But other modifier(s) should.

Need this!

Maybe someone can take a look at how they implemented the surround with quotes to get an idea to extend it. I think after v1.6 we could select text and press " or ' and that surrounded the text.

EDIT: Found it. https://github.com/Microsoft/vscode/blob/c80de308d5ac76d9db02c0a9a2c9ac3417071af5/src/vs/editor/common/commands/surroundSelectionCommand.ts

EDIT 2: See this for learning how to achieve this currently. Currently this feature is available when autoClosingBrackets is enabled.

What I would want is full surround with like exists in Visual Studio see here for super quick intro which would let me use any snippet in addition to punctuation.
It is very handy for things like moving code into an if or try/catch.

The far dream would have us be able to declare an arbitrary punctuation pair and use that like we can now with (), [], ect.

Most of this functionality could be hidden behind a shortcut like ctrl+k, s for those who are worried about it interfering with basic typing.

Was suprised to see the behaviour for markdown files concerning surrounding selected text with the typed character is different from say js, html, css, ... files. Was this by design intended to exclude md files from this behaviour?

Parity with Visual Studio would be great here. I know it _can_ be an extension, but IMHO this should be built in or a pre-installed extension.

How can I enable this feature in markdown files? Which command implements this?

I just pushed 8fbad6e which adds this feature for markdown. To use it, select some text in markdown and trigger the insert snippet command. The bold, Italic, and inline code snippets for markdown all have this enabled now.

Here's the reason a snipped based approach was selected and how other extensions/languages can implement this sort of functionality:

For basic surround logic, extensions can use the surroundWith language configuration option. Adding * for the surroundWith in the markdown language configuration allows the behavior that @danielschmitz originally described. surroundWith can also be configured separately from autoClosingPairs.

However, in testing, I was concerned that surroundWith for * and other special characters may get in the way of too many users. That's why I went with a more explicit snippet based approach. This makes use of the TM_SELECTED_TEXT value present in snippets:

    "Insert bold text": {
        "prefix": "bold",
        "body": "**${1:${TM_SELECTED_TEXT}}**$0",
        "description": "Insert bold text"
    },

Any extension can contribute snippets that make use of this pattern. Feel free to submit a PR if you would like any of the other markdown snippets to use the surround logic as well


Please let me know if you have any questions or concerns about the current approach

Was this page helpful?
0 / 5 - 0 ratings