Skip to main content
Early access — new tools and guides added regularly
🟣 Power User Workflows — Guide 11 of 11
View track
>_ claude codeAdvanced25 min

Building Custom Slash Commands

Extend Claude Code with custom slash commands that automate your specific workflows — from project scaffolding to deployment checks to team-specific code generators.

What you will build
A collection of custom slash commands tailored to your development workflow

What slash commands are and how they work

Slash commands are reusable prompts that you can invoke in Claude Code by typing a forward slash followed by the command name. Instead of typing the same complex instruction every time you want to perform a common task, you define it once as a slash command and invoke it with a short name. For example, instead of typing a 200-word prompt explaining how to review code every time, you type /review and the full prompt executes automatically. Slash commands live in markdown files within the .claude/commands/ directory in your project. Each file is a single command. The file name becomes the command name: .claude/commands/review.md creates the /review command. The file contents are the prompt that Claude Code executes when you invoke the command. Ask Claude Code: Create the .claude/commands/ directory in the project root. Create a simple test command at .claude/commands/hello.md with the content: Say hello and list the top 3 files in this project by size. Tell me what each one does. Now start a new Claude Code session and type /hello. Claude Code executes the prompt as if you had typed it yourself. The command can include any instruction you would normally give Claude Code: read files, make changes, run commands, or analyse code. Slash commands also support a special variable: $ARGUMENTS. This placeholder is replaced by whatever text follows the command name. For example, if your command file contains: Review the file $ARGUMENTS for bugs, security issues, and code style. Then typing /review src/lib/auth.ts passes src/lib/auth.ts as the argument. This makes commands flexible and reusable across different files and contexts.

Project scaffolding commands

One of the most valuable uses of slash commands is generating consistent project structures. Ask Claude Code: Create a command at .claude/commands/new-feature.md that scaffolds a complete feature. The prompt should be: Create a new feature called $ARGUMENTS. Generate the following files following our project conventions: a page component at src/app/$ARGUMENTS/page.tsx with proper metadata and server component structure, an API route at src/app/api/$ARGUMENTS/route.ts with GET and POST handlers including input validation with Zod and error handling, a types file at src/lib/types/$ARGUMENTS.ts with the core interfaces, and a test file at src/tests/$ARGUMENTS.test.ts with placeholder test cases for each API endpoint. Follow the patterns established in existing features. Use the coding standards from CLAUDE.md. Test it: start Claude Code and type /new-feature notifications. Claude Code should create all four files with consistent patterns matching your existing codebase. Ask Claude Code: Create a command at .claude/commands/new-component.md that generates a React component. The prompt: Create a new React component called $ARGUMENTS. Generate: the component file at src/components/$ARGUMENTS.tsx following our component conventions, a Storybook story at src/stories/$ARGUMENTS.stories.tsx with at least 3 variants, and a test file at src/tests/components/$ARGUMENTS.test.tsx. The component should use Tailwind CSS, be properly typed with TypeScript, include JSDoc comments, and handle loading and error states. Ask Claude Code: Create a command at .claude/commands/new-api.md for API routes specifically. Include: route handler with CRUD operations, Zod validation schemas, error handling middleware, and a Postman collection file for testing the endpoints. Each scaffolding command saves 15 to 30 minutes of boilerplate creation and ensures every new feature follows the same patterns.

Code quality and review commands

Review commands bring consistent analysis to any file or changeset. Ask Claude Code: Create a command at .claude/commands/review.md with this prompt: Review the file $ARGUMENTS thoroughly. Check for: bugs and logic errors, security vulnerabilities (injection, XSS, CSRF, auth bypass), performance issues (N+1 queries, unnecessary re-renders, missing memoisation), error handling completeness (are all error paths handled, do errors have useful messages), TypeScript type safety (any usage of 'any', missing types, incorrect assertions), accessibility issues in components (missing labels, keyboard navigation, focus management), and adherence to the project's coding standards in CLAUDE.md. For each issue found, state the severity (critical, high, medium, low), the specific line or lines, what the problem is, and how to fix it. End with a summary of the file's overall quality. Create a command at .claude/commands/review-pr.md for reviewing all changes in a PR: Run git diff main...HEAD to see all changes. Review every changed file for the same criteria as the /review command. Additionally check: do the changes work together cohesively, are there missing edge cases, is the commit history clean and descriptive, and are there any breaking changes that need migration notes. Provide a PR review summary with an overall assessment and a list of required changes versus suggestions. Ask Claude Code: Create a command at .claude/commands/security-check.md that specifically focuses on security: Scan all files in $ARGUMENTS (or the entire src/ directory if no argument) for security issues. Check for hardcoded secrets, SQL injection, XSS vulnerabilities, insecure randomness, missing input validation, exposed error details, and insecure dependencies. Cross-reference findings with the OWASP Top 10. Report findings with severity, location, and remediation steps.

Testing and documentation commands

