55

I didn't know that the three backticks code fence feature was implemented until I asked this question.

This is a great new feature! I think the code block button in the editor (the {} button) should add three backticks as well, instead of the old four leading spaces syntax.

What I mean is this:

I have the text:

some code
some code line two

When I select that in the editor and press the code block button (or Ctrl/Cmd + K), this would happen:

```
some code
some code line two
```

instead of this:

    some code
    some code line two
16
  • 20
    comment from balpha: "As for the other thing, there are a bunch of places where indenting is recommended. Once code fences have been working for a bit, we may revisit which way of creating code blocks should be the recommended one."
    – user247702
    Commented Jan 12, 2019 at 17:31
  • 18
    You don't mention any reason why you want this change or what advantages it would bring.
    – sth
    Commented Jan 12, 2019 at 19:16
  • 3
    Maybe it would make more sense to have it insert a tab rather than 4 spaces...
    – Travis J
    Commented Jan 13, 2019 at 0:01
  • @usr2564301 Aren't people supposed to view their question in the preview pane before posting?
    – user202729
    Commented Jan 14, 2019 at 8:50
  • 1
    Random arguments in favor of backticks: 1. less characters in the message when there is multiple lines of code; 2. allows to easily change the programming language syntax highlighting.
    – Cœur
    Commented Jan 14, 2019 at 8:52
  • @Cœur As if people write 65536-character answers that often... About 2, I think people don't use manual syntax highlighting very often.
    – user202729
    Commented Jan 14, 2019 at 8:56
  • 1
    @user202729 it did happen once (out of 60000+ posts edited), that I couldn't format a block of code properly because adding 4 spaces on each line was pushing the limit over 65536 characters. (in the end, I voted to close+delete the question as lacking MVCE)
    – Cœur
    Commented Jan 14, 2019 at 9:00
  • 6
    Obviously people prefer ``` upvotes this question and people prefer indent (or hate changes?) downvote this question, but... because adding ``` is just as easy as select and Ctrl+K, why don't you just triple backtick manually and leave Ctrl+K for people who like indentation?
    – user202729
    Commented Jan 14, 2019 at 12:06
  • 3
    There's indeed so much broken python formatting on the main page. Typically function or class definitions don't get indented because the first line is left out of the code block. Or OP doesn't even code format, which leads to broken indentation. Making the code-format button inject triple backticks probably wouldn't help with this specifically, but indeed in python code fences are a huge blessing. Also a lot of people familiar with github syntax will try using triple backticks (I often see that both on main and in chat). Commented Jan 15, 2019 at 0:06
  • FWIW, I have to always use workarounds to get code copy+pasted in with the correct indentation. It's always been my biggest annoyance dealing with code on S.O., and has stopped me from doing needed edits on many occasions. I would prefer if Indented == code was deprecated and removed ASAP. It's unintuitive, problematic, and annoying. It wouldn't be too difficult or bug prone to properly change billions of posts, right? 😇
    – GetSwifty
    Commented Jan 15, 2019 at 19:48
  • @Stargateur What about ~?
    – user202729
    Commented Jun 2, 2019 at 10:42
  • @user202729 upload.wikimedia.org/wikipedia/commons/thumb/b/b9/KB_France.svg/… "why don't you just triple backtick manually and leave Ctrl+K for people who like indentation?" because ` is painful to write on my keyboard. What about ~? I don't understand why you ask that
    – Stargateur
    Commented Jun 2, 2019 at 10:44
  • 1
    @Stargateur (Because triple ~ works as well)
    – user202729
    Commented Jun 2, 2019 at 10:51
  • Duplicate: meta.stackoverflow.com/questions/414860/… Commented Feb 23, 2023 at 18:45

3 Answers 3

3

You missed a use case where three backticks are counterintuitive:

  • If the code block is under a bullet point

    it needs to be indented by additional 4 spaces
    
    • and so on

      for each bullet level
      
  • If I use backticks instead

The code won't be indented
  • To achieve this,

    I need to indent it, too
    

This feature is undocumented, and if you only know backticks, you can't come to discover it 'cuz you wouldn't know why and how the leading space is significant for code formatting.

5
  • 61
    Might just be me... but indenting the backticks is far more intuitive than double indenting. To me, it's pretty clear that you're indenting the code block when you indent with backticks. Whereas without backticks, the indentation has two meanings: 1. Create a code block, then 2. Indent that created code block
    – Rob Mod
    Commented Jan 13, 2019 at 7:42
  • 1
    @Rob When you use backticks and rendered indentation is wrong, you have no idea what is wrong and what to do. Most will probably do nothing, leaving us with tons of posts with broken structure. When you use indentation and it's displayed as regular text -- just like without indent, -- a logical thought is to try to add more indent. Backticks allow to write posts with broken structure while indentation doesn't. Commented Jan 15, 2019 at 1:24
  • 2
    Not sure I follow... If people are unaware of indentation, their post is going to be badly formatted regardless of which approach they use. If they are aware of indentation, then I disagree with the idea that 'you have no idea what is wrong and what to do'
    – Rob Mod
    Commented Jan 15, 2019 at 1:41
  • @Rob If you use backticks without indent, the code will be formatted as code but the code block will be badly indented. You have no idea how to fix the indent, but also no motivation to look for solution 'cuz it's "good enough as it is". Indentation doesn't allow you to escape with that: with incorrect indent, code will not be formatted as code until you find a proper solution. So the backticks encourage problematic behavior and thus shall not be promoted as the primary way of formatting. Commented Jan 15, 2019 at 1:47
  • 2
    Indenting list items can be done with CTRL+K as well. if you have backtick formatting in a list but without indentation, CTRL+K indents it. So for code in lists, remapping CTRL+K to three backticks would make it harder to write code in lists (which in some cases is desired). Additionally, code fences are significantly easier to type (`````` -> ```|``` -> <newline and paste/type>, compared to manually adding four spaces to each line) Commented Jun 2, 2019 at 11:12
19

I'm in favor of this change because:

  • This allows better copy-paste (more compatible with other systems)
  • This reduces the number of wasted space (Character length of the answer, also octet space, just imagine how much space this style of indentation is taking on SO server)
  • Selecting a specific language is far easier (<!-- language: none --> vs ```none)
