Markdown Tabs vs Spaces: How CommonMark Reads a Tab
Quick answer
CommonMark does not turn tabs into spaces. Your tab stays a tab in the file and in code output. But wherever indentation decides the structure of a document, a tab is counted as spaces up to the next tab stop, and the tab stop is 4 characters wide. So one tab usually does the job of four spaces, with a few edge cases worth knowing.
The rule: a tab stop of 4 columns
Section 2.2 of the CommonMark spec says it directly: "Tabs in lines are not expanded to spaces. However, in contexts where spaces help to define block structure, tabs behave as if they were replaced by spaces with a tab stop of 4 characters." The GitHub Flavored Markdown spec repeats the same rule.
The word that matters is stop. A tab doesn't mean "add four spaces". It means "move to the next column that is a multiple of four". In the spec's examples, → stands for a tab. Two spaces followed by a tab reach column 4, so the tab is worth only two spaces:
→foo→baz→→bimThat line is an indented code block, exactly like →foo→baz→→bim on its own. The output is the same in both cases:
<pre><code>foo→baz→→bim
</code></pre>A tab can open an indented code block
The spec notes that "a tab can be used instead of four spaces in an indented code block." John Gruber's original Markdown syntax page says the same: indent every line by at least "4 spaces or 1 tab", and one level of that indentation is removed from each line.
Two details matter here:
- Tabs inside the code are passed through as literal tabs. The
→betweenfooandbazabove is still a tab in the HTML. - You can mix both kinds. A line with four spaces followed by a line starting with a tab (
→bar) stays in one code block, givingfooandbaron separate lines.
Remember that an indented code block cannot interrupt a paragraph, so you need a blank line before it. We covered the four-space side of this in indented code blocks.
Tabs in list items
Lists are where tabs turn up most. Gruber's page says each later paragraph in a list item "must be indented by either 4 spaces or one tab", and a code block inside a list item "needs to be indented twice — 8 spaces or two tabs." CommonMark keeps that behavior:
- foo
→barHere bar becomes a second paragraph of the same list item, exactly as four spaces would. Add a second tab and the meaning changes:
- foo
→→barNow the item holds a paragraph foo and a code block whose content is bar, with two leading spaces. Two tabs are eight columns. The list item's content starts two columns in, the code block takes four, and the remaining two columns are kept as spaces.
Tabs also work for nesting. In the spec's example, a line written as a tab followed by - baz lands as a third-level item under - foo and - bar. If you switch between bullet markers or mix tabs and spaces, count columns, not characters.
Tabs after a block quote marker
A block quote usually starts with > and an optional space. When a tab follows >, it is treated as three spaces, and one of those is the optional space that belongs to the delimiter. That leaves two columns of content from the first tab. A second tab adds four more, so the text sits six columns in:
>→→fooThe result is a block quote containing an indented code block whose content is foo. It catches out anyone who only wanted to push a quote to the right.
Tabs that stay tabs
Not every tab is structural, though. A tab between # and the heading text is fine: #→Foo renders as an h1. A line of *→*→*→ is a thematic break. In both cases the tab is just whitespace.
And in a code block or a plain paragraph, a tab is kept as typed. That is why tab-separated text survives inside a fenced block: Markdown does not touch it.
Which should you use?
Pick one per file. The spec guarantees that a tab at the start of a line acts like spaces up to the next multiple of four, so either works, but mixed indentation makes the column math easy to get wrong. If a list or code block renders oddly, look at the leading whitespace first: a tab after two spaces is worth two columns, not four, and a tab after > or a list marker is shorter than it looks.
How Carets fits in
Whitespace bugs are plain-text bugs, and you fix them by looking at the file itself. Carets is a native notes and code editor for iPhone, iPad, and Mac that keeps your Markdown, plain text, and code as ordinary files, so the tab you typed is the tab on disk, with nothing rewritten behind your back. Syntax highlighting for Markdown and code helps you see what the structure is, and files, projects, and tags keep related notes together. Carets is built natively for iOS and Mac, so a quick whitespace fix on the phone opens as fast as one at your desk.
Get Carets for iPhone, iPad, and Mac on the App Store.
Frequently Asked Questions
Does Markdown convert tabs to spaces?
No. CommonMark says tabs in lines are not expanded to spaces. Tabs only behave as spaces, with a tab stop of 4 characters, where indentation defines block structure. Inside code, a tab stays a literal tab in the output.
Can I use one tab instead of four spaces for a code block?
Yes. The spec says a tab can be used instead of four spaces in an indented code block, and Gruber's original syntax page says "4 spaces or 1 tab".
Why does a tab after > give me a code block?
A tab after the block quote marker counts as three spaces. One of them is the optional space after >, so a second tab leaves six columns of indentation inside the quote. That produces a code block starting with two spaces.
Is a tab in a heading line a problem?
A tab between # and the heading text is fine: #→Foo renders as an h1. A tab before the # would count as four columns of indentation and turn the line into an indented code block.