Sub topic 5
Work with inline suggestions
This chapter assumes you have installed the GitHub Copilot extension and signed in, and that you can open folders and edit code in VS Code. Inline suggestions are the feature you will use most, so it helps to understand how they appear and how to control them.
How inline suggestions work
As you type, Copilot sends the code around your cursor to the Copilot service. It then shows a suggestion as faded "ghost text" after your cursor. The suggestion can be a single line, a whole function, or a block of tests. Nothing is inserted into your file until you accept it.
Copilot reads more than the current line. It uses the text before and after the cursor, comments, and other open files in your editor. A clear comment or function name therefore improves the result more than any setting does.
Trigger a suggestion with a comment
A descriptive comment followed by a function signature is a reliable way to get a useful suggestion. Type this into a Python file:
# Return the n-th Fibonacci number using iteration
def fibonacci(n):After the signature, pause. Copilot should show a function body in ghost text. A typical result looks like this:
# Return the n-th Fibonacci number using iteration
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return aYour suggestion may differ. Copilot can return different code each time, so always read it before you accept it.
Keyboard shortcuts
These are the default shortcuts on Windows and Linux. On macOS, use Option in place of Alt.
| Action | Shortcut |
|---|---|
| Accept the whole suggestion | Tab |
| Accept the next word | Ctrl+Right Arrow |
| Dismiss the suggestion | Esc |
| Show the next suggestion | Alt+] |
| Show the previous suggestion | Alt+[ |
| Open a panel of several suggestions | Ctrl+Enter |
Accepting one word at a time is useful when a suggestion is mostly right but ends in the wrong place. You keep the good start and type the rest yourself.
Turn suggestions on or off
The Copilot icon in the status bar, at the bottom of the window, lets you enable or disable completions. You can turn them off for the language of the current file or for everything. If the icon shows a warning, select it to see the reason.
To keep suggestions on for most languages but off for plain text and Markdown, add this to settings.json:
{
"github.copilot.enable": {
"*": true,
"plaintext": false,
"markdown": false
}
}The "*" entry is the default for every language. A specific entry such as "markdown" overrides it. Language settings get more attention in a later chapter.
Common problems
- No ghost text appears. Check the status bar icon first. Copilot may be disabled for this language, or you may be signed out.
- Suggestions are slow or absent after a pause. Look for a network or proxy problem. The troubleshooting chapter covers this.
- Suggestions ignore your project style. Add clearer comments, or use custom instructions for chat. Inline suggestions respond mainly to the code and comments you have already written.
- You accepted something wrong. Press Ctrl+Z to undo. The accepted text is ordinary text in your file.
Exercises
Exercise
You typed a comment and a function signature, and Copilot shows a suggestion that is correct for the first two lines but wrong afterward. How can you keep only the correct part without accepting everything?
Show answer
Press Ctrl+Right Arrow (Option+Right Arrow on macOS is not the default, so check your keybindings) to accept the suggestion one word at a time. Stop when you reach the correct part, then type the rest yourself. You can also accept the whole suggestion with Tab and delete the wrong lines.
Exercise
Write the settings.json entry that keeps inline suggestions on everywhere except in Markdown files.
Show answer
Use "github.copilot.enable": { "": true, "markdown": false }. The "" key sets the default, and the "markdown" key overrides it for that language.