71

It would really be useful if Stack Overflow supported GitHub's style of triple backticks for code blocks.

Current Stack Overflow requires indenting every line. That means if I have code like this:

this
  is 
  my
code

I have to manually indent every line with 4 spaces. That mean I can't just copy and paste code from my text editor, I have to reformat it first like this

    this
      is
      my
    code

In my editor that means I end up modifying the file, which is lame. Or I need to copy and paste the code into another file just so I can modify it. Worse, if I'm using JavaScript I might just be copying and pasting directly out of the browser or from JSFiddle, CodePen, etc. which means it's even more annoying and time wasting to have to go first paste that into my editor or to have to hand edit each and every line in Stack Overflow.

Triple backticks solve this issue. I just put triple backticks before and after the code like this:

```
this
  is
  my
code
```

And it should show up like this

this
  is
  my
code

No need to manually edit the identation. GitHub supports this extension to markdown and it's super useful.

Could Stack Overflow please consider adding something similar?

5
  • 1
    It's already on the roadmap, some day: Implement ```-style Markdown code blocks.
    – Arjan
    Commented Jun 14, 2014 at 7:23
  • 1
    "Or I need to copy and paste the code into another file just so I can modify it." - You usually need to do this anyway. Cut irrelevant parts, remove superfluous comments, rename variables, unindent and verify it still compiles and reproduces the issue.
    – CodeCaster
    Commented Jun 14, 2014 at 10:39
  • Since you'd have Vim opened, just do: Esc + mzggVG>'z copy the code you want to paste here and then u'z. Couldn't be simpler. By the way: you can already use triple backticks for inline code. It's useful to typeset a single backslash: \. Otherwise you end up with: ``. This also means that what you are requesting isn't backward compatible...
    – Bakuriu
    Commented Jun 14, 2014 at 12:20
  • 4
    As @arjan predicted, it is implemented now - meta.stackexchange.com/a/322000/332482 (all you had to do was to wait for 5 years)
    – Klesun
    Commented Jan 14, 2019 at 18:22
  • I can't believe it. @JonSkeet told us we didn't need it and even dissed people who said otherwise.
    – user128511
    Commented Jan 14, 2019 at 18:23

2 Answers 2

42

Just highlight the four lines and use Ctrl + K or the {} button in the editor. That will indent everything that's highlighted, so long as at least one line is indented by fewer than 4 characters. (If everything is already indented, it outdents by 4 characters, which is handy if you're posting a snippet which was originally indented a long way.)

7
  • @Arjan I knew that. Jon now has another Silver badge here on meta due to this answer.
    – ClickRick
    Commented Jun 15, 2014 at 17:05
  • @facebook: I can't tell whether or not you're being serious. Backticks are great for inline code, but for a block of code I'd always just indent it instead...
    – Jon Skeet
    Commented Jul 18, 2014 at 1:26
  • 1
    I mean this is better ``` super fast multiline code markdown ``` .
    – eguneys
    Commented Jul 18, 2014 at 7:33
  • @gman: Ctrl-K doesn't, but indenting by another four characters does. Note that you didn't mention lists or quotes in your question (which is nearly 2 years old, btw... why wait so long before commenting?)
    – Jon Skeet
    Commented Apr 22, 2016 at 9:28
  • @gman: If you're indenting 30-60 lines in a list item, you're doing it wrong, IMO. There's no need to launch into ad hominem attacks, btw. While I certainly wouldn't mind triple-backtick support, I don't feel the need for it myself.
    – Jon Skeet
    Commented Apr 22, 2016 at 9:49
  • Okay, I'm done here... We disagree, and I don't see that we're going to get anywhere.
    – Jon Skeet
    Commented Apr 23, 2016 at 5:42
0

If you don't want to indent you can use HTML:

<pre><code>
Lines
Without
Indent
<code/></pre>
Lines
Without
Indent

Note, this is not a 'no' vote, just a workaround. It doesn't even work for all languages.

1
  • 6
    As an aside: this often needs one to manually encode < into &lt; and the like.
    – Arjan
    Commented Jun 14, 2014 at 7:27