occtx is a command-line tool for switching between different opencode contexts quickly and easily.
- π Fast context switching - Switch between opencode configurations instantly
- π Multi-level support - Global (
~/.config/opencode/) and project-level (./opencode/) contexts - π¨ Interactive mode - Built-in fuzzy finder with fzf integration
- π Multiple formats - Support for JSON and JSONC (JSON with Comments)
- π― Type-safe - Enum-based format validation
- πΎ State persistence - Remembers current and previous contexts
- π‘οΈ Safe operations - Atomic file operations prevent corruption
# Add the tap
brew tap hungthai1401/tap
# Install occtx
brew install hungthai1401/tap/occtxTo update:
brew update
brew upgrade hungthai1401/tap/occtxgit clone https://github.com/hungthai1401/occtx.git
cd occtx
go build -o occtx .
sudo mv occtx /usr/local/bin/Download the appropriate binary for your platform from the releases page.
Make the binary executable and place it in your PATH:
chmod +x occtx-*
sudo mv occtx-* /usr/local/bin/occtx- Right-click on 'This PC' or 'My Computer' and select 'Properties'.
- Click on 'Advanced system settings'.
- Click on 'Environment Variables'.
- Under 'System variables', find the 'Path' variable and click 'Edit'.
- Click 'New' and add the path to the directory where
occtx.exeis located. - Click 'OK' to close all dialog boxes.
go install github.com/hungthai1401/occtx@latest# List all contexts
occtx
# Switch to a context
occtx work
# Switch to previous context
occtx -
# Show current context
occtx -c# Create new context from current settings
occtx -n personal
# Create context with specific format
occtx -n work -f jsonc
# Delete a context
occtx -d old-context
# Rename a context
occtx -r old-name new-name
# Unset current context
occtx -u# Interactive selection (flag form)
occtx -i
# Interactive selection (command form)
occtx interactive# Show context content
occtx -s work
# Edit context with $EDITOR
occtx -e work
# Export context to stdout
occtx --export work
# Import context from stdin
echo '{"apiKey": "key"}' | occtx --import new-context# Use project-level contexts
occtx --in-project
# Create project-level context
occtx --in-project -n local-dev
# List project contexts
occtx --in-projectocctx -n my-context # Creates my-context.jsonStandard JSON format:
{
"theme": "default",
"provider": {
"anthropic": {
"api": "https://api.anthropic.com",
"options": {
"apiKey": "your-api-key",
"timeout": 30000
}
}
},
"agent": {
"default": {
"provider": "anthropic",
"model": "claude-4-sonnet"
}
}
}occtx -n my-context -f jsonc # Creates my-context.jsoncJSONC format with metadata:
# Create contexts for different environments
occtx -n development -f json
occtx -n production -f jsonc
occtx -n staging
# List all contexts
occtx
π€ Global contexts:
development
* production
staging
# Switch between contexts
occtx development
occtx production
occtx - # Back to development
# Interactive selection
occtx -i # Opens fuzzy finder# Work with project-level contexts
cd my-project
occtx --in-project -n local
occtx --in-project -n test
# List shows both levels
occtx
π€ Global contexts:
development
* production
π‘ Hint: Found 2 project-level contexts. Use --in-project to see them.- Global contexts:
~/.config/opencode/settings/*.json - Project contexts:
./opencode/settings/*.json - Active config:
~/.config/opencode/opencode.jsonor./opencode.json - State file:
.occtx-state.json(tracks current/previous contexts)
- fzf integration: Auto-detects and uses
fzfif available - Built-in finder: Fallback fuzzy finder using promptui
- Color coding: Current context highlighted in green
- Visual indicators: Emojis for different context levels
- Go 1.21 or later
- Optional: fzf for enhanced interactive mode
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
go build -o occtx .# Run all tests
go test -v ./test
# Run tests with coverage
go test -cover ./...
# Run benchmarks
go test -bench=. ./test
# Run specific test
go test -run TestContextFormat ./test
# Race detection
go test -race ./test
# Lint and vet
go vet ./...
go fmt ./...# Test with sample opencode config
cat > ~/.config/opencode/opencode.json << EOF
{
"theme": "default",
"provider": {
"anthropic": {
"api": "https://api.anthropic.com",
"options": {
"apiKey": "your-key-here"
}
}
},
"agent": {
"default": {
"provider": "anthropic",
"model": "claude-4-sonnet"
}
}
}
EOF
# Test context creation and switching
./occtx -n development
./occtx -n production -f jsonc
./occtx development
./occtx -c
# Test interactive mode
./occtx -i
# Test project-level contexts
./occtx --in-project -n local-dev
./occtx --in-project
# Clean up test contexts
./occtx -d development
./occtx -d production
./occtx --in-project -d local-devThis project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by kubectx - Kubernetes context switcher
- Inspired by cctx - Claude Code context management tool
- Built with Cobra CLI framework
- Interactive features powered by promptui
If you encounter the message "Apple could not verify 'occtx' is free of malware that may harm your Mac or compromise your privacy," you can resolve this by running the following command in your terminal:
xattr -cr /usr/local/bin/occtxThis command clears the extended attributes that are causing the warning, allowing you to run occtx without further issues.
If you encounter a SmartScreen warning when trying to run occtx, you can bypass it by following these steps:
- Right-click on the
occtxexecutable. - Select 'Properties'.
- In the 'General' tab, look for a security warning message and check the box that says 'Unblock'.
- Click 'Apply', then 'OK'.
This will allow you to run occtx without further SmartScreen warnings.