Sub topic 7
Edit code with inline chat
This chapter assumes you have installed the GitHub Copilot extension, signed in with GitHub, and used the Chat view at least once. You should also be comfortable selecting code and using the Command Palette in VS Code.
What inline chat is
Inline chat is a small prompt box that opens inside the editor, next to the code you are working on. You type a request in plain language, and Copilot proposes a change right in the file. You do not switch to the Chat view and you do not copy code back and forth.
Use inline chat for small, local edits. Examples are renaming variables in one function, adding error handling, writing a docstring, or fixing a single bug. For questions that span many files, use the Chat view or Agent mode instead. Those are covered in the next chapter.
Open inline chat
You can start inline chat in three ways:
- Press
Ctrl+Ion Windows and Linux, orCmd+Ion macOS. - Right-click in the editor and choose Copilot, then Editor Inline Chat.
- Open the Command Palette and run Inline Chat: Start Inline Chat.
If nothing happens, check that you are signed in. The troubleshooting chapter covers sign-in problems.
Edit a selection
Inline chat works best when you select the code you want changed. The selection tells Copilot exactly which lines are in scope.
Suppose you have this function in a file:
def average(numbers):
return sum(numbers) / len(numbers)Select both lines, press Ctrl+I or Cmd+I, and type:
Handle an empty list by returning 0.Copilot shows a proposed change as a diff. Removed lines are marked in one color and added lines in another. The result will look similar to this:
def average(numbers):
if not numbers:
return 0
return sum(numbers) / len(numbers)The exact output can differ from run to run, because the model does not always produce the same answer.
Review the result
Nothing is final until you decide. After Copilot responds, you can:
- Accept the change to keep it in the file.
- Close or discard it to return to your original code.
- Send a follow-up prompt in the same box, such as "Use a more descriptive name than numbers". Copilot revises the proposal.
Read the diff before accepting. Copilot can change more than you asked for, or introduce a subtle bug. Run your tests after you accept an edit, as you would for any code you did not write yourself.
If you accept something by mistake, use the normal undo shortcut (Ctrl+Z or Cmd+Z). The edit is applied like any other change in the editor.
Use commands and context
Inline chat accepts slash commands, which are short shortcuts that start with /. Common ones include:
| Command | What it does |
|---|---|
| `/explain` | Explains the selected code. |
| `/fix` | Proposes a fix for a problem in the selection. |
| `/tests` | Generates tests for the selected code. |
| `/doc` | Adds documentation comments. |
The available commands can change between extension versions. Type / in the box to see the list in your install.
Inline chat also sees the file you are editing. You can add more context with references such as #file, the same way you do in the Chat view. For example:
Rewrite this function to use the helper in #file:utils.ts.Common mistakes
- No selection. With nothing selected, Copilot may edit near the cursor or guess the scope. Select the code first.
- Prompts that are too broad. "Improve this file" gives unpredictable results. Ask for one specific change at a time.
- Skipping the diff. Accepting without reading is the most common source of bugs from AI edits.
- Wrong tool for the job. If a change touches several files, inline chat only edits the current one. Switch to Edit or Agent mode.
Exercise
You select a 10-line function and ask inline chat to add input validation. The diff also renames two variables you did not mention. What should you do?
Show answer
Do not accept the change as is. Send a follow-up prompt such as "Keep the original variable names and only add input validation", or discard the edit and rewrite the prompt. Then review the new diff before accepting.
Exercise
Name two situations where the Chat view or Agent mode is a better fit than inline chat.
Show answer
Any two of these: a question that is not tied to specific code, a change that spans multiple files, a task that needs running commands or tests, or a long conversation that you want to keep as a visible history.