What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 127 Stack Exchange communities.

Currently, Stack Exchange’s Markdown parser only allows four-space indents to represent code blocks:

// some code
// another line of code

GitHub Flavored Markdown and other Markdown implementations allow for an alternative syntax, that doesn’t require indenting each code line:

```
// some code
// another line of code
```

This is much more convenient to type out. It would be super useful if Stack Exchange could support this syntax.


By extension, this syntax also allow you to specify the source language right after the opening ```:

```js
// some code
// another line of code
```

…which would then enable syntax highlighting for that specific language. Although it’s interesting metadata, I don’t think this feature is needed on Stack Overflow, as the syntax highlighting library works pretty well for various languages.

So, even if you would allow this syntax but ignore the ```language, this would greatly improve my productivity on Stack Exchange.

What do you think?

The moderators are currently collecting feedback regarding this feature, so please post a comment with your thoughts. Good idea? Bad idea? Don’t really care? What do you think are the benefits/drawbacks? Experiences? Let them know! Thanks!

share|improve this question
35  
+1 I think this would be great. Not having to worry about an extra 4-space indent when editing code the textbox would make it less necessary to pull it out into an external editor and then paste it back in. –  cdeszaq Mar 9 '12 at 13:20
1  
Yes, I am aware of this, but for editing existing code, it would make things easier. –  cdeszaq Mar 9 '12 at 13:23
7  
@BoltClock'saUnicorn: This feature wouldn't be intended for people who accidentally stumble across it, but for people who actually know it's there and find it is a more convenient method of posting code than indenting by four spaces (I would probably use it). –  Greg Hewgill Mar 9 '12 at 20:35
4  
+1 This also makes it easier to edit posts that forgot code blocks. –  Tom Wijsman Mar 10 '12 at 6:14
13  
This is a reasonable suggestion, we're considering it. A nice side effect would be that this kind of code block wouldn't have a "placed after a list" problem. –  balpha Mar 11 '12 at 15:33
2  
When adding it, please consider handling four-backtick "fences" just like the regular three-backtick fences. On a German keyboard it's extremely annoying to enter an odd number of backticks since it's a deadkey and you cannot change it without side-effects. –  ThiefMaster May 17 '12 at 10:03
1  
@Tom: selecting code and pressing Ctrl+K (or the {} button in editor toolbar) seems easier than typing backticks six times at two different places. –  BalusC Aug 9 '12 at 14:56
1  
@Chichiray: Clicking is easier than dragging as the latter requires you to aim such that you select the text right, and ` is a single key where Ctrl+K is not; as a result click ``` click ``` is faster to perform. –  Tom Wijsman Aug 9 '12 at 15:31
2  
I can't count how many times I've gone through and put 4 spaces on the clipboard, then flew down the lines of code repeatedly hitting Ctrl+V, DownArrow, Home to indent the entire block. –  gobernador Aug 9 '12 at 17:38
2  
@gobernador what text editor are you using?, it is usually possible to indent the whole thing if it is selected and then insert some tabs. Or in the editor of this site, just select the whole thing and click the 'code' icon and will indent for you –  ajax333221 Aug 9 '12 at 22:58
1  
6  
Any update on this, it's nearly a year! –  Pineapple Under the Sea Jul 17 '13 at 20:03
1  
Seems like the community has spoken here and would like to see it implemented. Coming up on a year now, is it gonna happen? –  jcollum Jul 25 '13 at 15:56
1  
Is there something we can do to make this happen? –  Alan H. Feb 20 at 19:50
3  
+1 Please implement this. It's really inconvenient to add 4 spaces (not even a tab) for each line of code I write in SO. –  raviolicode Mar 10 at 13:05
show 11 more comments

6 Answers

You can currently specify a language to be syntax-highlighted.

Example:

<!-- language: lang-js -->

    //JS code...

This should not discourage the question, it is even a plus because they don't need to code this feature anymore.

The only thing left to do is to implement the ```` code blocks system (which I believe, is not that hard)

share|improve this answer
    
Good to know — thanks! Sounds like a regular expression that converts the triple backtick-syntax into the existing syntax behind the curtains could be a quick fix. –  Mathias Bynens Mar 10 '12 at 8:14
9  
This is not as nice than the triple-backtick syntax though. –  ThiefMaster Mar 23 '12 at 7:26
    
github supports the same using ```php for example –  pocesar Apr 11 '13 at 20:35
add comment

