---
title: Code2Prompt basic usage
description: This guides covers the basic usage of Code2Prompt CLI
---

import { Card } from "@astrojs/starlight/components";
import { Aside } from "@astrojs/starlight/components";


<Card title="Guide Overview">
  Welcome to the `Code2Prompt` usage guide.
</Card>

## Usage

Generate a prompt from a codebase:

```sh
code2prompt path/to/codebase
```

Generate a prompt and copy it to the clipboard:

```sh
code2prompt path/to/codebase -c
```

Launch the interactive TUI:
```sh
code2prompt path/to/codebase --tui
```

Display the token-map 

```sh
code2prompt path/to/codebase --token-map
```

Use a custom Handlebars template file:

```sh
code2prompt path/to/codebase -t path/to/template.hbs
```

Filter files using glob patterns:

```sh
code2prompt path/to/codebase --include="*.rs,*.toml"
```

Exclude files using glob patterns:

```sh
code2prompt path/to/codebase --exclude="*.txt,*.md"
```

Exclude files/folders from the source tree based on exclude patterns:

```sh
code2prompt path/to/codebase --exclude="*.npy,*.wav" --exclude-from-tree
```

Display the token count of the generated prompt:

```sh
code2prompt path/to/codebase --tokens
```

Specify a tokenizer for token count:

```sh
code2prompt path/to/codebase --tokens --encoding=p50k
```

Supported tokenizers: `cl100k`, `p50k`, `p50k_edit`, `r50k_bas`.
<Aside type="note">
See [Tokenizers](#tokenizers) for more details.
</Aside>

Save the generated prompt to an output file:

```sh
code2prompt path/to/codebase -O output.txt
```

Print output as XML:

```sh
code2prompt path/to/codebase -F JSON
```

The JSON output will have the following structure:

```json
{
  "prompt": "<Generated Prompt>", 
  "directory_name": "codebase",
  "token_count": 1234,
  "model_info": "ChatGPT models, text-embedding-ada-002",
  "files": []
}
```

Generate a Git commit message (for staged files):

```sh
code2prompt path/to/codebase --diff -t templates/write-git-commit.hbs
```

Generate a Pull Request with branch comparing (for staged files):

```sh
code2prompt path/to/codebase --git-diff-branch 'main, development' --git-log-branch 'main, development' -t templates/write-github-pull-request.hbs
```

Add line numbers to source code blocks:

```sh
code2prompt path/to/codebase --line-number
```

Disable wrapping code inside markdown code blocks:

```sh
code2prompt path/to/codebase --no-codeblock
```


Code2prompt is a nice way to generate prompts in a token efficient way. It can be practical for a variety of use cases, such as:
- Rewrite the code to another language.
- Find bugs/security vulnerabilities.
- Document the code.
- Implement new features.
