Quick references for code formatting tools and style best practices.
bash
# Install
pip install black
# Format file
black file.py
# Format directory
black src/
# Check without modifying
black --check file.py
# Configuration (pyproject.toml)
[tool.black]
line-length = 88
target-version = ['py310']
bash
# Install
pip install isort
# Sort imports
isort file.py
isort src/
# Black-compatible config
[tool.isort]
profile = "black"
bash
# Install
pip install flake8
# Lint
flake8 file.py
flake8 src/
# Config (setup.cfg)
[flake8]
max-line-length = 88
extend-ignore = E203, W503
bash
# Install
pip install ruff
# Lint
ruff check file.py
# Fix automatically
ruff check --fix file.py
# Format
ruff format file.py
bash
# Install
npm install -D prettier
# Format
npx prettier --write file.js
npx prettier --write src/
# Check
npx prettier --check file.js
# Config (.prettierrc)
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"printWidth": 80
}
# Ignore (.prettierignore)
node_modules/
build/
dist/
bash
# Install
npm install -D eslint
# Initialize
npx eslint --init
# Lint
npx eslint file.js
npx eslint src/
# Fix
npx eslint --fix file.js
# Config (.eslintrc.js)
module.exports = {
extends: ['eslint:recommended'],
parserOptions: { ecmaVersion: 2022 },
rules: {
'no-unused-vars': 'warn',
'no-console': 'warn'
}
}
json
// settings.json
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
}
}
bash
# Pretty print JSON
jq '.' file.json
# Compact (one line)
jq -c '.' file.json
# Sort keys
jq -S '.' file.json
bash
npx prettier --write file.json
npx prettier --write *.json
bash
python3 -m json.tool file.json
python3 -m json.tool file.json --sort-keys
bash
npx prettier --write README.md
npx prettier --write docs/
bash
# Install
npm install -D markdownlint-cli
# Lint
npx markdownlint README.md
npx markdownlint docs/
# Fix
npx markdownlint --fix README.md
# Header not Header =====Create .editorconfig in project root:
ini
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = space
indent_size = 2
[*.py]
indent_size = 4
[*.{json,yml,yaml}]
indent_size = 2
[*.md]
trim_trailing_whitespace = false
LanguageToolCommandPythonblackblack file.pyPythonruffruff format file.pyJS/TSPrettiernpx prettier --write file.jsJSONjqjq '.' file.jsonMarkdownPrettiernpx prettier --write README.mdShellshfmtshfmt -w script.shGogofmtgofmt -w file.goRustrustfmtrustfmt file.rs
.prettierrc, .eslintrc, pyproject.toml, .editorconfigpre-commit or husky to enforceyaml
repos:
- repo: https://github.com/psf/black
rev: 23.12.1
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
- id: prettier