User Snippets
Code snippets are reusable pieces of code that can be quickly inserted into your files. ThinkCode's user snippets feature allows you to create custom snippets to accelerate your coding workflow and maintain consistency in your projects.
Understanding Snippets
Snippets in ThinkCode provide a powerful way to:
- Insert common code patterns quickly
- Standardize code structure across a team
- Reduce repetitive typing
- Enforce best practices
- Document code templates
Accessing Snippets
ThinkCode offers multiple ways to access and manage snippets:
-
Command Palette:
- Press
Ctrl+Shift+P
(Windows/Linux) orCmd+Shift+P
(macOS) - Search for "Snippets: Configure User Snippets"
- Press
-
Settings Menu:
- Navigate to Settings (gear icon) → User Snippets
-
Keyboard Shortcut:
- Default:
Ctrl+Shift+S
(Windows/Linux) orCmd+Shift+S
(macOS) - This can be customized in keyboard settings
- Default:
Snippet Scope
ThinkCode supports several scope levels for snippets:
- Global snippets: Available in all file types
- Language-specific snippets: Only available for specific languages
- Project snippets: Limited to a specific workspace
- Extension snippets: Provided by installed extensions
Creating Snippets
Using the Snippet Editor
- Open the Command Palette and search for "Snippets: Configure User Snippets"
- Choose a language or "New Global Snippets file"
- Use the snippet editor to create your snippet:
Snippet Syntax
- Name: A descriptive name for your snippet (e.g., "React Functional Component")
- Prefix: The text you type to trigger the snippet (e.g., "rfc")
- Body: An array of strings representing each line of the snippet
- Description: A helpful explanation of what the snippet does
Snippet Variables
ThinkCode supports dynamic variables in snippets:
Variable | Description |
---|---|
$1 , $2 , ... | Tabstops, in order of navigation |
${1:default} | Tabstop with default text |
$0 | Final cursor position |
$TM_FILENAME | Current filename |
$TM_FILENAME_BASE | Current filename without extension |
$CURRENT_YEAR | Current year |
$CURRENT_MONTH | Current month (MM format) |
$CURRENT_DATE | Current date (DD format) |
$CURRENT_HOUR | Current hour (24-hour format) |
$RANDOM | Random number |
$CLIPBOARD | Contents of the clipboard |
Using AI to Generate Snippets
ThinkCode integrates AI capabilities to help you create snippets:
- Select code you want to convert to a snippet
- Right-click and select "AI: Create Snippet from Selection"
- The AI will generate a snippet with appropriate tabstops and descriptions
- Review and customize the generated snippet
Using Snippets
Once defined, snippets can be used in several ways:
- Type the prefix and press
Tab
to expand the snippet - IntelliSense: Start typing and select the snippet from the suggestion list
- Command Palette: Search for "Insert Snippet"
- Right-click menu: Select "Insert Snippet" from the context menu
After insertion, you can navigate through tabstops using the Tab
key.
Advanced Snippet Techniques
Multi-line Snippets
For multi-line snippets, use the array format:
Nested Snippets
Combine snippets for complex patterns:
Transformations
Apply transformations to variables:
Available transformations:
/upcase
: Convert to uppercase/downcase
: Convert to lowercase/capitalize
: Capitalize first letter/pascalcase
: Convert to PascalCase/camelcase
: Convert to camelCase/kebabcase
: Convert to kebab-case/snakecase
: Convert to snake_case
Sharing Snippets
Exporting Snippets
To share snippets with others:
-
Manual Export:
- Locate snippet files in
~/.thinkcode/snippets/
- Share the JSON files directly
- Locate snippet files in
-
Using ThinkCode Profiles:
- Include snippets in your Settings Profile
- Export and share the profile
Importing Snippets
To use snippets from others:
- Save the received snippet file to
~/.thinkcode/snippets/
- Restart ThinkCode or reload window
- Alternatively, copy-paste into your existing snippet file
Team Snippets
For team-wide snippets:
- Create a
.thinkcode/snippets/
folder in your project root - Add snippet files there
- These snippets will be available to anyone who opens the project
Snippet Management
As your collection of snippets grows, consider these management practices:
-
Organize by Category: Create separate files for different types of snippets
-
Use Clear Naming Conventions:
- Use consistent prefixes for related snippets
- Include the type in the name (e.g., "Component - Basic")
-
Document Usage: Add detailed descriptions to all snippets
-
Regular Cleanup: Periodically review and remove outdated snippets
-
Version Control: Store important snippets in your dotfiles repository
AI-Enhanced Snippets
ThinkCode offers unique AI capabilities to enhance your snippets:
-
Dynamic Context-Aware Snippets:
- Snippets that adapt based on the surrounding code
- AI-powered parameter suggestions
-
Natural Language Snippet Activation:
- Describe what you need in a comment
- AI generates appropriate code
-
Snippet Learning:
- ThinkCode learns from your coding patterns
- Suggests new snippets based on repeated code
To enable these features, configure AI snippets in settings:
Best Practices
- Start Small: Create snippets for your most frequently typed code patterns
- Be Consistent: Use naming conventions that make snippets easy to remember
- Add Placeholders: Use tabstops for customizable parts
- Update Regularly: Refine snippets as your coding style evolves
- Include Comments: Add explanatory comments in your snippets
- Keep It Simple: Focus on smaller, composable snippets rather than very complex ones
Troubleshooting
If you encounter issues with snippets:
-
Snippets Not Appearing:
- Check file type/language association
- Verify snippet syntax in the JSON file
- Restart ThinkCode
-
Snippet Format Issues:
- Ensure JSON is valid (no trailing commas)
- Check that escape sequences are correct
- Verify that arrays are properly formatted
-
Conflicts:
- Look for duplicate prefixes across files
- Check for extension conflicts
- Adjust snippet scope as needed
For more snippet features and advanced techniques, refer to the ThinkCode API documentation.