Prompt Engineering Step 1: Providing Clear Context (Copilot)

GitHub Copilot needs context to provide relevant suggestions. The more effectively you provide it, the better the results. Here are key methods:

1. Leverage Your Open Editor

Copilot heavily relies on the code in your currently active file, especially the code surrounding your cursor.

  • Ensure relevant files are open in your editor tabs.
  • Place your cursor where you want the code generated or near the code you're asking about.
  • Select specific code blocks before using inline chat (Ctrl+I / Cmd+I) or referencing #selection in the chat view.

2. Use '#' Context Variables (Chat View)

In the Copilot Chat view (Ctrl+Alt+I / Cmd+Alt+I), explicitly reference context using #:

  • #selection: Refers to the code currently selected in the active editor.
  • #file: Refers to the entire content of the active file in the editor.
  • #file:path/to/some/file.ext: Refers to a specific file within your workspace (start typing after #file: for suggestions).

Example Prompt using #:

Explain the logic in #selection within the context of #file:utils/helpers.py

3. Use '@' Participants (Chat View)

Engage specialized "participants" using @ in the Chat view for broader context:

  • @workspace (VS Code) / @project (JetBrains): Allows Copilot to search across your entire workspace/project to find relevant context based on your prompt. This is powerful for questions about how different parts of the code interact.

Example Prompt using @:

@workspace How is user authentication implemented in this project?

4. Specify Language / Framework / Libraries

Don't assume Copilot knows the specific technology unless it's obvious from the current file. Explicitly mention it in your prompt.

Example Prompt:

Generate a React component using functional syntax and hooks to...
How do I configure CORS in this Express app? Referencing #file:server.js
Key Takeaway

Combine clear natural language with explicit context using code selection, # variables, and @ participants in Copilot Chat to guide the AI towards the most helpful and relevant responses.