This shell script explores a specified project directory and gathers all the code from relevant files into a single text file called code_context.txt
or copies it directly to the clipboard. The script supports multiple programming languages by allowing the user to specify the language, and it automatically determines the appropriate file extensions to include.
To use this script, follow the instructions below.
- Ensure you have a Unix-like operating system with
bash
installed. - Make sure you have the necessary permissions to execute the script and read files in the specified directory.
- Save the script to a file, e.g.,
CodeContext.sh
. - Make the script executable:
chmod +x CodeContext.sh
The script takes three arguments:
- The project directory to explore.
- The programming language of the project.
- (Optional) The output mode:
text
orclipboard
.
text
: Outputs the code context tocode_context.txt
.clipboard
(default): Copies the code context directly to the clipboard.
If the project directory is not specified, the script will use the current directory. The supported languages and their corresponding file extensions are:
- python:
*.py,*.txt,*.md
- flutter:
*.dart,*.yaml,*.txt,*.md
- javascript:
*.js,*.jsx,*.json,*.html,*.css,*.txt,*.md
- java:
*.java,*.xml,*.txt,*.md
- html:
*.html,*.css,*.js,*.txt,*.md,*.json
- css:
*.css,*.html,*.js,*.txt,*.md
- c:
*.c,*.h,*.txt,*.md
- cpp:
*.cpp,*.h,*.txt,*.md
- Gather code from the current directory for a Python project and copy to clipboard:
./CodeContext.sh . python
- Gather code from a specific directory for a Flutter project and output to a text file:
./CodeContext.sh /path/to/your/flutter/project flutter text
- Gather code and explicitly copy to clipboard:
./CodeContext.sh /path/to/project javascript clipboard
The output will be either:
- A file named
code_context.txt
in the same directory where the script is executed (iftext
mode is used). - Directly copied to the clipboard (if
clipboard
mode is used).
Assume the following directory structure for a Python project:
project/
├── main.py
├── module/
│ ├── __init__.py
│ └── utils.py
├── README.md
└── requirements.txt
main.py
:print("Hello, World!")
module/utils.py
:def add(a, b): return a + b
README.md
:# Project Title This is a sample project.
requirements.txt
requests numpy
If outputted to a file, the code_context.txt
will look like this:
### ./main.py ###
print("Hello, World!")
### ./module/__init__.py ###
### ./module/utils.py ###
def add(a, b):
return a + b
### ./README.md ###
# Project Title
This is a sample project.
### ./requirements.txt ###
requests
numpy
If using the clipboard
mode, the same content will be available in your clipboard, ready to paste anywhere.