Skip to content

Instantly share code, notes, and snippets.

@cmstead
Last active March 3, 2024 06:40
Show Gist options
  • Save cmstead/7ad64539d13dfdb5a2591bc3be3c8eeb to your computer and use it in GitHub Desktop.
Save cmstead/7ad64539d13dfdb5a2591bc3be3c8eeb to your computer and use it in GitHub Desktop.
VS Code snippet transform: convert camel case to Pascal case or vice versa
// If you want to convert a PascalCase variable to camelCase, you can do the following:
// Where `n` is the tab stop you want to reference
${n/^(.)(.*)$/${1:/downcase}${2}/}
// Example: ${1:This is my original} => ${1/^(.)(.*)$/${1:/downcase}${2}/}
// If you want to convert a camelCase variable to PascalCase, you can do the following:
// Where `n` is the tab stop you want to reference
${n/^(.)(.*)$/${1:/upcase}${2}/}
// Example: ${1:This is my original} => ${1/^(.)(.*)$/${1:/upcase}${2}/}
@omarbenmegdoul
Copy link

Thank you!

@cmstead
Copy link
Author

cmstead commented May 18, 2021

I don't know how you found my gist, but I'm really glad it was helpful!

@omarbenmegdoul
Copy link

omarbenmegdoul commented May 19, 2021 via email

@fayax555
Copy link

fayax555 commented Jul 4, 2021

Thanks!

@cgbeutler
Copy link

Google hath blessed your gist, which hath blessed me in turn.

@hackrx
Copy link

hackrx commented Sep 8, 2021

@cmstead Thanks for this gist.
I have an issue, can you please help me to fix it?
I want to covert $1 value(Pascal Value) to camelCase, but your first snippet which is
${1/^(.)(.*)$/${1:/downcase}${2}/} is not working, in this case, it is typing the same variable with this snippet for eg.

void $1(){

var _$1Request = {$2};

var sendReq = anotherFunction(_$1Request);


}

What I am getting for $1 = HelloWorld

void HelloWorld(){

var _HelloWorldRequest = {"😋"};

var sendReq = anotherFunction(_HelloWorldRequest);


}

Expected behavior:

void HelloWorld(){

var _helloWorldRequest = {"😋"};

var sendReq = anotherFunction(_helloWorldRequest);

}

Can you please tell me how can I achieve this behavior?

@cgbeutler
Copy link

@hackrx, I think you want

void $1(){
  var _${1/^(.)(.*)$/${1:/downcase}${2}/}Request = {$2};
  var sendReq = anotherFunction(_${1/^(.)(.*)$/${1:/downcase}${2}/}Request);
}

In other words, the first $1 should be normal, the rest of the references do the find-replace of the first character with a lowercase.

@cmstead
Copy link
Author

cmstead commented Sep 8, 2021

Thanks for helping @cgbeutler!

@nokternol
Copy link

My only issue was forgetting to hit tab while designing the snippet as the transform is not applied until you attempt to navigate to the next tabstop. Thank you for this regex and options combination as it is a very common and useful one.

@cathei
Copy link

cathei commented Jan 10, 2022

Seems like there is /camelcase and /pascalcase transform, which could make everything simpler.
But this acts like it's meant to convert snake_case to camelCase, not PascalCase to camelCase.
There is also /capitalize transform, which can be used for converting camelCase to PascalCase, but there is no opposite transform.

Therefore these do give expected result,

${1:snake_to_camel} => ${1/(.*)/${1:/camelcase}/} // result is "snakeToCamel"
${2:snake_to_pascal} => ${2/(.*)/${1:/pascalcase}/} // result is "SnakeToPascal"
${3:camelToPascal} => ${3/(.*)/${1:/capitalize}/} // result is "CamelToPascal"

But these do not give expected result.

${1:PascalToCamel} => ${1/(.*)/${1:/camelcase}/} // result is "pascaltocamel"
${2:camelToPascal} => ${2/(.*)/${1:/pascalcase}/} // result is "Cameltopascal"

What a mess.

@programmermark
Copy link

thank you

@viktor-evdokimov
Copy link

viktor-evdokimov commented Oct 5, 2022

Answer to vscode snippet transform file name in the snippet to camel case or pascal case variable

given file_name_foo.ext file

${TM_FILENAME_BASE/^(.*)_foo$/${1:/pascalcase}/}  ==> FileName
${TM_FILENAME_BASE/^(.*)_foo$/${1}/}  ==> file_name
${TM_FILENAME_BASE/^(.*)_foo$/${1:/upcase}/}  ==> FILE_NAME

@alicerocheman
Copy link

alicerocheman commented Oct 13, 2022

:/camelcase works as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment