Extends ../CLAUDE.md
Export your codebase for AI/LLM context in one command.
pip install treemapper
treemapper . -o context.yaml # paste into ChatGPT/ClaudeUnlike tree or find, TreeMapper exports structure + file contents in a format optimized for LLM context windows:
name: myproject
type: directory
children:
- name: main.py
type: file
content: |
def hello():
print("Hello, World!")
- name: utils/
type: directory
children:
- name: helpers.py
type: file
content: |
def add(a, b):
return a + btreemapper . # YAML to stdout
treemapper . -o tree.yaml # save to file
treemapper . -o - # explicit stdout output
treemapper . --format json # JSON format
treemapper . --format text # plain text with indentation
treemapper . --format md # Markdown with headings and fenced code blocks
treemapper . --no-content # structure only (no file contents)
treemapper . --max-depth 3 # limit directory depth
treemapper . --max-file-bytes 10000 # skip files larger than 10KB
treemapper . -i custom.ignore # custom ignore patterns
treemapper . --no-default-ignores # disable .gitignore/.treemapperignore (custom -i still works)
treemapper . -v 2 # verbose output (0=ERROR, 1=WARNING, 2=INFO, 3=DEBUG)
treemapper . -c # copy to clipboard (no stdout)
treemapper . -c -o tree.yaml # copy to clipboard + save to file
treemapper . --tokens # show token count (for LLM context planning)
treemapper . --tokens --token-encoding cl100k_base # use GPT-4 tokenizer
treemapper --version # show versionShow token count for LLM context planning with --tokens:
treemapper . --tokens # 12,847 tokens (o200k_base)
treemapper . --tokens --copy # tokens + clipboardEncodings:
o200k_base(default) — GPT-4o tokenizero200k_harmony— GPT-4.1/newer models tokenizercl100k_base— GPT-4/GPT-3.5 tokenizer
Token count is displayed on stderr only when connected to a TTY (won't break pipes).
Copy output directly to clipboard with -c or --copy:
treemapper . -c # copy to clipboard (no stdout)
treemapper . -c -o tree.yaml # copy to clipboard + save to fileSystem Requirements:
- macOS:
pbcopy(pre-installed) - Windows:
clip(pre-installed) - Linux/FreeBSD (Wayland):
wl-copy(install:sudo apt install wl-clipboard) - Linux/FreeBSD (X11):
xcliporxsel(install:sudo apt install xclip)
from treemapper import map_directory, to_yaml, to_json, to_text, to_markdown
# Full function signature
tree = map_directory(
path, # directory path (str or Path)
max_depth=None, # limit traversal depth
no_content=False, # exclude file contents
max_file_bytes=None, # skip files larger than N bytes
ignore_file=None, # custom ignore file path
no_default_ignores=False, # disable .gitignore/.treemapperignore
)
# Examples
tree = map_directory("./myproject")
tree = map_directory("./src", max_depth=2, no_content=True)
tree = map_directory(".", max_file_bytes=50000, ignore_file="custom.ignore")
# Serialize to string
yaml_str = to_yaml(tree)
json_str = to_json(tree)
text_str = to_text(tree)
md_str = to_markdown(tree) # or to_md(tree)Respects .gitignore and .treemapperignore automatically. Use --no-default-ignores to include everything.
Features:
- Hierarchical: nested
.gitignore/.treemapperignorefiles work at each directory level - Negation patterns:
!important.logun-ignores a file - Anchored patterns:
/root_only.txtmatches only in root,*.logmatches everywhere - Output file is always auto-ignored (prevents recursive inclusion)
When file content cannot be read normally, placeholders are used:
<file too large: N bytes>— file exceeds--max-file-byteslimit<binary file: N bytes>— file detected as binary (contains null bytes)<unreadable content: not utf-8>— file is not valid UTF-8<unreadable content>— file cannot be read (permission denied, I/O error)
pip install -e ".[dev]"
pytest
pre-commit run --all-filesIntegration tests only - test against real filesystem. No mocking.
src/treemapper/
├── cli.py # argument parsing
├── ignore.py # gitignore/treemapperignore handling
├── tokens.py # token counting (optional tiktoken)
├── tree.py # directory traversal
├── writer.py # YAML/JSON/text/Markdown output
└── treemapper.py # main entry point
Apache 2.0