| name | code-formatting |
| description | Format code according to project standards and fix linting issues |
| auto-activates | ["format code","fix formatting","run formatter","code style","linting errors"] |
Skill: Code Formatting
When to Use This Skill
This skill activates when you need to:
- Format code to match project style guidelines
- Fix linting errors
- Set up automatic code formatting
- Understand project formatting standards
Prerequisites
Check project for existing formatting configuration:
.prettierrc, .eslintrc (JavaScript/TypeScript)
.black, pyproject.toml, ruff.toml (Python)
.rustfmt.toml (Rust)
.gofmt settings (Go)
Step-by-Step Workflow
Step 1: Detect Formatting Tools
Check {{lint_command}} or look for:
package.json scripts: "format", "lint", "lint:fix"
- Configuration files in project root
.pre-commit-config.yaml for pre-commit hooks
- CI/CD workflows for formatting checks
Step 2: Format Code by Tech Stack
Python
Option A: Black (Opinionated)
pip install black
black .
black myfile.py
black --check .
black --diff .
Add to pyproject.toml:
[tool.black]
line-length = 88
target-version = ['py310']
include = '\.pyi?$'
extend-exclude = '''
/(
# directories
\.eggs
| \.git
| \.venv
| build
| dist
)/
'''
Option B: Ruff (Fast, Modern)
pip install ruff
ruff format .
ruff check --fix .
ruff check .
Add ruff.toml:
line-length = 88
target-version = "py310"
[lint]
select = ["E", "F", "I", "N", "W"]
ignore = ["E501"]
[format]
quote-style = "double"
indent-style = "space"
Option C: autopep8
pip install autopep8
autopep8 --in-place --aggressive --aggressive myfile.py
find . -name '*.py' -exec autopep8 --in-place --aggressive --aggressive {} \;
JavaScript/TypeScript
Option A: Prettier
npm install --save-dev prettier
npx prettier --write .
npx prettier --write "src/**/*.{js,jsx,ts,tsx,json,css,md}"
npx prettier --check .
Add .prettierrc:
{
"semi": true,
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2,
"useTabs": false
}
Option B: ESLint (with autofix)
npm install --save-dev eslint
npx eslint --fix .
npx eslint --fix src/**/*.js
Combined (Prettier + ESLint):
npx prettier --write . && npx eslint --fix .
Add to package.json:
{
"scripts": {
"format": "prettier --write .",
"lint": "eslint .",
"lint:fix": "eslint --fix ."
}
}
Go
Built-in gofmt:
gofmt -w .
gofmt -w main.go
gofmt -l .
Alternative: goimports (adds/removes imports)
go install golang.org/x/tools/cmd/goimports@latest
goimports -w .
Option: golangci-lint (comprehensive)
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
golangci-lint run --fix
Rust
Built-in rustfmt:
cargo fmt
cargo fmt -- --check
Add rustfmt.toml:
max_width = 100
hard_tabs = false
tab_spaces = 4
edition = "2021"
Clippy (Rust linter):
cargo clippy
cargo clippy --fix
Ruby
RuboCop:
gem install rubocop
rubocop -A
rubocop
rubocop --safe-auto-correct
Add .rubocop.yml:
AllCops:
TargetRubyVersion: 3.0
NewCops: enable
Style/StringLiterals:
EnforcedStyle: single_quotes
Java
Google Java Format:
wget https://github.com/google/google-java-format/releases/download/v1.17.0/google-java-format-1.17.0-all-deps.jar
java -jar google-java-format-1.17.0-all-deps.jar -i MyFile.java
find . -name "*.java" -exec java -jar google-java-format-1.17.0-all-deps.jar -i {} \;
Step 3: Set Up Automatic Formatting
VS Code
Create .vscode/settings.json:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[go]": {
"editor.formatOnSave": true
}
}
Pre-commit Hooks
Install pre-commit:
pip install pre-commit
Create .pre-commit-config.yaml:
For Python:
repos:
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.280
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
For JavaScript/TypeScript:
repos:
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0
hooks:
- id: prettier
types_or: [javascript, jsx, ts, tsx, json, css, markdown]
- repo: https://github.com/pre-commit/mirrors-eslint
rev: v8.44.0
hooks:
- id: eslint
args: [--fix]
Install hooks:
pre-commit install
Run on all files:
pre-commit run --all-files
Step 4: Check Formatting in CI
GitHub Actions example (.github/workflows/format-check.yml):
Python:
name: Format Check
on: [push, pull_request]
jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- run: pip install black ruff
- run: black --check .
- run: ruff check .
JavaScript:
name: Format Check
on: [push, pull_request]
jobs:
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm ci
- run: npm run lint
- run: npx prettier --check .
Common Issues and Solutions
Issue: Formatter not found or command fails
Solutions:
- Install formatter:
pip install black or npm install -D prettier
- Check if it's in package.json/requirements.txt
- Activate virtual environment (Python)
- Clear npm cache:
npm cache clean --force
Issue: Formatting conflicts between tools
Solutions:
- Choose one primary formatter (Black/Prettier)
- Configure linter to defer to formatter
- Use prettier-eslint or eslint-config-prettier for JS
- Disable formatting rules in linter config
Issue: Files keep getting reformatted back and forth
Solutions:
- Ensure all developers use same formatter version
- Commit .prettierrc, .black, etc. to repo
- Run formatter in CI to enforce
- Check for conflicting editor settings
Issue: Some files excluded from formatting
Solutions:
- Check .prettierignore, .gitignore, exclude patterns
- Verify file extensions are included
- Check formatter configuration
- Run with verbose flag to see what's excluded
Success Criteria
- ✅ Code formatted consistently across all files
- ✅ No formatting errors in output
- ✅ Linting errors fixed or documented
- ✅ Format-on-save configured (optional)
- ✅ Pre-commit hooks working (optional)
Quick Reference Commands
| Language | Format Command | Check Command |
|---|
| Python (Black) | black . | black --check . |
| Python (Ruff) | ruff format . | ruff check . |
| JavaScript | prettier --write . | prettier --check . |
| Go | gofmt -w . | gofmt -l . |
| Rust | cargo fmt | cargo fmt -- --check |
| Ruby | rubocop -A | rubocop |
Related Skills
- local-dev-setup - Initial project setup
- git-workflow - Git pre-commit integration
- ci-pipeline - CI formatting checks
Documentation References