Strip comments and documentation from source files using tree-sitter
Project description
strip-comments
Strip comments and documentation from source files using tree-sitter. Blank lines are removed from the output by default, producing compact, token-efficient code. Designed for AI coding agents to read code without comment noise.
Installation
pip
pip install strip-comments
To support additional languages, install their tree-sitter grammar:
pip install tree-sitter-javascript tree-sitter-typescript
Or install all supported grammars at once:
pip install strip-comments[all]
uv / uvx
Run without installing:
uvx strip-comments myfile.py
With all languages:
uvx --with strip-comments[all] strip-comments myfile.py
Or install as a tool:
uv tool install strip-comments
# with all languages
uv tool install strip-comments[all]
Supported Languages
The following languages are supported. Only Python is required by default; other grammars are optional dependencies.
| Language | Extension | Install Command |
|---|---|---|
| Python | .py |
(included) |
| JavaScript | .js |
pip install tree-sitter-javascript |
| TypeScript | .ts |
pip install tree-sitter-typescript |
| TSX | .tsx |
pip install tree-sitter-typescript |
| HTML | .html |
pip install tree-sitter-html |
| CSS | .css |
pip install tree-sitter-css |
| Go | .go |
pip install tree-sitter-go |
| Rust | .rs |
pip install tree-sitter-rust |
| C | .c |
pip install tree-sitter-c |
| C++ | .cpp |
pip install tree-sitter-cpp |
| Java | .java |
pip install tree-sitter-java |
| Ruby | .rb |
pip install tree-sitter-ruby |
| PHP | .php |
pip install tree-sitter-php |
| Bash | .sh |
pip install tree-sitter-bash |
| JSON | .json |
pip install tree-sitter-json |
| Lua | .lua |
pip install tree-sitter-lua |
| Swift | .swift |
pip install tree-sitter-swift |
| Kotlin | .kt |
pip install tree-sitter-kotlin |
| TOML | .toml |
pip install tree-sitter-toml |
| Dockerfile | .dockerfile |
pip install tree-sitter-dockerfile |
| C# | .cs |
pip install tree-sitter-c-sharp |
| Zig | .zig |
pip install tree-sitter-zig |
Usage
Strip comments from a single file:
strip-comments myfile.py
Process all files in a directory:
strip-comments src/
Pass glob patterns directly:
strip-comments "src/**/*.py"
strip-comments "tests/test_*.py"
Filter by glob pattern:
strip-comments src/ --include "*.py" --exclude "test_*.py"
When using recursive patterns (**), common noise directories are automatically excluded:
.git/, node_modules/, __pycache__/, .venv/, venv/, .tox/, dist/, build/,
.eggs/, .mypy_cache/, .pytest_cache/, .ruff_cache/, .idea/, .vscode/, and *.egg-info/.
To disable this and search all directories:
strip-comments src/ --include "**/*.py" --no-default-excludes
Pipe source through stdin (useful for editor integrations):
cat myfile.py | strip-comments --stdin --lang py
Output as JSON:
strip-comments src/ --json > output.json
Coding Agent Integration
Works with any agent that reads an instructions file. Add the snippet below to
CLAUDE.md, AGENTS.md, .cursorrules, or your tool's equivalent:
## Tool: strip-comments
When reading source files to explore or understand the codebase, use
`strip-comments` instead of reading the raw file. It removes comments, docstrings,
and blank lines, giving you the same code with fewer tokens.
```bash
strip-comments <file> # single file
strip-comments <file1> <file2> ... # multiple files (each under a "=== path ===" header)
strip-comments <dir> --include "**/*.py" # directory, recursive filter
strip-comments "src/**/*.py" # glob pattern
cat <file> | strip-comments --stdin --lang py # pipe via stdin
```
Read the raw file instead when you are about to edit it, or when the comments or
docstrings themselves are relevant — stripped output has shifted line numbers and
omits text, so it must not be used as the basis for edits.
Options
--json— Output as JSON instead of plain text--include PATTERN— Include files matching glob pattern--exclude PATTERN— Exclude files matching glob pattern--no-default-excludes— Do not exclude common directories--stdin— Read source from stdin (requires--lang)-l, --lang— Language for stdin mode (e.g.py,.py,python)--stdin-filename— Display name for stdin output (default:<stdin>)
Development
Run the test suite:
python -m unittest discover -s tests -v
Requires Python 3.13+.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strip_comments-2026.6.20.tar.gz.
File metadata
- Download URL: strip_comments-2026.6.20.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
909ee88d633018d4486e73805ba79ff1a143fadaf424a567218ec67c99602995
|
|
| MD5 |
acef7ead46c1de12d87567ed4606832e
|
|
| BLAKE2b-256 |
dd15cec4f0fdbe597ce9c9868834d492091aae3ad0f8516895619cfc13f822a3
|
Provenance
The following attestation bundles were made for strip_comments-2026.6.20.tar.gz:
Publisher:
pypi.yml on Alir3z4/strip-comments
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strip_comments-2026.6.20.tar.gz -
Subject digest:
909ee88d633018d4486e73805ba79ff1a143fadaf424a567218ec67c99602995 - Sigstore transparency entry: 1886825338
- Sigstore integration time:
-
Permalink:
Alir3z4/strip-comments@58a9e8cdf7bd1a3affe4a2ffa3a1471f8cc0f165 -
Branch / Tag:
refs/tags/2026.6.20 - Owner: https://github.com/Alir3z4
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@58a9e8cdf7bd1a3affe4a2ffa3a1471f8cc0f165 -
Trigger Event:
release
-
Statement type:
File details
Details for the file strip_comments-2026.6.20-py3-none-any.whl.
File metadata
- Download URL: strip_comments-2026.6.20-py3-none-any.whl
- Upload date:
- Size: 10.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4273216397875fd6faf2447603afa8585cf0a616a5e0935be7bf7d691402c758
|
|
| MD5 |
0c5162254cec39f053e000f6f400a24e
|
|
| BLAKE2b-256 |
b6488f67ade42598591eb0c79f24e62922b36c3251e95375dbf10f7fc28100e4
|
Provenance
The following attestation bundles were made for strip_comments-2026.6.20-py3-none-any.whl:
Publisher:
pypi.yml on Alir3z4/strip-comments
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strip_comments-2026.6.20-py3-none-any.whl -
Subject digest:
4273216397875fd6faf2447603afa8585cf0a616a5e0935be7bf7d691402c758 - Sigstore transparency entry: 1886825416
- Sigstore integration time:
-
Permalink:
Alir3z4/strip-comments@58a9e8cdf7bd1a3affe4a2ffa3a1471f8cc0f165 -
Branch / Tag:
refs/tags/2026.6.20 - Owner: https://github.com/Alir3z4
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@58a9e8cdf7bd1a3affe4a2ffa3a1471f8cc0f165 -
Trigger Event:
release
-
Statement type: