Skip to content

[feat] custom slash commands #299

@monotykamary

Description

@monotykamary

There's an opportunity to add support for custom slash commands similar to Claude Code's slash command system, allowing users to create reusable prompts and workflows as markdown files.

Background

Currently, opencode has a keybinding-based command system with built-in triggers (like /help, /new, /sessions), but lacks the ability to create custom slash commands. Claude Code provides a powerful slash command system that allows users to:

  • Create custom commands as .md files
  • Support both project-level and personal commands
  • Use dynamic arguments and bash execution
  • Reference files directly in commands

Proposed Feature

Custom Slash Commands

Allow users to create custom slash commands by placing .md files in:

  • Project commands: .opencode/commands/*.md (shared with team)
  • Personal commands: ~/.opencode/commands/*.md (user-specific)

Command Syntax

/project:<command-name> [arguments]
/user:<command-name> [arguments]

Command File Format

Commands would be markdown files with optional YAML frontmatter:

---
description: "Analyze code for performance issues"
allowed-tools: ["bash", "read", "edit"]
---

Analyze this code for performance issues and suggest optimizations:

$ARGUMENTS

Key Features

  1. Namespacing: Support subdirectories for organization

    • .opencode/commands/frontend/component.md/project:frontend:component
  2. Dynamic Arguments: Use $ARGUMENTS placeholder for user input

    # Usage: /project:fix-issue 123
    # Command: Fix issue #$ARGUMENTS following our coding standards
  3. Bash Command Execution: Execute bash commands with ! prefix

    ## Context
    - Current git status: !`git status`
    - Recent commits: !`git log --oneline -10`
  4. File References: Include file contents with @ prefix

    Review the implementation in @src/utils/helpers.js
    Compare @src/old-version.js with @src/new-version.js

Implementation Plan

1. Command File System

  • Create command directory scanning logic
  • Support both .opencode/commands/ (project) and ~/.opencode/commands/ (user)
  • Parse markdown files with YAML frontmatter

2. Command Parser

  • Extend existing completion system in packages/tui/internal/completions/
  • Add custom command discovery and parsing
  • Support namespaced command syntax

3. Command Execution Engine

  • Extend command registry in packages/tui/internal/commands/command.go
  • Implement dynamic command registration from files
  • Add argument substitution ($ARGUMENTS)
  • Add bash execution support (! prefix)
  • Add file reference support (@ prefix)

4. Integration Points

  • Modify chat editor to handle custom slash commands
  • Update completion provider to include custom commands
  • Extend command execution logic in TUI

Benefits

  1. Improved Workflow: Users can create reusable prompts for common tasks
  2. Team Collaboration: Project-level commands can be shared via version control
  3. Consistency: Standardize common workflows across team members
  4. Flexibility: Support for dynamic arguments and file references
  5. Extensibility: Easy to add new commands without code changes

Examples

Project Command: Code Review

.opencode/commands/review.md:

---
description: "Perform code review with context"
---

## Context
- Current branch: !`git branch --show-current`
- Modified files: !`git diff --name-only`

## Task
Review the following changes for:
- Code quality and best practices
- Security vulnerabilities
- Performance implications

$ARGUMENTS

Usage: /project:review Please focus on the authentication logic

Personal Command: Quick Fix

~/.opencode/commands/quickfix.md:

---
description: "Quick bug fix with standard format"
---

Fix the following issue following our coding standards:

Issue: $ARGUMENTS

Please:
1. Identify the root cause
2. Implement a minimal fix
3. Add appropriate tests
4. Update documentation if needed

Usage: /user:quickfix Button click handler not working on mobile

Related Work

This feature is inspired by Claude Code's slash command system, which provides similar functionality. The implementation would leverage opencode's existing command and completion infrastructure.

Acceptance Criteria

  • Support creating custom commands via .md files
  • Implement project-level (.opencode/commands/) and user-level (~/.opencode/commands/) command directories
  • Support namespaced commands via subdirectories
  • Implement $ARGUMENTS placeholder for dynamic content
  • Support bash command execution with ! prefix
  • Support file references with @ prefix
  • Integrate with existing completion system
  • Commands appear in / completion dropdown
  • Maintain backward compatibility with existing command system

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions