7

Is it possible to replace text in a Visual Studio Snippet literal after the enter key is pressed and the snippet exits its edit mode?

For example, given a snippet like this:

 public void $name$
 {
   $end$
 }

If I were to enter $name$ as:

 My function name

is it possible to have Visual Studio change it to:

 My_function_name

or

 MyFunctionName
3
  • 2
    There are only four functions available while inserting code snippet. Currently there is no option to create user defined functions. three of them - msdn.microsoft.com/en-us/library/ms242312.aspx Callbase - Returns a call to the base member, or returns a NotImplementedException if the member is abstract. Commented Dec 1, 2009 at 6:55
  • 1
    You should answer this in an answer so it can be voted on and accepted. Commented Dec 1, 2009 at 21:11
  • Snippet functionality is very limited. It's just text insert. After many years, VS did not add needed basic string manipulation. Commented Dec 22, 2018 at 19:23

3 Answers 3

14

After many years there is an answer, for anyone still coming across this question:

"Replace Whitespaces": {
    "prefix": "wrap2",
    "body": [
        "${TM_SELECTED_TEXT/[' ']/_/gi}",
    ],
    "description": "Replace all whitespaces of highlighted Text with underscores"
},

Add this to your user snippets. Or alternativly you can add a keyboard shortcut like this:

{
    "key": "ctrl+shift+y",
    "command": "editor.action.insertSnippet",
    "when": "editorTextFocus",
    "args": {
        "snippet": "${TM_SELECTED_TEXT/[' ']/_/gi}"            
    }
},

Hope this helps anyone coming across this in the future

Sign up to request clarification or add additional context in comments.

2 Comments

This is awesome! If you selected some text like ' te'xt ', could you use this to replace the outermost single quotes with double quotes?
@Tedskovsky Probably - the replacement in the snippet part is just a RegEx. As I only know my basic ways around RegEx-Magic (it definitely is magic), maybe try asking this in a seperate question.
2

In my case the goal was to replace a single instance of the word Authoring with Baker:

${TM_FILENAME_BASE/(Authoring)/Baker/}

Comments

1

It's great. I used it to wrap things in a function with quotes. If a selection has quotes it can remove the quotes. In the snippet part, it seems to break down into:

TM_SELECTED_TEXT - this is the input
[ ' '] - regex find
_ - regex replace
gi - global flag for regex

So what I wanted is change: "User logged in" into: <%= gettext("User logged in") %> For this in the snippet I used:

"body": ["<%= gettext(\"${TM_SELECTED_TEXT/['\"']//gi}\") %>"],

Note: you need to escape the quotein the regular expression, therefore: " becomes \".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.