Benefits

  • Compatibility with GitHub-Flavored Markdown and other Markdown libraries that support this de facto standard (and possibly its variants). This enables copy-pasting from gists or Markdown files within repositories, which can be useful on programming-related sites like Stack Overflow.
  • The triple backtick syntax is easier and faster to type than indenting each line of code with four spaces.
  • Saves some storage in the database, too! ;)

All the above benefits apply to the new syntax, even without the additional functionality to specify the programming language. With that functionality, I’ll add another (huge) benefit:

  • As it’s much easier to specify the language of code blocks using this syntax (compared to the current syntax), I wouldn’t be surprised if this change caused more people to explicitly declare the programming language, so that the syntax highlighting can be more accurate. This is useful for answers that include, e.g. separate HTML and CSS code blocks.

Drawbacks

I honestly can’t think of any. It’s very likely no one is using the triple backtick-syntax at the moment as it doesn’t work; so I doubt it will break anything. If you don’t like the new syntax, feel free to stick to the old one.

Experiences

Ever since GitHub enabled this syntax, I’ve grown accustomed to it. I ♥ triple backtick syntax, and every time I’m forced to go back to the four-space-indent syntax, it feels awkward.

share|improve this answer
6  
159 places have a triple-backtick (I was scared to search for a double-backtick, though): data.stackexchange.com/stackoverflow/query/edit/76842 As far as I can tell, none of them would be broken by implementing this. –  minitech Aug 9 '12 at 15:20
1  
@minitech Thanks for the query. I see a few that might cause problems, but with just 159 results it's not beyond the realm of possibility to manually verify that each one is correct, or is fixed, if this were implemented. –  Servy Aug 9 '12 at 15:23
add comment

The main benefit I see coming from this is that you'll be able to see a little more of the code in the edit box without wrapping. Making it a little easier to specify language is nice too, but I find that the tags take care of it in a large majority of cases.

I'm certainly not opposed, but I don't think it will make fixing code formatting that much easier. You can already select the lines and hit the Code Sample button to add the first level of indent. From there on it's the same amount of work to add additional indents--either copy four spaces and use CTRL-V as a tab key for each line, or copy into editor, fix, and copy back. I think being able to select multiple lines and indent/unindent multiple times would be a lot more helpful than this.

