If you use Copilot for coding or technical documentation, you may want its responses to always appear inside fenced code blocks. This prevents formatting issues, makes copying code easier, and keeps the output clean. By default, Copilot does not wrap every response in code fences. You can force this behavior using a system prompt or custom instructions. This article explains how to configure Copilot to output only fenced code blocks.
Key Takeaways: How to Force Copilot to Use Fenced Code Blocks
- Copilot system prompt or custom instructions: Add a rule to always wrap responses in triple backticks.
- Copilot chat pane > Settings > Custom instructions: Paste the constraint text to apply it to every conversation.
- Prompt injection at the start of each query: Manually include the instruction “Enclose your entire response in a fenced code block” for one-off cases.
What Are Fenced Code Blocks and Why Constrain Copilot to Them
Fenced code blocks are sections of text enclosed by triple backticks (“`) in Markdown. They tell rendering engines to display the content as code, with monospace font and syntax highlighting. When Copilot outputs text outside these fences, the text may inherit the formatting of the surrounding document. For example, bullet lists, bold text, or inline code may break the intended structure.
Constraining Copilot to fenced code blocks only is useful in several scenarios:
Consistent Formatting in Documentation
If you paste Copilot responses into a Markdown file, wiki, or README, you want every snippet to look the same. Without fences, Copilot may mix paragraph text with code, making the output harder to read.
Copying Code Without Extra Text
When Copilot explains a code snippet, it often includes introductory or concluding sentences. Those sentences are not code. By forcing the entire response into a fenced block, you ensure that only the code itself appears, which simplifies copying and pasting.
Integration with Code Editors
Some code editors and IDEs can detect fenced code blocks and apply syntax highlighting automatically. If Copilot’s output is not fenced, the editor treats it as plain text and no highlighting occurs. Constraining the output fixes this.
Methods to Force Copilot to Output Only Fenced Code Blocks
There are three reliable methods. Choose the one that fits your workflow best.
Method 1: Using Custom Instructions in Copilot for Microsoft 365
- Open Copilot in Microsoft 365
Launch any Microsoft 365 app such as Word, Excel, or Teams. Click the Copilot icon in the ribbon or the side pane. - Access custom instructions
Click the three-dot menu in the Copilot pane and select Settings. Then choose Custom instructions. - Add the constraint rule
In the text box, paste the following instruction: “Always wrap your entire response in a fenced code block using triple backticks. Do not include any text outside the code block.” - Save and test
Click Save. Send a test query such as “Write a Python function to reverse a string.” Verify that the response starts with “` and ends with “`.
Method 2: Including the Constraint in the Prompt
- Start a new conversation
Open Copilot in any supported app or at copilot.microsoft.com. - Write the constraint as the first line of your prompt
Type: “Enclose your entire response in a fenced code block using triple backticks.” Then press Enter or Shift+Enter to add your actual query on the next line. - Send the combined prompt
Press Enter to send. Copilot will apply the instruction to that single response. Repeat this for each query if you want consistent fencing.
Method 3: Using System Prompts in Copilot Studio or API
- Open Copilot Studio
Go to copilotstudio.microsoft.com and sign in with your work or school account. - Create or edit a copilot
Select an existing copilot or click Create to build a new one. - Add a system prompt
In the System prompt section, paste: “You must output all responses inside a single fenced code block. Use triple backticks to open and close the block. Do not include any text outside the block.” - Publish the copilot
Click Publish to apply the change. All future interactions will follow the rule.
Common Issues When Constraining Copilot to Fenced Code Blocks
Copilot Ignores the Custom Instruction
Copilot may occasionally ignore custom instructions if the instruction conflicts with other system-level rules. To fix this, rephrase the instruction to be more specific. For example, replace “Always wrap your response” with “Your response must start with three backticks and end with three backticks. Do not output any text before the opening backticks or after the closing backticks.”
The Fenced Code Block Breaks the Document Layout
In some Microsoft 365 apps such as Word, a fenced code block appears as a large gray box. This may disrupt the visual flow of a document. To avoid this, use the custom instruction only in chat sessions where you plan to copy the output elsewhere. Alternatively, use a code editor that handles fences gracefully.
Copilot Adds Explanatory Text Outside the Block
If Copilot still adds introductory sentences like “Here is the code:” before the backticks, update the instruction to forbid any text outside the block. Add the phrase “Do not include any introductory or concluding text. The entire response must be inside the code block.” Test again.
| Item | Custom Instructions | Prompt Injection | System Prompt |
|---|---|---|---|
| Scope | All conversations in the app | Single query only | All interactions in the copilot |
| Setup effort | One-time configuration | Repeated each query | One-time configuration |
| Override difficulty | Low | None | Medium |
| Best for | Daily use in Microsoft 365 | Occasional or testing use | Team or enterprise copilots |
You can now force Copilot to output only fenced code blocks using custom instructions, prompt injection, or system prompts. Start with the custom instructions method if you work in Microsoft 365 apps daily. For one-off queries, use prompt injection. If you manage a team copilot, apply the system prompt in Copilot Studio. After setting the rule, test with a simple code request to confirm the output format. For advanced control, combine the constraint with a language specification inside the fence, such as “`python.