Getting Hands OnLesson 9 of 18

Custom Commands

Creating Custom Slash Commands

Custom commands let you define reusable automation workflows accessible via slash notation. Each command is a markdown file in your project's .claude/commands/ directory. The filename becomes the command name -- audit.md creates the /audit command.

bashCustom command file structure
# Directory structure
your-project/
  .claude/
    commands/
      audit.md        # Creates /audit command
      test-gen.md     # Creates /test-gen command
      fix-vuln.md     # Creates /fix-vuln command
markdownExample: dependency audit command
<!-- .claude/commands/audit.md -->
Review the dependency list in package.json.
For each dependency:
1. Check if there are known vulnerabilities
2. Check if a newer major version exists
3. Flag any that are unmaintained (no updates in 12+ months)

Output a markdown table with columns:
| Package | Current | Latest | Vulnerabilities | Status |

Arguments

Use the $ARGUMENTS placeholder in your command file to accept runtime parameters. For example: 'Generate unit tests for the file at $ARGUMENTS' lets you run '/test-gen src/auth/login.ts'.

Effective Command Design

Well-designed custom commands are specific, actionable, and include clear output format expectations. They turn multi-step workflows into single-command operations.

markdownExample: parameterized test generation command
<!-- .claude/commands/test-gen.md -->
Generate comprehensive unit tests for: $ARGUMENTS

Requirements:
- Use the project's test framework (check package.json)
- Include edge cases and error handling tests
- Mock external dependencies
- Follow existing test file naming conventions
- Aim for >90% branch coverage of the target file
  • Keep commands focused on one task -- compose multiple commands for complex workflows
  • Include output format expectations (tables, lists, file structure) for consistent results
  • Use $ARGUMENTS for parameters that change per invocation (file paths, descriptions)
  • Restart Claude Code after creating or modifying command files for changes to take effect
⚠️

Remember

You must restart Claude Code after creating or modifying command files. The commands are loaded at startup and changes won't be picked up during an active session.

Key Takeaways

  • Custom commands are markdown files in .claude/commands/ -- the filename becomes the /command name
  • Use $ARGUMENTS placeholder to accept runtime parameters like file paths or descriptions
  • Design commands to be specific, actionable, and include expected output format
  • Commands turn multi-step workflows into single-command operations for your team
  • Always restart Claude Code after creating or modifying command files

Check Your Understanding

Test what you learned in this lesson.

Q1.Where do custom command files go in your project?

Q2.You create a command file called test-gen.md with the content 'Generate tests for $ARGUMENTS'. How do you use it?

Q3.After creating a new command file, what must you do before it becomes available?