Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code: How to convert snippet placeholder to uppercase or lowercase?

In VS Code, the docs for creating user defined snippets mentions some Grammar which includes options for /upcase, /downcase, and /capitalize, but I can't figure out how to use it.

I'm using the latest version of VS Code: Version 1.25.0 on Mac.

It seems like this snippet should convert the value of the placeholder to uppercase and to lowercase after typing it and hitting tab, but it doesn’t:

"test": {     "prefix": "test",     "body": "${1} -> ${1:/upcase} ${1:/downcase}" }, 

Flow and Expected Result

  1. type test
  2. hit tab to get the snippet.
  3. type Asdf to result in:

    Asdf -> Asdf Asdf 
  4. hit tab to get expected result of:

    Asdf -> ASDF asdf 

Current Result

asdf -> asdf asdf 
like image 970
Beau Smith Avatar asked Jul 10 '18 19:07

Beau Smith


People also ask

How do I edit a snippet in VS code?

To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by language identifier) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages.

How do I change case in VSCode?

Use Ctrl + Shift P for Windows and Linux. As shown in the image above, you can convert a string or sentence's case into: Upper case. Lower case.


2 Answers

Try this:

"test": {     "prefix": "test",     // "body": "${1} -> ${1/(.*)/${1:/upcase}/} > ${1/(.*)/${1:/downcase}/}"     // simpler version below works too     "body": "${1} -> ${1/(.*)/${1:/upcase} ${1:/downcase}/}" } 

You need to hit Tab to apply the transformation.

like image 130
Mark Avatar answered Sep 18 '22 22:09

Mark


Kind of solution:

  "test": {     "prefix": "test",     "body": "$1 ${1/(.*)/${1:/upcase}/}  ->  ${1/(.*)/${1:/downcase}/}  ->  ${1/(.*)/${1:/capitalize}/}"   } 

result:

asdF ASDF  ->  asdf  ->  AsdF 
like image 39
Taras B. Avatar answered Sep 18 '22 22:09

Taras B.