GitHub Copilot can generate substantial code blocks, like functions or classes, based on context or explicit instructions.
Copilot automatically suggests multi-line blocks when it detects context like a function signature, class definition, or a descriptive comment.
class User:
or function calculateTotal(items) {
).// function to fetch user data from API
).Example (Comment Trigger):
# User types:
# Function to calculate the factorial of n using recursion
def factorial(n):
# Copilot Suggests (ghost text):
def factorial(n):
"""Calculates the factorial of n recursively."""
if n == 0:
return 1
else:
return n * factorial(n-1)
Use Copilot Chat for more explicit control over generation, especially for complex blocks or when you want to refine the request.
#selection
or #file
, or @workspace
/ @project
participants to provide context if needed./new
or /newNotebook
for generating new files/notebooks.Example Prompt (Chat):
/new Create a simple Python Flask route for /hello that returns "Hello, World!"
Copilot Chat will generate the necessary Flask code block, potentially in a new file context.
Clearly define the language, purpose, inputs, outputs, and any libraries/frameworks to use. Use Chat context variables (#file
, #selection
) or provide examples in your prompt.
Instead of asking for an entire application, prompt for smaller components (e.g., "Generate the User model class", then "Generate the API endpoint to get users").
Use follow-up prompts in Chat to modify the generated code (e.g., "Add error handling to the previous code", "Refactor this using list comprehensions"). Review and test all generated code.