Writing tests and documentation is tedious but essential. Slash commands make it faster. Ask Claude Code: Create a command at .claude/commands/test.md with this prompt: Read the file $ARGUMENTS and generate comprehensive tests for it. If it is a utility function, write unit tests covering: normal inputs, edge cases (empty arrays, null values, very large inputs), error conditions (invalid inputs, missing dependencies), and boundary values. If it is an API route, write integration tests covering: successful requests with valid data, validation failures with specific invalid fields, authentication and authorisation checks, and error handling for each failure mode. If it is a React component, write tests covering: rendering with default props, rendering with all prop variations, user interaction (clicks, typing, form submission), loading and error states, and accessibility (ARIA attributes, keyboard navigation). Use the testing framework already configured in this project. Aim for 90 percent code coverage. Create a command at .claude/commands/document.md: Read the file $ARGUMENTS and add comprehensive documentation. For exported functions, add JSDoc comments with parameter descriptions, return value description, example usage, and thrown errors. For React components, add prop type documentation with descriptions and defaults. For modules, add a file-level comment explaining the module purpose and its relationship to other modules. If a README does not exist in the directory, create one explaining the directory contents. Ask Claude Code: Create a command at .claude/commands/changelog.md: Review all commits since the last tag (or last 20 commits if no tags). Generate a changelog entry grouped by type: Features, Bug Fixes, Performance, and Breaking Changes. Use the conventional commit messages to categorise. Include the commit hash and a one-line description for each entry.

Deployment and operations commands

Operational commands automate pre-deployment checks and common maintenance tasks. Ask Claude Code: Create a command at .claude/commands/pre-deploy.md: Run a comprehensive pre-deployment check. Verify: all tests pass (npm test), the build succeeds (npm run build), no TypeScript errors (npx tsc --noEmit), no ESLint errors (npx eslint src/), no high-severity vulnerabilities (npm audit --audit-level=high), environment variables are documented in .env.example, the database migrations are up to date, and the bundle size is within budget. Report each check as pass or fail with details for failures. Give a final deploy/no-deploy recommendation. Create a command at .claude/commands/debug.md: I am experiencing this issue: $ARGUMENTS. Help me debug it. First, search the codebase for relevant code. Then check recent git changes that might have introduced the issue (git log --oneline -20 and git diff HEAD~5). Look at error logs if available. Form a hypothesis about the root cause. Suggest a targeted fix and explain your reasoning. If you are not confident, suggest diagnostic steps to narrow down the cause. Ask Claude Code: Create a command at .claude/commands/deps.md: Analyse the project dependencies. List all direct dependencies with their purpose, check for outdated packages (npm outdated), identify unused dependencies by scanning import statements, find duplicate packages in the dependency tree, check for security vulnerabilities, and report total dependency count and node_modules size. Recommend which dependencies to update, remove, or replace. Ask Claude Code: Create a command at .claude/commands/status.md: Give me a complete status report on this project. Show: git branch and recent commits, any uncommitted changes, test suite health (run tests and report pass/fail count), build health (attempt a build), dependency health (outdated and vulnerable packages), and TODO/FIXME/HACK comments in the codebase. This is your daily standup command — run it at the start of each session to understand the project state.

Team and project-specific commands

The most powerful slash commands are unique to your team's workflow. Ask Claude Code: Create a command at .claude/commands/onboard.md for helping new team members understand the codebase: You are helping a new developer understand this project. Read the CLAUDE.md, README, and package.json. Then provide a comprehensive onboarding briefing: what the project does (in 2-3 sentences), the tech stack and key dependencies, the directory structure and what lives where, how to run the project locally, how to run tests, the deployment process, the key architectural decisions and patterns used, and the 5 most important files to understand first. End with 3 suggested first tasks for a new developer to get familiar with the codebase. Create a command at .claude/commands/standup.md: Prepare my daily standup update. Check git log for my commits in the last 24 hours. Summarise what I worked on yesterday in 2-3 bullet points. Look at open TODO comments and recent branches to suggest what I should work on today. Flag any blockers you can identify (failing tests, unresolved merge conflicts, dependency issues). Format as: Yesterday, Today, Blockers. Ask Claude Code: Create a command at .claude/commands/refactor.md: Analyse the file or directory $ARGUMENTS for refactoring opportunities. Look for: duplicated code that could be extracted into shared utilities, functions that are too long (over 50 lines) and should be split, deeply nested conditionals that could be flattened, inconsistent patterns compared to the rest of the codebase, and missing error handling. For each opportunity, explain what to change and why. Prioritise by impact: which refactoring will improve maintainability the most? These team-specific commands encode your team's collective knowledge into reusable tools. When a senior developer writes a /review command that checks for the team's specific anti-patterns, they are scaling their expertise across the entire team.

Sharing and managing commands across teams

Slash commands become more valuable when shared. Ask Claude Code: Create a command management system. First, organise commands into categories. Create a README at .claude/commands/README.md that lists every available command with its name, description, arguments, and an example invocation. Group them by category: scaffolding, review, testing, deployment, and team. This README is the reference documentation for your command library. Ask Claude Code: Create a command at .claude/commands/help.md: List all available custom slash commands in this project. For each command, show the name, a one-line description based on the first line of the command file, and whether it takes arguments. Format as a clean table. This meta-command makes commands discoverable without reading the README. For sharing across projects, ask Claude Code: Create a script at scripts/sync-commands.sh that copies a shared set of commands from a central repository to the current project. The script should take a Git repository URL as an argument, clone or pull the latest commands, and copy them to .claude/commands/ without overwriting project-specific commands. This allows a team to maintain a shared command library in a dedicated repository and distribute updates to all projects. Ask Claude Code: Add versioning to commands. Add a YAML front matter block to each command file with version, author, and last-updated fields. Create a script that checks all commands against the shared repository and reports which ones are outdated. Commands are the highest-leverage customisation in Claude Code. A library of 15 to 20 well-crafted commands tailored to your workflow can save hours every week and ensure consistency across your team. Start with the commands in this guide, customise them for your project, and add new ones whenever you find yourself typing the same complex prompt more than twice.

Related Lesson

Extending AI Tools

This guide is hands-on and practical. The full curriculum covers the conceptual foundations in depth with structured lessons and quizzes.

Go to lesson