```python
print 'bar'
```

without spaces should work: from GitHub help page:

Just wrap your code blocks in ``` and you won't need to indent manually to trigger a code block.


As illustrated in hilz's answer below, you need to indent the ```` with the same indentation level + 2 spaces than your list.
The content of the code block doesn't need to be indented.

1. foo

  ````python
print 'bar'
  ````

  1.

    ````python
print 'bar'
    ````

See this gist as an example:

Answer from VonC on Stack Overflow
Discussions

Long Code Block
So I was trying to find a way to wrap really long syntax highlighted blocks in a div element so there'd be a vertical scrollbar, but I can't seem to find a way to do it. A user posted a rea... More on github.com
🌐 github.com
10
May 19, 2016
Give a code block a filename - Meta Stack Overflow
It's relevant and useful to include the name of the file that a code block should go in. For example, an update to a Next configuration would go in the next.config.js file, an NPM script would go i... More on meta.stackoverflow.com
🌐 meta.stackoverflow.com
Making a github like code block in html
For a code block, you'll want to use the element. For the text highlighting, you'll need JavaScript for that. Look up highlight.js More on reddit.com
🌐 r/css
2
2
June 10, 2022
syntax-highlighting code-blocks in Markdown: question about tree-sitter
As far as I know this isn't currently supported by any package. Don't stop you working on this but markdown-mode support it: https://github.com/jrblevin/markdown-mode/blob/6102ac5b7301b4c4fc0262d9c6516693d5a33f2b/markdown-mode.el#L9026-L9035 More on reddit.com
🌐 r/emacs
8
5
November 8, 2024
People also ask

How do I add syntax highlighting to a Markdown code block?
Add the language name immediately after the opening triple backticks, for example python or javascript. The renderer applies syntax highlighting based on the language identifier. GitHub, VS Code, and GitLab all support this.
🌐
glukhov.org
glukhov.org › home › documentation tools in 2026: markdown, latex, pdf & printing workflows › markdown code blocks: complete guide with syntax, languages & examples
Markdown Code Blocks: Complete Guide with Syntax, Languages & ...
Which programming languages are supported in Markdown code blocks?
Supported languages depend on the renderer. Most platforms support Python, JavaScript, TypeScript, Java, Go, Rust, C, C++, PHP, Ruby, HTML, CSS, SQL, YAML, JSON, and Bash among many others. GitHub uses Linguist (hundreds of languages), while VS Code and most web renderers use highlight.js or Pygments.
🌐
glukhov.org
glukhov.org › home › documentation tools in 2026: markdown, latex, pdf & printing workflows › markdown code blocks: complete guide with syntax, languages & examples
Markdown Code Blocks: Complete Guide with Syntax, Languages & ...
How do you write inline code in Markdown?
Wrap the text in single backticks (). For example, code` renders as inline code. Use this for short snippets, variable names, or commands within a sentence.
🌐
glukhov.org
glukhov.org › home › documentation tools in 2026: markdown, latex, pdf & printing workflows › markdown code blocks: complete guide with syntax, languages & examples
Markdown Code Blocks: Complete Guide with Syntax, Languages & ...
🌐
GitHub
github.com › jspsych › jsPsych › discussions › 1113
How to include a code block in your comment · jspsych/jsPsych · Discussion #1113
Why when you click the <> Add Code Block, In the GITHub comment block. it doesn't default to this automatically. It only outputs `` which makes the code look horrible, Had to search this to finally get it to look right.
Author: jspsych
🌐
GitHub
docs.github.com › en › get-started › writing-on-github › getting-started-with-writing-and-formatting-on-github › basic-writing-and-formatting-syntax
Basic writing and formatting syntax - GitHub Docs
The text within the backticks will not be formatted. You can also press the Command+E (Mac) or Ctrl+E (Windows/Linux) keyboard shortcut to insert the backticks for a code block within a line of Markdown.
🌐
GitHub
github.com › orgs › community › discussions › 42489
Allow to highlight lines in a code block (github flavored markdown) · community · Discussion #42489
This might make things slightly incompatible with vanilla Markdown, since there's supposed to be nothing inside a code block that can be parsed as markup - but I suppose that can be mitigated by using a directive at the beginning, and it would not break existing parsers, it would just cause these backtick characters to be displayed.
Find elsewhere
🌐
GitHub
github.com › Python-Markdown › markdown › blob › master › docs › extensions › fenced_code_blocks.md
markdown/docs/extensions/fenced_code_blocks.md at master · Python-Markdown/markdown
A Python implementation of John Gruber’s Markdown with Extension support. - markdown/docs/extensions/fenced_code_blocks.md at master · Python-Markdown/markdown
Author: Python-Markdown
🌐
Codecademy
codecademy.com › docs › markdown › code blocks
Markdown | Code Blocks | Codecademy
February 17, 2025 - However, the recommended way to create a code block in GitHub Markdown is using triple backticks (```).
🌐
Ardalis
ardalis.com › markdown-code-block-syntax-highlighting-and-diff
Markdown Code Block Syntax Highlighting and Diff - Ardalis (Steve Smith)
November 3, 2021 - If you're writing blog posts, GitHub content, and/or Stack Over flow questions and answers using Markdown, it's often helpful to show code in code blocks of within code fences. Did you know these support many different languages to allow for proper syntax highlighting, including a diff 'language'?
🌐
GitHub
github.com › orgs › community › discussions › 77414
markdown code block title · community · Discussion #77414
November 30, 2023 - I also like the idea, as this really helps to give more sense to code blocks in the documentation. Are there any plans to have this feature implemented? ... There was an error while loading. Please reload this page. Something went wrong. There was an error while loading. Please reload this page. ... I think it would be good to use progressive enhancement here. I.e. it should be readable in markdown, it should be readable in a markdown renderer that does not know the new syntax and finally it should look good when rendered by something that understands it.
🌐
Rost Glukhov
glukhov.org › home › documentation tools in 2026: markdown, latex, pdf & printing workflows › markdown code blocks: complete guide with syntax, languages & examples
Markdown Code Blocks: Complete Guide with Syntax, Languages & Examples - Rost Glukhov | AI Systems & Infrastructure
Indented code blocks use four spaces or a tab at the start of each line. They do not support syntax highlighting and are uncommon in modern Markdown. Fenced code blocks use triple backticks (```) or tildes (~~~) before and after the code. This is the preferred method, especially on platforms like GitHub, because:
🌐
GitHub
gist.github.com › MarcoEidinger › c0f0583f19baca0a8f33bcded644be41
Master GitHub markdown tables with code blocks · GitHub
Master GitHub markdown tables with code blocks. GitHub Gist: instantly share code, notes, and snippets.
🌐
GitHub
github.com › github › markup › issues › 893
Long Code Block · Issue #893 · github/markup
May 19, 2016 - So I was trying to find a way to wrap really long syntax highlighted blocks in a div element so there'd be a vertical scrollbar, but I can't seem to find a way to do it. A user posted a rea...
Author: github
🌐
StackEdit
stackedit.io
StackEdit – In-browser Markdown editor
StackEdit provides very handy formatting buttons and shortcuts, thanks to PageDown, the WYSIWYG-style Markdown editor used by Stack Overflow. Whether you write, you review, you comment… StackEdit's layout provides you with the flexibility you need, without sacrifice. StackEdit’s Scroll Sync feature accurately binds the scrollbars of the editor panel and the preview panel to ensure that you always keep an eye on the output while writing. StackEdit can sync your files with Google Drive, Dropbox and GitHub.
🌐
GitHub
docs.github.com › en › get-started › writing-on-github › working-with-advanced-formatting
Working with advanced formatting - GitHub Docs
You can streamline your Markdown by creating a collapsed section with the <details> tag. Share samples of code with fenced code blocks and enabling syntax highlighting.
🌐
Tolaria
tolaria.md
Tolaria
Engineering context that helps developers and agents understand the codebase. ... Every note is a Markdown file with a YAML frontmatter. No database, no proprietary format. Read them with any editor, grep them from the terminal, version them with Git. ... Block-based editing with slash commands, wikilinks, raw Markdown, whiteboards, media previews, table navigation, and note width controls.
🌐
Markkit
markkit.dev › guides › markdown-code-block
Markdown Code Block Guide - Syntax Highlighting | Markdown Visualizer
Different platforms may render markdown code blocks slightly differently. GitHub-flavored markdown code blocks support additional features like line highlighting and diff formatting.
Top answer
1 of 2
10

