Retext
GitHub CopilotCopilotCode Explanation

GitHub Copilot Code Explanation Prompt Template

You understand what the code does, why it is written that way, and how its tricky parts work, at the level of detail you asked for. This version is set up for GitHub Copilot: select the relevant code first, or reference files in the chat, so Copilot answers about your code rather than in general. Below you'll find the prompt, how to run it in GitHub Copilot, a worked example and answers to common questions.

Installs as a snippet — type ;explaincode to paste this prompt anywhere on your Mac.
Prompt Template
Explain the code below so I understand it well enough to change it.

## Code
Language: [e.g. JavaScript]
[paste the code]

## About me
My experience with this language: [e.g. comfortable with Python, new to JavaScript]
Why I am reading this: [e.g. I need to add a field, fixing a bug, learning]

## Instructions
- Start with a two-sentence summary of what the code does overall.
- Then walk through it in order, explaining each important line or block.
- Explain any syntax or language feature I might not know, with a tiny example.
- Point out anything surprising, risky or easy to break.
- Do not rewrite the code unless I ask.

## Output format
1. Summary
2. Walkthrough
3. Unfamiliar syntax explained
4. Things to be careful with when changing it

Context
- Files involved: [list the files, or reference them in the chat]
- How I build and test: [e.g. npm test]

How to use this prompt

  1. 1

    Open GitHub Copilot

    Open Copilot Chat in your editor and paste the prompt.

  2. 2

    Give it context

    Select the relevant code first, or reference files in the chat, so Copilot answers about your code rather than in general.

  3. 3

    Review and iterate

    Ask follow-ups in the same thread, and copy or insert the suggestion only after you have read it.

  4. 4

    Save what you repeat

    Keep repository-wide guidance in a .github/copilot-instructions.md file so it applies to every chat in that repo.

Working in GitHub Copilot

  • •Inline suggestions only see the file you are in. Use Copilot Chat for anything that spans several files.
  • •Copilot Chat has built-in slash commands such as /explain, /fix and /tests. The prompt here gives more control when you need a specific format.
  • •Short, concrete requests work best: name the function, the input and the expected result.

Worked example

What you give it

Language: JavaScript
Experience: comfortable with Python, new to modern JavaScript.
Why: I need to change this so players are grouped by team and then by position, and I do not want to break the current output.

const byTeam = players.reduce((acc, p) => {
  (acc[p.team] ||= []).push(p.name);
  return acc;
}, {});

What a good answer looks like (shortened)

1. Summary: this builds an object where each key is a team and each value is a list of player names on that team. It is like a Python defaultdict(list).

2. Walkthrough: reduce starts with an empty object {} as acc. For each player p, it finds or creates the list for p.team, adds p.name, and returns acc for the next step.

3. Syntax: x ||= y assigns y to x only if x is falsy. So acc[p.team] ||= [] creates an empty list the first time a team appears. The whole expression evaluates to that list, which is why .push works right after it.

4. Careful: a team name that is an empty string still works, but if you group by a field that can be undefined, all those players land under the key "undefined".

Tips for better results

  • •Use "Why I am reading this" to aim the explanation. Saying you need to add a field makes the AI focus on where that change would go.
  • •Compare to a language you know in About me. Mapping reduce to a Python loop or defaultdict lands faster than an abstract definition.
  • •Short, concrete requests work best: name the function, the input and the expected result.
  • •Avoid this: Not saying your experience level, so the explanation is either too basic or skips the exact part you did not understand.

Get the free prompt pack

Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.

Variations

Line by line

Change Output format to: Add a short comment above each line of the original code explaining it, and return the commented code.

Diagram

Add this line to Instructions: Show the flow of data as a short text diagram, with function calls as arrows.

Unfamiliar codebase

Add this section: ## Surrounding code [paste the callers and types it uses] and explain how this piece fits in.

Save it as a Retext snippet

Retext is a free text expander for Mac. Save this prompt once, then type ;explaincode in any app (ChatGPT, Claude, your editor, a browser tab) and the full prompt is pasted in.

  1. Click Add to Retext. The prompt installs as a snippet with the trigger ;explaincode.
  2. Change the trigger in Retext if you prefer a different one.
  3. Type the trigger wherever you want the prompt, then fill in the brackets.

Frequently asked questions

Can AI explain code in a language I do not know?

Yes, and it helps to name a language you do know so it can compare patterns. Still verify any claim about behavior by running the code with a small input.

Does this prompt work with inline suggestions or only chat?

Use it in Copilot Chat. Inline suggestions complete code as you type and don't take long instructions like this.

What if the explanation sounds right but I am not sure it is?

Ask for a tiny input and the exact output the code would produce, then run it. If the prediction is wrong, the explanation is too.

How do I reuse this prompt without copying it every time?

Click Add to Retext to save it as a snippet. Then type ;explaincode in any Mac app, including your AI tool, and the full prompt is pasted in.

Related Templates

Get the free prompt pack

Our best coding prompts in one Markdown file, delivered by email. The templates here stay free either way — the pack just saves you the copying.