Shell Command to Combine Multiple Files for AI Coding

We will create a shell command to quickly combine the contents of multiple files, including directory structure information, for LLM prompts.

Introduction

When coding with a simple chat-based LLM like ChatGPT without using AI coding tools, it is often desirable to quickly paste the contents of multiple files into a prompt. However, manually copying and pasting multiple files one by one can be tedious.

Although there are many specialized tools available for such purposes, if you just want to accomplish the bare minimum, a few lines of shell script will suffice.

Here is my approach.

Note: This article was translated from my original post.

Shell Command to Concatenate Multiple Files for AI Coding

Goal

  • Concatenate the following information and copy it to the clipboard as text:
    • Project directory structure
    • Path and contents of the specified files
    • Multiple file specifications are allowed
  • Environment: macOS

By executing this command, it becomes convenient to quickly paste the necessary information when pair programming with AI.

Command Implementation

A simple shell command can easily achieve the above objective.

catr() {
  (
    tree
    for f in "$@"; do
      printf "\n── %s ──\n" "$f"
      cat "$f"
    done
  ) | pbcopy
}

If you add this to your ~/.bashrc or ~/.zshrc, you can execute it as the catr command (you can change the name if you prefer).

What this command does:

  1. Uses the tree command to output the directory structure.
  2. Outputs the file path specified as an argument.
  3. Uses the cat command to output the contents of the file specified as an argument.
  4. Repeats steps 2-3 for each specified file path.
  5. Pipes the output to pbcopy to copy it to the clipboard.

If you want to add more features, it’s easy to customize since the base code is only a few lines.

Now, let's look at a concrete example of executing this command.

Examples

Using the initial state of a Python project created with uv as an example, we will execute the above catr command.

Example of specifying multiple files for execution:

catr main.py pyproject.toml 

# Text copied to the clipboard:
.
├── README.md
├── main.py
└── pyproject.toml

1 directory, 3 files

── main.py ──
def main():
    print("Hello from testproject!")


if __name__ == "__main__":
    main()

── pyproject.toml ──
[project]
name = "testproject"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = []


Specifying all files managed by git in the repository:

catr $(git ls-files)

# Text copied to the clipboard:
.
├── README.md
├── main.py
└── pyproject.toml

1 directory, 3 files

── .gitignore ──
# Python-generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info

# Virtual environments
.venv

── .python-version ──
3.11

── README.md ──

── main.py ──
def main():
    print("Hello from testproject!")


if __name__ == "__main__":
    main()

── pyproject.toml ──
[project]
name = "testproject"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = []


You can also use commands like find to specify file paths with any logic. Additionally, you can customize the output of the directory structure by setting options in the tree command within the script.

Instead of just copying to the clipboard, you can also pipe the content to stdout or save it to a specific file. It might be useful to allow behavior adjustment via command-line arguments.

Conclusion

We have summarized a simple shell command to concatenate multiple file information for AI coding.

It’s nice to be able to achieve this without relying on external tools.

I hope this helps someone!

[Related Articles]

en.bioerrorlog.work

References