I fully agree with the proposal and I don't understand all of the downvotes.

This is already doable e.g. via python markdown extensions, like this:


    ``` py title="bubble_sort.py"
    def bubble_sort(items):
        for i in range(len(items)):
            for j in range(len(items) - 1 - i):
                if items[j] > items[j + 1]:
                    items[j], items[j + 1] = items[j + 1], items[j]
    ```

See here:

  • https://squidfunk.github.io/mkdocs-material/setup/extensions/python-markdown-extensions/#snippets
  • https://squidfunk.github.io/mkdocs-material/reference/code-blocks/#adding-a-title
2 of 2
9

It's relevant and useful to include the name of the file that a code block should go in

Not always. Many questions give a short block of text as an example of where they're having the problem, and answers respond with an amended version of the code block. It doesn't matter what the name of the file is (it really could be anything with an appropriate extension). The code might not even be for a file! Most regexes, for example, can be used in almost any language, but where I find myself using them most often is in the search box of my IDE. Furthermore, not everything that finds itself in a code block is even code (e.g. error messages).

Needless to say, you don't need to provide a file name (or even a context) for every block of code. So we're back to the start: People will still assume they don't need to include the file name when it would be helpful to do so. Or maybe people will include a file name in contexts where they don't need to, making the post noisier.

Lastly, I prefer regular markdown formatting (if not plain text) over your suggestion, both aesthetically and formatting-wise — your suggested format is much too tricky for the vast majority of people to use.

🌐
Pluralsight
pluralsight.com › blog › tech guides & tutorials
Highlighting React Code in GitHub Flavored Markdown | Pluralsight
You can see this GitHub gist here. Now indent the entire code four spaces. You will notice that the code block will fade once you have indented the code. Now commit this change, and you will see that the Markdown will format the code block this time.
🌐
APITemplate.io
apitemplate.io › home › free markdown to pdf converter | real-time preview & editor
Free Markdown to PDF Converter | Real-Time Preview & Editor - APITemplate.io
May 15, 2026 - The tool supports standard Markdown syntax including headings, bold, italic, links, images, ordered and unordered lists, tables, code blocks, inline code, blockquotes, and horizontal rules.
🌐
Quora
quora.com › How-do-you-add-a-code-block-in-Markdown
How to add a code block in Markdown - Quora
Answer: To add a code block in Mark Down, enter a line with three back-quotes, then enter your block of code, followed by another line with three back-quotes. For example: [code]``` if ($pid > 0) { sleep 1 while !-e $FIFO; } else { system("mknod", $FIFO, "p") == 0 ...