DEV Community

Cover image for Reusable GitHub Copilot Prompt for Refactoring Opportunities
PeterMilovcik
PeterMilovcik

Posted on

Reusable GitHub Copilot Prompt for Refactoring Opportunities

Refactoring becomes easier when you receive fast feedback about improvement options in code.

GitHub Copilot Chat already helps with this, but writing a long prompt each time feels wasteful.

A small prompt instruction file in your repository removes friction.

This article walks through one concrete example.

What is a prompt instruction file

GitHub Copilot Chat supports reusable prompts stored under a .github/prompts folder in your repository.

Each file holds instructions for the agent.

You reference such a file from chat with a slash command instead of copying full text again.

This section focuses on one refactoring helper prompt.

Step 1: Create the prompt file

Create folder .github/prompts in your repository if it does not exist yet.

Add a new markdown file named refactoring_opportunities.prompt.md inside this folder.

Use the following content.

---
agent: agent
---

Check for potential refactoring opportunities in the following code. If you find any, suggest specific improvements along with code examples. Focus on enhancing readability, maintainability, and performance. If the code is already optimal, simply state that no changes are necessary.
Enter fullscreen mode Exit fullscreen mode

A short explanation:

  • The front matter at the top selects which agent runs this prompt.
  • The text below tells Copilot Chat what to look for and how to respond.

Step 2: Use the prompt from chat

Open GitHub Copilot Chat in your editor.

Use this command:

/refactoring_opportunities #file:<yourfile>
Enter fullscreen mode Exit fullscreen mode

Replace <yourfile> with the file name you want to inspect, for example:

/refactoring_opportunities #file:MyClass.cs
Enter fullscreen mode Exit fullscreen mode

Copilot receives code from this file together with prompt text from refactoring_opportunities.prompt.md.

The agent then reviews the code and responds with suggested refactorings or a short confirmation when nothing needs to change.

What this prompt asks for

The instruction focuses on three aspects.

  • Readability
    Names, structure, and formatting should help new readers understand code quickly.

  • Maintainability
    Smaller functions, clear responsibilities, and fewer hidden dependencies reduce effort for future changes.

  • Performance
    The agent points out unnecessary allocations, redundant work, and obvious algorithm problems.

The last sentence in the prompt keeps responses grounded.

When no clear improvement appears, Copilot replies with a short message instead of forcing weak suggestions.

Noise in your review process stays low.

Why store the prompt in your repository

Keeping prompt files inside version control brings a few benefits.

  • Shared language for the team
    Everyone calls the same prompt name and receives similar guidance.

  • History
    You refine wording through pull requests and review changes together.

  • Portability
    Prompt files travel with the codebase, so new team members gain useful helpers from day one.

A prompt becomes part of your engineering practice, not a snippet lost in chat history.

Ideas for extensions

Once refactoring_opportunities.prompt.md works, expand with more prompt files in the same folder.

Some examples:

  • security_review.prompt.md for quick checks of obvious security problems.
  • test_gaps.prompt.md for simple suggestions where tests might be missing.
  • api_consistency.prompt.md for checks of naming and behavior across endpoints and clients.

Each file follows the same pattern.

Front matter selects an agent, body text explains the task.

Summary

A small prompt instruction file turns GitHub Copilot Chat into a repeatable refactoring assistant.

You spend less time writing prompts and more time judging suggestions.

Start with refactoring_opportunities.prompt.md under .github/prompts, then call:

/refactoring_opportunities #file:YourFile.cs
Enter fullscreen mode Exit fullscreen mode

whenever you want a fresh look at a piece of code.

Top comments (0)