Skip to content

Instantly share code, notes, and snippets.

@natemoo-re
Last active May 4, 2024 15:56
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save natemoo-re/94cfba2bfb17cbeec0680c05309ea9ee to your computer and use it in GitHub Desktop.
Save natemoo-re/94cfba2bfb17cbeec0680c05309ea9ee to your computer and use it in GitHub Desktop.
Visual Studio Code Snippets – Common Case Transformations

Common Case Transformation Snippets

Visual Studio Code allows Snippets to perform ✨Variable Transforms✨ using Regex.

Here are some common case transformations that you can apply to your snippets. In these examples, I'm using $TM_FILENAME_BASE, but the same transformations should apply to any of the Snippet Variables.

snippets-from-delimited.json can convert filenames like my-file-name, my_file_name, my file name. If your filename is delimited by a dash, underscore, or space, these should work.

snippets-from-mixed-case.json can convert filenames like myFileName and MyFileName. If your filename is in camel or Pascal case, these should work.

{
"ConstantCase": {
"prefix": "filename-constant",
"description": "",
"body": "${TM_FILENAME_BASE/(\\w+)?[-_\\s]+(\\w+)/${1:/upcase}_${2:/upcase}/g}"
},
"SnakeCase": {
"prefix": "filename-snake",
"description": "",
"body": "${TM_FILENAME_BASE/(\\w+)?[-_\\s]+(\\w+)/${1:/downcase}_${2:/downcase}/g}"
},
"KebabCase": {
"prefix": "filename-kebab",
"description": "",
"body": "${TM_FILENAME_BASE/(\\w+)?[-_\\s]+(\\w+)/${1:/downcase}-${2:/downcase}/g}"
},
"CamelCase": {
"prefix": "filename-camel",
"description": "",
"body": "${TM_FILENAME_BASE/(\\w+)?[-_\\s]+(\\w+)/${1:/downcase}${2:/capitalize}/g}"
},
"PascalCase": {
"prefix": "filename-pascal",
"description": "",
"body": "${TM_FILENAME_BASE/(\\w+)?[-_\\s]+(\\w+)/${1:/capitalize}${2:/capitalize}/g}"
}
}
{
"PascalCase": {
"prefix": "filename-pascal",
"description": "",
"body": "${TM_FILENAME_BASE/(\\w)([a-z]+)/${1:/capitalize}${2:/downcase}/g}"
}
}
@rioj7
Copy link

rioj7 commented Apr 7, 2023

A snippet to transform Camel Case to Kebab Case

  "CamelCase to Kebab": {
    "prefix": "camel2kebab",
    "body": "${TM_FILENAME_BASE/(^[A-Z])|(?<=[a-z0-9])([A-Z])/${1:/downcase}${2:+-}${2:/downcase}/g}",
  }

@wlkns
Copy link

wlkns commented Sep 14, 2023

Thanks @rioj7 I'd tried and been looking for this 👍

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