|
| 1 | +--- |
| 2 | +title: Configuring Code2Prompt 📖 |
| 3 | +description: Learn how to use .c2pconfig to automate your prompt generation workflow and ensure team consistency. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Card, Steps, Aside, Tabs, TabItem } from "@astrojs/starlight/components"; |
| 7 | + |
| 8 | +<Card title="Tutorial Overview"> |
| 9 | + Manually typing long exclude patterns or specific tokenizer settings every time can be tedious. Therefore this tutorial shows you **how to use a `.c2pconfig` configuration file** to "set and forget" your project settings. |
| 10 | +</Card> |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | + |
| 16 | +Ensure you have `code2prompt` installed. If you haven't installed it yet, refer to the [Installation Guide](/docs/how_to/install). Familiarity with [TOML syntax](https://toml.io/en/) is helpful but not required. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## What is .c2pconfig? |
| 21 | + |
| 22 | +The `.c2pconfig` file is a configuration file written in **TOML** format. When you run `code2prompt`, it automatically searches for this file in your current working directory. |
| 23 | + |
| 24 | +It allows you to define: |
| 25 | +* **Filtering Rules:** Persistent include/exclude patterns. |
| 26 | +* **Output Formats:** Default to JSON, Markdown, or XML. |
| 27 | +* **Template Context:** Pre-define variables for your Handlebars templates. |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## Quick Start |
| 32 | + |
| 33 | +Create a file named `.c2pconfig` at the root of your project to define your base behavior. |
| 34 | + |
| 35 | +```toml |
| 36 | +# .c2pconfig example |
| 37 | +default_output = "stdout" # Options: stdout, clipboard, file |
| 38 | +include_patterns = ["src/**/*.rs", "Cargo.toml"] |
| 39 | +exclude_patterns = ["**/target/**", "tests/fixtures/**"] |
| 40 | +line_numbers = true |
| 41 | +output_format = "markdown" |
| 42 | + |
| 43 | +[user_variables] |
| 44 | +project_name = "MyAwesomeProject" |
| 45 | +author = "Developer" |
| 46 | + |
| 47 | +``` |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Configuration Reference |
| 52 | + |
| 53 | +The following table describes the keys available in the configuration file. |
| 54 | + |
| 55 | +| Key | Type | Description | |
| 56 | +| --- | --- | --- | |
| 57 | +| `path` | String | Default path to codebase (usually `.`). | |
| 58 | +| `include_patterns` | Array | Glob patterns of files to include. | |
| 59 | +| `exclude_patterns` | Array | Glob patterns of files to exclude. | |
| 60 | +| `line_numbers` | Boolean | If `true`, adds line numbers to code blocks. | |
| 61 | +| `absolute_path` | Boolean | Use absolute paths instead of relative paths. | |
| 62 | +| `full_directory_tree` | Boolean | Generate the full tree even for excluded files. | |
| 63 | +| `output_format` | String | `markdown`, `json`, or `xml`. | |
| 64 | +| `sort_method` | String | `name_asc`, `name_desc`, `date_asc`, `date_desc`. | |
| 65 | +| `encoding` | String | Tokenizer: `cl100k`, `p50k`, `o200k`. | |
| 66 | +| `diff_enabled` | Boolean | Include git diff (HEAD vs Index). | |
| 67 | +| `token_map_enabled` | Boolean | Display a hierarchical token usage map. | |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Implementation Guide |
| 72 | + |
| 73 | +Follow these steps to integrate a configuration file into your workflow. |
| 74 | + |
| 75 | +<Steps> |
| 76 | + |
| 77 | +1. **Initialize your Configuration** |
| 78 | + Navigate to your project root and create the config file: |
| 79 | + ```bash |
| 80 | + touch .c2pconfig |
| 81 | + |
| 82 | + ``` |
| 83 | +
|
| 84 | +2. **Define your Source of Truth** |
| 85 | + Exclude heavy directories like `node_modules` or build artifacts to keep the LLM context clean. |
| 86 | + ```toml |
| 87 | + exclude_patterns = [ |
| 88 | + "**/node_modules/**", |
| 89 | + "package-lock.json", |
| 90 | + "dist/**" |
| 91 | + ] |
| 92 | +
|
| 93 | + ``` |
| 94 | + |
| 95 | +3. **Set your Model Encoding** |
| 96 | + Match the tokenizer to your target LLM. Use `o200k` for GPT-4o, or `cl100k` for Claude and GPT-4. |
| 97 | + ```toml |
| 98 | + encoding = "o200k" |
| 99 | + |
| 100 | + ``` |
| 101 | +
|
| 102 | +4. **Inject Custom Context** |
| 103 | + Use the `[user_variables]` section to pass data into your [Handlebars templates](/docs/learn/templates). |
| 104 | + ```toml |
| 105 | + [user_variables] |
| 106 | + project_goal = "Refactor the authentication module for better security." |
| 107 | +
|
| 108 | + ``` |
| 109 | + |
| 110 | +5. **Run with Zero Arguments** |
| 111 | + Simply run the tool. It will now respect all your predefined rules without extra CLI flags. |
| 112 | + ```bash |
| 113 | + code2prompt . |
| 114 | + |
| 115 | + ``` |
| 116 | +
|
| 117 | +</Steps> |
| 118 | +
|
| 119 | +--- |
| 120 | +
|
| 121 | +## Understanding Precedence |
| 122 | +
|
| 123 | +It is important to understand how `code2prompt` decides which settings to use when multiple sources conflict. |
| 124 | +
|
| 125 | +<Aside type="tip" title="Priority Order"> |
| 126 | +**CLI Arguments > Configuration File > Default Settings** |
| 127 | +
|
| 128 | +Arguments passed directly via the CLI will always override values defined in `.c2pconfig`. This allows you to maintain a "base" config while remaining flexible for one-off commands. |
| 129 | +
|
| 130 | +</Aside> |
| 131 | +
|
| 132 | +### Advanced Filtering Logic |
| 133 | +
|
| 134 | +The engine uses a tiered selection system: |
| 135 | +
|
| 136 | +* **Static (A/B):** Defined in your `.c2pconfig` (Include/Exclude). |
| 137 | +* **Dynamic (A'/B'):** If you use **Interactive Mode**, your manual toggle selections override the static patterns for that specific session. |
| 138 | +
|
| 139 | +--- |
| 140 | +
|
| 141 | +## Example: The "Review-Ready" Config |
| 142 | +
|
| 143 | +Use this setup if your primary goal is generating prompts for code reviews. |
| 144 | +
|
| 145 | +```toml |
| 146 | +default_output = "clipboard" |
| 147 | +line_numbers = true |
| 148 | +token_map_enabled = true |
| 149 | +
|
| 150 | +exclude_patterns = [ |
| 151 | + "tests/**", |
| 152 | + "**/migrations/**", |
| 153 | + "*.md" |
| 154 | +] |
| 155 | +
|
| 156 | +[user_variables] |
| 157 | +review_focus = "Check for DRY principle violations and complexity." |
| 158 | +
|
| 159 | +``` |
| 160 | + |
| 161 | +--- |
| 162 | + |
| 163 | +## Next Steps |
| 164 | + |
| 165 | +<Card title="Level Up your Workflow"> |
| 166 | + |
| 167 | +* **Master Templates:** Explore [Custom Templates](/docs/tutorials/learn_templates) to see how to use `user_variables` effectively. |
| 168 | +* **Refine Filtering:** Check the [Filtering Guide](/docs/tutorials/learn_filters) for advanced glob pattern syntax. |
| 169 | +</Card> |
0 commit comments