On the downside, ``` would be one of the least intuitive bits of markdown. Doesn't really have the same WYSIWYG feel, at least for me. Using the same sequence to start and end the formatted section could also be a bit confusing, particularly when there are multiple code blocks in a post.

Something like {{{ to start and }}} to end would be clearer, or a custom html tag like, <codeblock></codeblock>, or just allow the <code> tag to span multiple lines to create a block. Or, combine with specifying the language, for example: <java></java>.

share|improve this answer
    
It's a big help when you have mixed tabs and spaces, too. –  minitech Aug 9 '12 at 15:36
2  
@bemace I know you can select the lines and then hit the Code button, but as a heavy keyboard user, I find typing three backticks, a line break, Cmd + V, another line break, and three closing backticks is much faster. –  Mathias Bynens Aug 9 '12 at 15:36
    
I like the thought of the language tags <java></java>. –  gobernador Aug 9 '12 at 17:35
3  
But why invent a new syntax when several existing Markdown implementations already support the triple backtick syntax? Interoperability (and copy-pastability) with other Markdown flavors is a big plus IMHO! –  Mathias Bynens Aug 9 '12 at 19:05
    
@MathiasBynens as I explained, with multiple code blocks delimited by ``` separated by a line or two of text (as is common on Stack Overflow) it could get very confusing trying to determine which ``` are starting a code block, and which are ending a code block-- or rather, which lines are code and which are text. –  Brad Mace Aug 9 '12 at 19:08
1  
@bemace There would be no confusion if the existing Markdown implementations that support this syntax are followed. Triple backtick code blocks need two line breaks before and after (unless they’re at the start or end of the input). –  Mathias Bynens Aug 16 '12 at 8:06
add comment

I don't really care either way whether this gets implemented: it's a superfluous extension to Markdown (unlike, say, inline HTML, spoiler markdown, or the as-yet-unimplemented-on-Stack-Exchange table syntax), but it is true the fenced code block has been added to pretty much every Markdown library that doesn't stick to just the original spec.

However, the triple backtick syntax is a GitHub extension, and it's not exactly the "de facto standard" Mathias Bynens portrays it as.

Fenced code blocks, as far as I understand it, were first introduced in the PHP Markdown Extra library as the following:

Fenced code blocks are like Markdown’s regular code blocks, except that they’re not indented and instead rely on a start and end fence lines to delimit the code block. The code block start with a line containing three or more tilde ~ characters, and ends with the first line with the same number of tilde ~. For instance:

This is a paragraph introducing:

~~~~~~~~~~~~~~~~~~~~~
a one-line code block
~~~~~~~~~~~~~~~~~~~~~

Other implementations followed suit, by either supporting only 3+ tildes or supporting both 3+ tildes and 3+ backticks. I ran through the implementations I know of, and Wikipedia's (incomplete) list of implementations (before GitHub went down), and found:

Supports only the 3+ tilde syntax:

Supports only the triple backtick syntax:

Supports both:

Supports neither, or is unspecified:

Based on this, I think Stack Exchange should support the tilde syntax, the triple backtick syntax, and the 3+ backtick syntax. Bonus: support of either the tilde syntax or the 3+ backtick syntax should solve the deadkey problem.

The syntax toolbar should continue to indent code with 4 spaces as per the original Markdown spec, or if that's not feasible, it should use the tilde syntax, which has the benefits of being the original version, compatible with GitHub, and marginally more supported by existing implementations.

share|improve this answer
3  
Excellent research! Good point about supporting all variants of the syntax to eliminate the dead key problem. –  Mathias Bynens Aug 16 '12 at 8:09
2  
Thanks for the great research. I'm still not 100% sure we should do it, but this answer contains excellent information to base a decision on. –  balpha Aug 17 '12 at 9:49
1  
«Bonus: support of either the tilde syntax or the 3+ backtick syntax should solve the deadkey problem.»: while I agree with you say, this last one is false. In my standard Portuguese keyboard, both `` ` `` and ~ are deadkeys. –  JMCF125 Feb 16 at 18:49
    
Both tilde and backtick is dead keys on Swedish keyboards too. –  Emil Vikström Apr 2 at 18:00
add comment

Only drawback I could see is being unclear as to which is the 'right way'. But on a whole I think this would be a great feature to add for someone like me who doesn't want to be using either the button above or spacing in.

share|improve this answer
    
But there is not "right way". All ways that works are correct. –  Emil Vikström Apr 2 at 18:01
add comment

On Stack Overflow, all the time I'm seeing this happen to people who don't know the syntax.

class Foo {

public static void main(String[] args) {
    // y my code no is working
    Sytem.out.println(qrew3r);
}

}

Some people stop there and never fix it because it's close enough. Others figure out that the first and last lines aren't part of the code block because they're not indented. So they indent those two lines - only those two lines - because the editor doesn't provide an apparent way to indent the whole block.

class Foo {

public static void main(String[] args) {
    // y my code no is working
    Sytem.out.println(qrew3r);
}
}

The result is that we have a site that systematically causes newbs to post questions with poorly-formatted code. Whether or not you like fenced code blocks, when you look at all of the mistakes that users are making, you've got to appreciate that the current UX is broken.

share|improve this answer
add comment

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .