code-formatter
Automatically format code files using project-specific formatters. Use when editing code files or when the user asks to format code.
SKILL.mdexamples/code-formatter-with-hooks/SKILL.md
GitHubCode Formatter
This skill automatically formats code files after editing or writing, using project-specific formatters.
How It Works
When you edit or write code files, a post-edit hook runs to format the file using:
- Python:
blackorautopep8 - JavaScript/TypeScript:
prettier - Go:
gofmt - Rust:
rustfmt
Instructions
- Edit or write code as normal
- Automatic formatting happens via the PostToolUse hook
- Formatted code is applied back to the file
Hook Script
The hook script (.claude/hooks/format-code.sh) should:
- Detect file type from extension
- Run appropriate formatter
- Handle errors gracefully
- Return formatted content
Example Hook Script
#!/bin/bash
# .claude/hooks/format-code.sh
FILE_PATH=$(echo "$1" | jq -r '.file_path')
EXT="${FILE_PATH##*.}"
case "$EXT" in
py)
black "$FILE_PATH" 2>/dev/null || autopep8 -i "$FILE_PATH"
;;
js|jsx|ts|tsx)
prettier --write "$FILE_PATH" 2>/dev/null
;;
go)
gofmt -w "$FILE_PATH"
;;
rs)
rustfmt "$FILE_PATH"
;;
esac
exit 0
Setup
- Create the hook script at
.claude/hooks/format-code.sh - Make it executable:
chmod +x .claude/hooks/format-code.sh - Install formatters for your languages