Using Snippets

The Pieces for Sublime Text Plugin enhances your development workflow with powerful features like saving and reusing snippets, ensuring your most critical code is always accessible.

Here’s a look at some commonly saved and frequently reused materials:


Logging Utilities

A helper function for detailed server-side logging with timestamps and error levels.

Speeds up debugging and monitoring by inserting reliable logging methods.

Validation Functions

A function to validate JSON payloads or form inputs before submission.

Ensures data integrity with reusable, thoroughly tested validation logic.

API Integration Code

Prewritten axios code for making GET or POST requests, complete with error handling and response parsing.

Reduces repetitive work when integrating RESTful APIs or GraphQL endpoints.

Component Templates

A React functional component template with basic hooks and styling setup.

Provides a consistent starting point for building new components.

Database Queries

A MongoDB query to retrieve active user data filtered by last login.

Streamlines database tasks by reusing optimized query patterns.


Boosting Productivity with Snippet Management

The Pieces for Sublime Text Plugin connects you with your development environment, so you can focus on what matters—building great software—while empowering you with tools for efficient code reuse.

  • Improved Productivity: Focus on solving new problems rather than resolving old ones.

  • Contextual Awareness: Snippets enriched with metadata, descriptions, and links to relevant searches help you remember the origin, purpose, and ideal usage scenarios for each piece of code.

  • Code Standardization: Sharing best practices and reusable components helps developers ensure uniform coding practices and conventions are available across different teams and projects.

By using the Pieces for Sublime Text Plugin, you can capture and manage these snippets to streamline daily tasks and enhance your workflow in several ways:

Save & Organize

Users can save snippets by highlighting the desired code, right-clicking, and selecting the Save to Pieces option. This will open a new Sublime Text tab with your new snippet.

This guarantees that important pieces of code, such as code fragments, functions, configuration files, and command-line instructions, are always at hand—just when you need them.

Additionally, each snippet is enriched with context, enabling you to insert well-documented, relevant snippets where they make sense within your projects.

Search & Reuse

You can directly search through your collection of saved materials from within Sublime Text, allowing you to quickly find snippets by keyword or context.

To search you can open the command palette with ⌘+shift+p (macOS) or ctrl+shift+p (Windows/Linux), enter Pieces: Search, and select the algorithm that suits your search case best.

These intelligent search options ensure that you can find the exact snippet you need, even if you don't remember the exact details. They also prevent you from wasting time rewriting or searching for the same solutions across projects.

Edit & Update

With a few keystrokes, you can open saved snippets from your local repository and make edits to keep your code evergreen and improve your most important pieces of code.

You can open your saved materials by opening the command palette with ⌘+shift+p (macOS) or ctrl+shift+p (Windows/Linux), enter Pieces: Open Saved Material, and search or select for the snippet you’d like to edit.

After selecting, the snippet will open up in a new page, where you can select Edit and make your necessary edits to the snippet.

Regularly revisiting your snippets ensures that your code evolves with your projects, adapting to new requirements and improvements. 

Collaborate & Share

Is your teammate scrambling for a crucial piece of boilerplate code? Are they reaching out to you on vacation, desperately searching for the shell script that keeps server #1 running smoothly?

This is where sharing snippets comes into play—this feature lets you share your code with team members, even if they don’t use Pieces.

This can be done without the recipient needing a Pieces for Developers Account.

Sharing useful code snippets can eliminate productivity roadblocks, especially when a co-developer is new to the project or a junior team member.

For instance, a teammate handling server monitoring could leverage your shared script to automate daily tasks seamlessly, saving time and effort without needing extra setup.

These shared links also include their original enriched metadata, like tags, descriptions, and other information.

To share a snippet in Sublime Text, you can highlight some lines of code, right-click, hover over Pieces and click Generate Shareable Link. After Pieces shares the code, a pop-up will appear, allowing you to copy or dismiss the link.

This copies a link you can share with teammates, ensuring easy access to your reusable code.

Use Cases

With Pieces for Sublime Text Plugin, your snippets become a robust library of reusable code that you can organize, search, edit, and share across projects.

Here are some examples of how you can use them:

Reusable Scheduling Functions

Imagine yourself regularly tackling projects that require scheduling tasks to run at specific intervals.

Instead of rewriting the same logic for each new project, you can save an evergreen snippet to your Pieces repository.

This allows you to reuse a well-crafted scheduling function across multiple applications that’s useful in a variety of scenarios:

package main

import (
	"fmt"
	"time"
)

// A reusable function to schedule tasks at a specified interval
func scheduleTask(interval time.Duration, task func()) {
	ticker := time.NewTicker(interval)
	defer ticker.Stop()

	for {
		select {
		case <-ticker.C:
			task()
		}
	}
}

File System Monitoring

If you frequently work on projects that involve monitoring file system changes, you can save a helpful snippet to your Pieces repository that does exactly that, using generic and reusable naming conventions:

package main

import (
	"fmt"
	"log"
	"github.com/fsnotify/fsnotify"
)

// A reusable function to monitor file system changes
func watchFileChanges(path string) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	err = watcher.Add(path)
	if err != nil {
		log.Fatal(err)
	}

	for {
		select {
		case event := <-watcher.Events:
			if event.Op&fsnotify.Write == fsnotify.Write {
				fmt.Println("Modified file:", event.Name)
			}
		case err := <-watcher.Errors:
			fmt.Println("Error:", err)
		}
	}
}

Additional Snippet Scenarios

Read up on some additional scenarios and use cases for Pieces Snippets and see how the Pieces for Sublime Text Plugin could benefit you and your workflow.



Download the Pieces for Sublime Text Plugin today!
Updated on