Claude Code Custom Slash Commands

Skills are the extension mechanism for Claude Code, through custom slash commands (e.g.,/deploy、/explain-code) to enhance Claude's capabilities.

Each custom command is essentially an independent Skill file that can encapsulate complex workflows, project conventions, and common operations, allowing you to invoke it quickly in any session.

Custom commands have been integrated into the Skills system — located at.claude/commands/deploy.mdthe command files and located at.claude/skills/deploy/SKILL.mdthe Skill files will all create/deploycommands, working in the same way.

In essence,.claude/commands/is the shorthand form of Skills, and the two directory structures are completely equivalent.


Why Use Custom Slash Commands

The core value of custom slash commands lies inStandardization + Reuse:

  • Codify Workflows: Encapsulate common multi-step operations (such as "deploy to production") into a single command
  • Ensure team consistency: Codify project conventions (such as "code review checklist") into commands to ensure team members follow the same standards
  • Reduce cognitive load: No need to memorize complex command combinations; a single sentence can trigger the complete workflow
  • Reduce repetitive input: High-frequency tasks change from multi-step operations to a single invocation

The experience of Anthropic's internal team is: tasks performed more than twice a day should be turned into Skills. Get it right once, and it keeps working.


File Structure

Each Skill is a directory that contains a requiredSKILL.mdfile:

my-skill/
├── SKILL.md           # 主指令文件(必需)
├── template.md        # Claude 填充的模板
├── examples/
│   └── sample.md      # 展示预期格式的示例
└── scripts/
    └── validate.sh    # Claude 可以执行的脚本

The file format consists of two parts:

  • YAML frontmatter(located---between the delimiters): used to configure metadata
  • Markdown body: the instructions Claude follows when invoked

Create Your First Custom Slash Command

1. Create a Skill Directory

Create the skill directory in an appropriate location. The storage location determines the command's scope:

mkdir -p ~/.claude/skills/explain-code
# 或者在项目中创建
mkdir -p .claude/skills/explain-code

2. Write the SKILL.md File

In the directory, createSKILL.md, containing YAML frontmatter and instructions:

Example

---
name
: explain-code
description
: Explain code with visual diagrams and analogies. When explaining how code works,
explaining a codebase, or when a user asks"How does this work?"use
---

When explaining code, always include the following:
1- **Start with an analogy**: Compare the code to everyday things
2- **Draw a diagram**: Use ASCII art to show the flow, structure, or relationships
3- **Walk through the code step by step**: Explain step by step what happens
4- **Point out a pitfall**: What are the common mistakes or misconceptions?

3. Use the Custom Command

Directly invoke it in Claude Code:

/explain-code src/auth/login.ts

Claude will automatically match a skill that fits the description, or you can invoke it explicitly.


Configuration Fields Explained

