Microsoft OneNote, a handy place to keep programming notes, save code snippets, and organize technical information, lets you keep your code and explanations together in one notebook, whether you are learning Python, working with HTML, or documenting a project.
However, OneNote does not offer a dedicated code editor with full syntax highlighting like Visual Studio Code or Notepad++. If you paste code directly into a regular note, the formatting may look messy, especially when indentation, spacing, and line breaks matter.
In general, you can still make your code clean, readable, and easy to copy. By choosing a suitable font, adjusting the paragraph layout, using a code-friendly background, and applying a few practical formatting techniques, you can create professional-looking code notes.
In this guide, you will learn how to properly format code in OneNote, including the easiest method for beginners and alternative ways to improve the appearance of your code.
How To Properly Format Code In OneNote

To properly format code in OneNote, follow these steps:
- Open OneNote and navigate to the page where you want to add your code.
- Insert a new text box or click an empty area on the page.
- Paste or type your code into the note.
- Select the entire code snippet and choose a monospaced font, such as Consolas or Courier New.
- Adjust the font size, paragraph spacing, and indentation to make the code easier to read.
- Use a light background, table, or text container to separate the code from regular notes.
- Add a short explanation or heading above the code so you can identify its purpose later.
- Review the formatting and test-copy the code into a code editor to make sure the spacing and line breaks remain correct.
Step 1: Open OneNote and Create a Dedicated Code Page
Start by opening Microsoft OneNote on your Windows computer. Depending on your version, you may be using the OneNote desktop app or the OneNote app available through Microsoft 365.
Open the notebook and section where you normally keep your programming notes. If you frequently save code, consider creating a separate section named Code Snippets, Programming Notes, or Project Documentation. This makes it easier to find your code later.
Create a new page by selecting Add Page or the plus sign next to your existing pages. Give the page a descriptive title, such as Python File Handling Example or HTML Login Form Code.
A clear page title helps you recognize the purpose of the snippet without opening the entire note. If you are working on a larger project, you can also create separate pages for different programming languages or features.
Once your page is ready, click in an empty area to create a text container. OneNote uses flexible text containers, so you can place text in different areas of the page instead of following a fixed document layout.
Step 2: Paste or Type Your Code Carefully
The next step is to add your code to OneNote. You can type the code manually or copy it from a programming environment, website, or text editor.
Before pasting, remember that code depends on accurate spacing, indentation, and line breaks. For example, Python uses indentation to define code blocks, so changing the spacing can affect how the program works.
When you paste code into OneNote, check whether the application preserves the original line breaks and indentation. If the code comes from a webpage, it may also include unwanted formatting, hyperlinks, or colors.
If the pasted code looks unusual, undo the action and try pasting it as plain text when that option is available. You can also paste the code into a plain-text editor first, copy it again, and then insert it into OneNote. This can help remove unwanted webpage styling.
After pasting, compare the code with the original version. Make sure braces, quotation marks, spaces, and indentation are still present. This is especially important when working with Python, JavaScript, JSON, YAML, or shell commands.
Step 3: Select a Monospaced Font
One of the most important steps in formatting code is choosing the right font. Regular fonts are designed for ordinary reading, but programming code is generally easier to understand when every character occupies the same horizontal space.
A monospaced font gives each character an equal width. This makes indentation, columns, brackets, and code alignment easier to recognize.
To change the font in OneNote, select the entire code snippet. You can usually do this by clicking and dragging over the code or placing the cursor inside the text and using Ctrl + A carefully. If the text container contains other content, make sure you select only the code before changing its formatting.
Go to the Home tab and locate the font controls. Open the font menu and choose a monospaced option such as Consolas, Courier New, or another available coding-friendly font.
Consolas is often a comfortable choice because its characters are designed for screen readability. Courier New is another familiar option that works well for simple code snippets.
After selecting the font, review the code. Characters such as 1, l, I, 0, and O should be easier to distinguish. This can help reduce mistakes when reading or copying code.
Step 4: Adjust the Font Size and Text Appearance
Once you have chosen a monospaced font, adjust the font size so the code is easy to read without taking up too much space.
For most code notes, a font size between 10 and 12 points is a reasonable starting point. If you are viewing the page on a large monitor or presenting the code to someone else, you may prefer a slightly larger size.
Select the code and use the font-size controls on the Home tab. Increase or decrease the size until the lines are comfortable to read.
You can also apply bold formatting to a short heading above the code, but avoid making the entire code snippet bold. Heavy text can make programming symbols and punctuation harder to distinguish.
For regular code, a simple combination of black or dark text, a monospaced font, and consistent sizing is usually more effective than excessive colors or decorative styles.
If you want to emphasize a particular line, you can temporarily use a text color or highlighting option. However, keep the main code formatting consistent so the page remains easy to scan.
Step 5: Organize Indentation, Line Breaks, and Spacing
Correct indentation is essential when you format code in OneNote. It helps readers understand the relationship between functions, loops, conditions, and other code blocks.
After pasting your snippet, inspect the beginning of each line. Check whether spaces or tabs have been removed or changed. If the indentation is incorrect, place the cursor at the affected line and restore the required spacing.
For example, Python code should maintain indentation after statements such as if, for, and def. JavaScript, C++, and Java may use braces to define blocks, but consistent indentation still makes the code easier to understand.
You should also check the line breaks. A long line that wraps across the page can be difficult to read. OneNote may wrap text depending on the width of the text container, but visual wrapping does not always mean that a real line break was inserted.
Avoid adding unnecessary blank lines inside short snippets. At the same time, leave enough space between separate examples so they do not appear as one large block of text.
If the code is too wide, you can resize the text container or reduce the font size slightly. Try to keep the original structure intact rather than forcing every line into a narrow area.
Step 6: Separate Code From Regular Notes
A useful way to make code stand out is to separate it from your explanations. This is particularly helpful when a OneNote page contains a mixture of programming instructions, screenshots, definitions, and code examples.
Before the code, add a short heading such as Example: Reading a Text File or JavaScript Function for Validating Input. Format this heading using bold text or a slightly larger font.
You can then place the code in its own text container. OneNote allows you to move and resize text containers, so you can create a clear visual separation between the explanation and the code.
Another option is to use a one-cell table as a simple code area. Go to Insert > Table and create a table with one column and one row. Paste the code into the cell, then apply a monospaced font.
A table can provide a defined boundary around the code, making it easier to distinguish from surrounding text. However, very long snippets may become awkward to manage inside a table, so this method works best for short examples.
You can also apply a subtle text highlight or background color if your OneNote version supports the formatting you need. Use a restrained color so the code remains readable in both light and dark viewing environments.
Step 7: Add Comments and Explanations Around the Code
Proper code formatting is not only about appearance. Your notes should also help you understand what the code does when you return to it later.
Add a brief explanation above or below each snippet. For example, if you save a Python function, explain what the function accepts, what it returns, and why you might use it.
You can include comments inside the code when they are part of the original example. Comments can explain an important operation or clarify why a particular command is necessary.
For instance, a Python snippet might include a comment describing a file-reading operation. Keep comments short and use the correct comment syntax for the programming language.
Avoid mixing long explanations directly into the middle of a code block. Instead, place the explanation outside the code or in a separate text container. This helps preserve the original snippet and makes copying easier.
If the code requires certain libraries, packages, or settings, record those details nearby. This can save time when you use the snippet again.
Step 8: Check the Code Before Saving the Page
Before finishing, review the entire page to make sure the code is readable and accurate.
Look for missing indentation, altered quotation marks, broken line breaks, or accidental formatting changes. Check whether the code is still in the correct font and whether any lines have become difficult to distinguish.
It is a good idea to copy the formatted code from OneNote and paste it into a suitable code editor. This lets you check whether the snippet retains its important structure.
If the code is executable, test it in the appropriate programming environment. OneNote is primarily a note-taking application, so it does not replace a dedicated editor or compiler.
Finally, save the page and organize it under the appropriate notebook section. You can add tags, links to related pages, or a short summary to make the code easier to locate in the future.
Use a Dedicated Code Editor Before Pasting
If you want cleaner code formatting, write or prepare the snippet in a dedicated code editor first. Applications such as Visual Studio Code or Notepad++ are designed for programming and provide features such as syntax highlighting, indentation controls, and code navigation.
You can then copy the code into OneNote for documentation. This approach is useful when you want OneNote to store the explanation while the dedicated editor handles the actual programming work.
Keep in mind that copying from a code editor may not preserve every visual feature, such as syntax colors or editor-specific backgrounds.
Paste a Screenshot of the Code
Another option is to insert a screenshot of your code into OneNote. This method preserves the exact visual appearance of the snippet, including syntax highlighting and indentation.
To use this approach, open your code editor, display the code clearly, and capture the relevant area. Then go to Insert > Pictures or use the available screen-clipping option in OneNote.
Screenshots are useful for tutorials, presentations, and visual references. However, the code inside an image cannot be copied as easily as ordinary text. For that reason, screenshots are best used alongside a selectable text version of the code.
Use a Markdown or HTML-Based Workflow
If you regularly write technical documentation, you can prepare code in Markdown or HTML and then transfer the content into OneNote.
Markdown supports fenced code blocks, which make it easy to organize snippets in a consistent structure. You can prepare the document in a suitable editor and then copy the code and explanations into OneNote.
This workflow is useful for longer documentation projects, although OneNote may not preserve all Markdown styling automatically. You may need to apply the font and layout formatting manually after pasting.
FAQs
Does OneNote have a dedicated code formatting option?
OneNote does not provide a full programming-code formatting system with automatic syntax highlighting, code execution, and advanced code editing. You can still format code manually using monospaced fonts, spacing, text containers, and tables.
What is the best font for code in OneNote?
Consolas is a good choice for many users because it is designed for screen readability. Courier New is another suitable monospaced font. The best option depends on your preferred appearance and which fonts are available on your computer.
Can I add syntax highlighting to OneNote?
OneNote does not generally provide the same automatic syntax highlighting found in dedicated code editors. You can use colored text manually, paste a formatted screenshot, or prepare the code in another application before adding it to OneNote.
Why does my code lose its formatting when I paste it?
Code may lose its formatting because the original application uses styles that OneNote does not preserve. Webpages may also add unwanted formatting. Try pasting as plain text, checking the indentation, and applying a monospaced font afterward.
Can I run code directly in OneNote?
OneNote is mainly a note-taking and documentation tool. It is not a general-purpose programming environment, so you should use a suitable editor, interpreter, or development environment to run and test your code.
Bottom Lines
Learning how to properly format code in OneNote can make your programming notes much easier to read and maintain. The most effective approach is to use a monospaced font, preserve indentation and line breaks, separate code from explanations, and organize snippets under clear page titles.
For short examples, a dedicated text container or one-cell table can work well. For more advanced formatting, prepare the code in a dedicated editor or use a screenshot alongside the original text.
Although OneNote does not replace a professional code editor, it can be an excellent place to organize programming knowledge, save useful snippets, and document your projects in a clear, beginner-friendly way.