DEV Community

Cover image for Snippets I Use Regularly #1: File Name and Directory Name
Andreas Riedmüller
Andreas Riedmüller

Posted on

Snippets I Use Regularly #1: File Name and Directory Name

I found that I frequently type the name of a new component several times when creating it manually:

  1. When creating the folder or file
  2. Naming the actual function
  3. The css module className

For example:

// MyAwesomeTitle.tsx

export function MyAwesomeTitle() {
  return <h1 className={styles.myAwesomeTitle}>
    
  </h1>
}
Enter fullscreen mode Exit fullscreen mode

To spare my fingers I have created these easy to remember snippets:

  • Dir: current directory/folder in PascalCase
  • dir: current directory/folder in camelCase
  • File: current file in PascalCase
  • file: current file in camelCase

As these are not really language specific I added them to …/snippets/global.code-snippets.

You can easily open/create this file by cmd+shift+PSnippets: Configure User Snippets.

Directory Name PascalCase

MyAwesomeTitle/index.tsx → MyAwesomeTitle
Enter fullscreen mode Exit fullscreen mode
"Dirname PascalCase": {
    "prefix": "Dir",
    "body": ["${TM_DIRECTORY/^.+[\\/\\\\]+(.*)$/${1:/pascalcase}/}"]
}
Enter fullscreen mode Exit fullscreen mode

Directory Name camelCase

MyAwesomeTitle/index.tsx → myAwesomeTitle
Enter fullscreen mode Exit fullscreen mode
"Dirname camelCase": {
    "prefix": "dir",
    "body": ["${TM_DIRECTORY/^.+[\\/\\\\]+(.*)$/${1:/camelcase}/}"]
}
Enter fullscreen mode Exit fullscreen mode

File Name PascalCase

…/MyAwesomeTitle.tsx → myAwesomeTitle
Enter fullscreen mode Exit fullscreen mode
"Filename PascalCase": {
    "prefix": "File",
    "body": ["${TM_FILENAME_BASE:pascalcase}"]
}
Enter fullscreen mode Exit fullscreen mode

File Name camelCase

…/MyAwesomeTitle.tsx → myAwesomeTitle
Enter fullscreen mode Exit fullscreen mode
"Filename camelCase": {
    "prefix": "file",
    "body": ["${TM_FILENAME_BASE:camelcase}"]
}
Enter fullscreen mode Exit fullscreen mode

Happy coding!

Top comments (0)