Question

I'm writing a sublimetext snippet, and I want to mirror a field, but get the mirrored field as a lowercase while the original field is in Title case.

getUser ('username', function(err, user){});
   ^^^^                            ^^^^

Here it says that I could use perl regex but I don't know either enough to figure out
what would be the appropriate regex to achieve this?

get${1:User}('',function(err,${1/???/g}){});
Was it helpful?

Solution

Instead of doing a simple replacement, you need a format string to your regex, as discussed in the snippets reference. So, your snippet should look like this:

<snippet>
    <content><![CDATA[
get${1:User} ('$2', function(err, ${1/(.*)/\L\1\E/i}){$0});
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <tabTrigger>get</tabTrigger>
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.c</scope>
</snippet>

The regex matches all possible characters in variable $1 (except newlines) and replaces it with itself but formats it in all lowercase (\L begins lowercasing, \1 is the first capture group, and \E ends it). After it does that, hitting Tab moves to $2, so you can enter that value if you wish. Hitting Tab once more puts the cursor between the {} curly braces.

Make sure you set the <scope> to the appropriate language.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top