Smart Inline Completions: Use Cases, Examples, and Exercises
From Comments to Code
Copilot has access to your comments and reads them carefully to understand what you want to build. Anything you write in a comment can serve as a prompt, context, or hint for Copilot.
For example, if you write # Create a list of numbers from 1 to 10 and hit ENTER, Copilot will immediately understand and suggest the following code (or another variation of it depending on the model used, the context, the version of Copilot, etc.):
# Create a list of numbers from 1 to 10
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
You don't need to explicitly instruct Copilot to generate code from comments (e.g., by typing "generate code" or similar). The following comment is sufficient:
# A list of numbers from 1 to 10
Here is another example:
# Fibonacci function that returns the nth number in the sequence
This is the code that Copilot might generate.
# Fibonacci function that returns the nth number in the sequence
def fibonacci(n):
if n <= 0:
return "Invalid input"
elif n == 1:
return 0
elif n == 2:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)
If you don't want to generate the code, you can press the ESC key to dismiss the suggestion.
Sometimes, Copilot may not generate the code you expect. In such cases, you can provide more context by adding more comments, code, or examples.
In some cases, Copilot may also fail to import a module or a package. Let's say you started with this code:
# List all files in a directory
def list_files(directory):
The code generated by Copilot could be:
# List all files in a directory
def list_files(directory):
returnBuilding with GitHub Copilot
From Autocomplete to Autonomous AgentsEnroll now to unlock all content and receive all future updates for free.
