Code Blocks
Code blocks let you present pretty, syntax-highlighted code. There are two related blocks:
Code block: a single snippet in one language.
Code tabs: several snippets shown as tabs, for instance the same code in different languages.
To add either one:
Type / on a new line or click on the on the left of any line.
Select Code block or Code tabs
Supported Languages
The following languages are currently supported for syntax highlighting:
Bash, C , C-Like, C++, C#, CSS, CSV, D, Dart, Django, Docker, Go, Groovy, HTML, HTTP, Javascript, Java, JSON, JSX, Kotlin, Lua, Markdown, Markup, Objective-C, Pascal, Perl, PHP, Powershell, Python, R, Rust, Ruby, Sass, Scss, Scala, SQL, Squirrel, Swift, Typescript, VB.NET, Visual Basic, XML.
If you need more languages, we'd be happy to support them. Give us a nudge!
Syntax error highlighting only shows while editing
Code Tabs Example
A Code tabs block showing the fibonacci sequence in different languages:
No Formatting Code Block
Code blocks can have no formatting if language "None" is selected. Also, if only language "None" was provided for the code block, then the language selector will disappear unless if the tab has a title. For example:
And another one with title:
Titled Tabs
You can also provide titles for each tab, which is a great way to identify and group the content. For example:
Collapsing Contents
When a code block has more than 100 lines, it is shown with its foldable regions collapsed, so a long snippet does not dominate the page. Folding is language-aware: each function or indented block folds at its own boundary, and languages without their own grammar fall back to indentation-based folding. Example:
To fold or unfold a region yourself, click the fold arrows in the gutter to the left of the code.
Show Line Numbers
There are two ways to show line numbers:
For each tab, you could enable showing line numbers from the tab options.
Globally, you could enable showing line numbers for all code blocks in the project. See option
code.showLineNumbersin advanced settings.
Highlight Code
Lines of code can be highlighted. From the tab option menu, you can write down the line numbers to be highlighted. The syntax for highlighting is as such:
Use dashes
-to make an inclusive range.Use commas to separate rules.
For example:
5-7: Highlights lines 5 to 7.1,2,3: Highlights lines 1, 2 and 3.1,10-20,25: Highlights lines number 1, 10 to 20 and 25.
See the following code blocks for examples:
Line Wrapping
By default, code block lines wrap unless a line exceeds 120 characters or the view is in mobile mode. However, you have the option to enable or disable line wrapping as needed.
Need help? Visit our community forums or contact us.