Sub topic 2
Work with Inline Suggestions
Assume you can open a VS Code folder, edit code, and use the Command Palette. You also need a signed-in GitHub Copilot account and a programming language supported by your project.
Accept and compare suggestions
Copilot can show a gray inline suggestion as you type. The suggestion uses nearby code, comments, and open files as context.
- Press
Tabto accept the suggestion. - Press
Escto dismiss it. - Press
Alt+]to show the next suggestion. - Press
Alt+[to show the previous suggestion.
Keyboard shortcuts can differ by operating system or your customized VS Code settings.
For example, type a descriptive comment and a function signature:
# Return the n-th Fibonacci number using iteration.
def fibonacci(n: int) -> int:
if n < 0:
raise ValueError("n must be non-negative")
previous, current = 0, 1
for _ in range(n):
previous, current = current, previous + current
return previous
print(fibonacci(10))Copilot may suggest the loop or the complete function after you type only the comment and signature. Review the result before accepting it. Check boundary cases, types, and whether the code matches your design.
Ask questions in the Chat view
Open the Chat view from the activity bar or with the configured Chat shortcut. Ask focused questions such as:
- “Explain the error in
fibonacci.” - “Suggest tests for the boundary cases in this file.”
- “Compare this implementation with an iterative approach.”
Include relevant context with references such as #file:utils.ts, or select code before asking. A precise question and narrow context usually produce a more useful answer than a broad request about the entire project.
Edit code with inline chat
Select the code you want to change, then open inline chat with the editor shortcut. Describe the desired change, such as “Add input validation and preserve the current return type.” Copilot proposes an edit in place. Review the diff, then accept or reject it.
Inline chat is best for a localized change. Use the Chat view when you need an explanation, several files of context, or a discussion before editing.
Exercise
Which interaction should you use to ask why a function fails without immediately changing it?
Show answer
Use the Chat view in a question-oriented mode, and provide the relevant file or selection as context.