Field Description
name Display name; becomes a slash command (format: lowercase letters, numbers, hyphens, up to 64 characters)
description Skill function and when to use it (recommended to fill in; Claude's auto-invocation depends on this field)
argument-hint Prompt shown during autocomplete (e.g.[filename] [format])
disable-model-invocation Set totruewhen set, prevents Claude from auto-invoking; only takes effect when you explicitly invoke it
user-invocable Set tofalsewhen set, from/hidden from the menu; only Claude can invoke it
allowed-tools List of tools Claude can use without asking
context Set toforkwhen set, runs in a branch subagent
agent Setcontext: forkthe subagent type used when set
paths List of Glob patterns, restricting the file scope for skill activation

Invocation Control

Control who can invoke the skill via frontmatter:

Configuration You can invoke Claude can invoke
(default) Yes Yes
disable-model-invocation: true Yes no
user-invocable: false no Yes

Variable Substitution

Use variables in the skill content, and Claude will replace them automatically:

Variable Description
$ARGUMENTS All arguments passed at invocation
$ARGUMENTS[N] Access a specific parameter by 0-based index
$N $ARGUMENTS[N]shorthand for
${CLAUDE_SESSION_ID} Current session ID
${CLAUDE_SKILL_DIR} Directory where the skill's SKILL.md file is located

Skill Storage Locations

Skills can be stored in multiple locations with different scopes:

Location Path Scope
Enterprise level Determined by managed settings All users in the organization
Personal ~/.claude/skills/<skill-name>/SKILL.md All projects
Project .claude/skills/<skill-name>/SKILL.md Current project only
Plugin <plugin>/skills/<skill-name>/SKILL.md Within the plugin's enabled scope

.claude/commands/Files within them are still valid, supporting the same frontmatter. If a skill and a command share the same name, the skill takes precedence.


Advanced Examples

Example 1: Deployment Command (Manual Invocation Only)

Example

---
name
: deploy
description
: Deploy the application to the production environment
disable-model-invocation
: true
---

Deploy $ARGUMENTS to the production environment:
1. Run the test suite
2. Build the application
3. Push to the deployment target

Example 2: Component Migration Command with Arguments

Example

---
name
: migrate-component
description
: Migrate a component from one framework to another
---

Migrate the $0 component from $1 to $2.
Preserve all existing behavior and tests.

Usage: /migrate-component SearchBar React Vue

Example 3: PR Summary Command with Dynamic Context

Example

---
name
: pr-summary
description
: Summarize the changes of a Pull Request
context
: fork
agent
: Explore
allowed-tools
: Bash(gh *)
---

## Pull Request Context
- PR diff
: !`gh pr diff`
- PR comments
: !`gh pr view --comments`
- Changed files
: !`gh pr diff --name-only`

## Your task
Summarize this Pull Request...

Built-in Slash Commands

Claude Code provides some built-in commands that work out of the box:

Command Function
/batch <instruction> Orchestrate large-scale changes across the codebase
/claude-api Load API reference materials
/debug [description] Enable debug logging
/loop [interval] <prompt> Run prompts repeatedly at intervals
/simplify [focus] Review code quality issues

Best Practices

1. How to Write description

The description is the basis for Claude's auto-invocation, so be sure to write it clearly:

  • Clarify trigger timing:Call when the user requests review / analysis / inspection of code quality
  • State preconditions:Use after the specification document has been confirmed
  • Write clear boundaries: cases in which it should not be invoked

2. Single Responsibility Principle

Each skill should do only one thing, with clear inputs/outputs:

  • /deploy: only responsible for deployment
  • /test: only responsible for running tests
  • /review: only responsible for code review

3. Set Invocation Permissions as Needed

  • Dangerous operations (deploy, delete): Settingsdisable-model-invocation: true
  • Claude auxiliary tasks: Settingsuser-invocable: false
  • General workflow: use default configuration

4. Use allowed-tools to Restrict Permissions

Set minimal permissions for security-critical operations:

Example

---
name
: read-only-analyzer
description
: Read-only code analysis, for reviewing and understanding code
allowed-tools
: Read, Grep, Glob, Bash(gh *)  # Only allow these tools
---

Execute read-only code analysis...

Team Sharing Strategy

Commit project-level skills to git for team sharing:

my-project/
├── .claude/
│   ├── skills/
│   │   ├── deploy/
│   │   │   └── SKILL.md
│   │   ├── test/
│   │   │   └── SKILL.md
│   │   └── review/
│   │       └── SKILL.md
│   └── commands/  # 也可以用 commands/ 目录
└── CLAUDE.md

Reference and explain project-specific skills in CLAUDE.md:

## 自定义斜杠命令

本项目提供以下自定义斜杠命令:

- `/deploy [env]` — 部署到指定环境(dev/staging/prod)
- `/test [module]` — 运行测试套件或指定模块
- `/review` — 执行标准代码审查

详细使用说明请参考 @.claude/skills/README.md

FAQ

Q: What is the difference between skills and the commands directory?

Essentially no difference..claude/commands/Yes.claude/skills/shorthand form, both formats work fine. If they share the same name, skills take priority.

Q: How do I pass multiple parameters when calling?

Use quotes to wrap multiple parameters:/migrate SearchBar "from:React to:Vue", then use it in the skill$ARGUMENTSto get the complete string.

Q: Can skills be used in sub-agents?

Yes. By setting thecontext: forkandagentfield, sub-agents can run skills in an independent context.

Q: How do I make Claude remember commonly used skills?

List all available custom commands and their usage scenarios in CLAUDE.md, and Claude will automatically match the appropriate skill based on the descriptions.

Other extensions