7
  • Waste space? In the editor? That may be a good point, but normally code line length are not that long anyway. About better copy paste: actually GitHub support 4-space indentation as well.
    – user202729
    Commented Jun 2, 2019 at 10:50
  • 2
    @user202729 I don't have stats but " GitHub support 4-space indentation as well" is not a good point. The question is how many system support "markdown style" vs indentation style. And I think the answer is a lot more support markdown. Also, when copy paste from source, you copy useless space and must remove them somehow, generally shift+tab. This is not needed with markdown style.
    – Stargateur
    Commented Jun 2, 2019 at 10:56
  • With triple backtick (if that's what you mean with "Markdown style") you must remove useless indentation as well, otherwise the code in the question is over-indented and that looks bad.
    – user202729
    Commented Jun 2, 2019 at 11:22
  • 2
    Actually, the only code block style "original" Markdown supported is indentation. daringfireball.net/projects/markdown/syntax#precode
    – user202729
    Commented Jun 2, 2019 at 11:25
  • 2
    @Stargateur That blog post is by the person who originally came up with Markdown; go to the Main tab and find the download from 2004 (when Markdown was introduced). John Gruber has been working on the internet for many, many years. Commented Jun 2, 2019 at 20:25
  • 2
    @Stargateur the first link (on the word "original") is by Wilfred Hughes, but the second link, to daringfireball.net, is John Gruber's website.
    – muru
    Commented Jun 3, 2019 at 1:02
  • I think I miss understood what you tell me. My bad @user202729
    – Stargateur
    Commented Jun 4, 2019 at 5:15
5

I agree with this proposal.
My arguments are as follows:

  • New users may misunderstand how to use 4 spaces style and don't realize that the indentation matters. This results in only the first line of code being displayed as code, or the last line getting ignored, etc.
  • When the question gets edited, the editor often simply adds ``` to define code fences explicitly. But it turns into another awkward thing, lots of spaces in every line of code, e.g.: enter image description here
  • You can see code fences clearly and exactly, when it BEGINS with , **ENDS** with , but not 4 spaces in every line ...
  • You can set programming language easily. You just need to write it after the first ```. As far as 4 spaces style is concerned, I don't know for sure whether you can select a language there, at all! But even if it is possible, it must be more complicated.
2
  • 1
    <!-- language: blahblahblah -->
    – 0xLogN
    Commented Jul 6, 2021 at 21:38
  • @LoganDevine That syntax is officially deprecated for indented code blocks, and may be removed in the future; using it is generally discouraged.
    – zcoop98
    Commented Jul 6, 2021 at 22:59

You must log in to answer this question.

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