dircat for llm context
I have a little command line utility that I use to grab the contents of a directory to paste into an LLM.
Examples
Recursive directory catting
The relative path to the file is prepended to the file as a comment in whatever language is appropriate. This gives the LLM useful file structure context!
❯ dircat src/services // src/services/chatServices/anthropic.ts import Anthropic from "@anthropic-ai/sdk"; import type { ChatMessage, ChatService } from "./types"; import type { AuthUser } from "@/types"; export class AnthropicService implements ChatService { private client: Anthropic; ...<Rest of this file> // src/services/chatServices/openai.ts import OpenAI from "openai"; import type { ChatMessage, ChatService } from "./types"; import type { AuthUser } from "@/types"; export class OpenAIService implements ChatService { private client: OpenAI; ...<Rest of this file> ...<Rest of the files under src/services>
Directory tree visualization builder
You can also build a visualization of the directory tree to show to the LLM.
❯ dircat --tree src src/ ├── config/ │ ├── env.ts │ └── index.ts ├── middleware/ │ ├── auth.ts │ └── rate-limit.ts ├── routes/ │ └── chat.ts ├── services/ │ ├── chatServices/ │ │ ├── anthropic.ts │ │ ├── factory.ts │ │ ├── mistral.ts │ │ ├── openai.ts │ │ ├── openrouter.ts │ │ ├── types.ts │ │ └── xai.ts │ ├── db/ │ │ └── mongo.ts │ └── usage/ │ └── reporter.ts ├── index.ts └── types.ts
Usage
Ignore files and directories with .dircatignore
It follows gitignore rules to skip traversing/outputting directories/files, but you have to put the rules in a .dircatignore
file. (Sometimes you want to dircat things that you've gitignored, other times you want to git things that you've dircatignored)
Grabbing the contents of a directory
dircat src
Grabbing the contents of multiple directories
dircat src/app/api/chat src/app/chat
Render a tree of a directory
dircat --tree src/app
Render a tree of multiple directories
The tree for each is rendered separately then they're concatenated separated by a new line.
dircat ---tree src/app/api/chat src/app/chat
Copying to your clipboard
This one is platform dependent. On mac I use pbcopy
.
dircat src | pbcopy
How to install it
I put the python script (with shebang line) for it in the folder ~/bin
, and then add the following to .zshrc
.
export PATH="$HOME/bin:$PATH"
You can install the version you build however you like!
Where's the code?
I initially had included the code for it here, but to be honest it's vibecoded slop from months ago. Paste this blog post into any new generation LLM and I'm sure you'll get a better version of the code that I have.
If you really want the code email or dm me or something.