Claude Code中英文系列教程:创建自己的斜杠快捷命令

Create custom slash commands

创建自定义斜杠命令

Claude Code supports custom slash commands that you can create to quickly execute specific prompts or tasks.

Claude Code 支持自定义斜杠命令,你可以通过创建这些命令快速执行特定的提示或任务。

一,Create project-specific commands

创建项目特定命令

Suppose you want to create reusable slash commands for your project that all team members can use.

假设你想为你的项目创建可重用的斜杠命令,所有团队成员都可以使用。

1.1 Create a commands directory in your project

在你的项目中创建一个 commands 目录

mkdir -p .claude/commands

1.2 Create a Markdown file for each command

为每个命令创建一个 Markdown 文件

echo "Analyze the performance of this code and suggest three specific optimizations:" > .claude/commands/optimize.md

分析这段代码的性能,并提出三个具体的优化建议

1.3 Use your custom command in Claude Code

在 Claude Code 中使用自定义命令

> /optimize

使用技巧:

Command names are derived from the filename (for example, optimize.md becomes /optimize)

命令名称源自文件名(例如, optimize.md 变为 /optimize )

You can organize commands in subdirectories (for example, .claude/commands/frontend/component.md

creates /component with "(project:frontend)" shown in the description)

您可以将命令组织在子目录中(例如, .claude/commands/frontend/component.md 会创建一个 /component 命令,并在描述中显示"(project:frontend)")

Project commands are available to everyone who clones the repository

项目命令可供所有克隆代码仓库的用户使用

The Markdown file content becomes the prompt sent to Claude when the command is invoked

当命令被调用时,Markdown 文件的内容将成为发送给 Claude 的提示词

二,Add command arguments with $ARGUMENTS

使用$ARGUMENTS 添加命令参数

Suppose you want to create flexible slash commands that can accept additional input from users.

假设你想创建灵活的斜杠命令,这些命令可以接受用户的额外输入

2.1,Create a command file with the $ARGUMENTS placeholder

创建一个包含$ARGUMENTS 占位符的markdwon命令文件

echo 'Find and fix issue #$ARGUMENTS. Follow these steps:

1.Understand the issue described in the ticket

2.Locate the relevant code in our codebase

3.Implement a solution that addresses the root cause

4.Add appropriate tests

5.Prepare a concise PR description' >

.claude/commands/fix-issue.md

找到并修复问题#$ARGUMENTS。遵循以下步骤:

1.理解issue中描述的问题。

2.在我们的代码库中找到相关的代码。

3.实现一个解决根本原因的解决方案。

4.添加适当的测试。

5.准备一份简洁的PR描述

2.2,Use the command with an issue number

使用带有问题编号的命令

In your Claude session, use the command with arguments.

在您的 Claude 会话中,使用带参数的命令。

> /fix-issue 123

This replaces $ARGUMENTS with "123" in the prompt.

这将在提示中替换$ARGUMENTS 为"123"。

使用技巧:

The $ARGUMENTS placeholder is replaced with any text that follows the command

$ARGUMENTS 占位符会被替换为命令后面的任何文本

You can position $ARGUMENTS anywhere in your command template

您可以在命令模板中的任何位置放置$ARGUMENTS

Other useful applications:

generating test cases for specific functions,

creating documentation for components,

reviewing code in particular files,

or translating content to specified languages

其他实用应用,可以创建这些命令方便后续使用:

为特定函数生成测试用例、

为组件创建文档、

在特定文件中审查代码

或将内容翻译成指定语言

三,Create personal slash commands

创建个人斜杠命令

Suppose you want to create personal slash commands that work across all your projects.

假设你想创建可在所有项目中使用的个人斜杠命令。

3.1 Create a commands directory in your home folder

在你的家目录中创建一个 commands 目录

mkdir -p ~/.claude/commands

3.2 Create a Markdown file for each command

为每个命令创建一个 Markdown 文件

echo "Review this code for security vulnerabilities, focusing on:" >

~/.claude/commands/security-review.md

查看此代码中的安全漏洞,重点关注:

3.3 Use your personal custom command

使用你的个人自定义命令

> /security-review

使用技巧:

Personal commands show "(user)" in their description when listed with /help

个人命令在 /help 列表中显示时,描述中会标有"(user)"

Personal commands are only available to you and not shared with your team

个人命令仅对您可见,不会与您的团队共享

Personal commands work across all your projects

个人命令适用于您所有的项目

You can use these for consistent workflows across different codebases

您可以使用这些命令在不同代码库中实现一致的工作流程

相关推荐
小村儿6 小时前
连载04-最重要的Skill---一起吃透 Claude Code,告别 AI coding 迷茫
前端·后端·ai编程
北冥有羽Victoria8 小时前
OpenCLI 操作网页 从0到1完整实操指南
vscode·爬虫·python·github·api·ai编程·opencli
Thomas.Sir8 小时前
GitHub Copilot从入门到精通【从基础补全到智能代理,解锁AI编程全技能】
github·copilot·ai编程
加瓦点灯9 小时前
Vibe Coding 最佳实践:人控架构,AI执行
ai编程
财经资讯数据_灵砚智能10 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年4月9日
人工智能·python·信息可视化·自然语言处理·ai编程
恋猫de小郭10 小时前
手机直接运行 Codex/OpenCode/Claude Code ,实时管理你的 AI Coding
前端·openai·ai编程
JaydenAI10 小时前
[FastMCP设计、原理与应用-02]以命令行和客户端与MCP服务器交互
ai编程·ai agent·mcp·fastmcp
财经资讯数据_灵砚智能11 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月8日
大数据·人工智能·信息可视化·自然语言处理·ai编程
程序员鱼皮11 小时前
AI 最需要的 15 个开源项目,装完直接起飞!
ai·程序员·开源·编程·ai编程
好运的阿财1 天前
process 工具与子agent管理机制详解
网络·人工智能·python·程序人生·ai编程