1

Usually, when selecting the code (of course, in edit mode) and pressing Ctrl+K, or {}, the selection is correctly formatted as code:

Sub Foo()
    'Whatever in here...
End Sub

But sometimes, the above way of formatting, simply does not work! The code is moved for one Tab space, but it looks messed up.

In such a situation I use the next (less elegant) way, using ``:

Sub Foo() 'Whatever in here... End Sub

Is it a trick, something to be known, in order to avoid such a behavior?

I searched for such a clarification, but I could not find anything conclusive. A link to a place supplying a solution for the above described issue will be good enough.

Edited:

Using ``` on the lines in front and after the code:

    Sub Foo()
        'Whatever in here...
    End Sub
8
  • 2
    use three ``` one line before and one line after the code block. Commented Jun 16, 2020 at 13:37
  • 1
    @Scott Craner: Thanks! I will edit my question and experiment it. But One character before plus another one after, meant two... Where to put the third one? :)
    – FaneDuru
    Commented Jun 16, 2020 at 13:40
  • no use three together on the line before and three more on the line after: ``` code ``` Commented Jun 16, 2020 at 13:41
  • 1
    @Scott Craner: Yes! this was the trick! Thanks! If you will place an answer, I will mark it as accepted one.
    – FaneDuru
    Commented Jun 16, 2020 at 13:43
  • 4
    stackoverflow.com/help/formatting tells you all that and more... Commented Jun 16, 2020 at 14:02
  • @Heretic Monkey: Thanks! I will study it...
    – FaneDuru
    Commented Jun 16, 2020 at 14:04
  • @FaneDuru also with those you can influence the language formatting ```python code ``` for example. stackoverflow.com/editing-help#syntax-highlighting Commented Jun 16, 2020 at 15:30
  • 1
    @β.εηοιτ.βε: The (canonical) one with the most detailed information is What is syntax highlighting and how does it work? Commented Jun 17, 2020 at 12:41

1 Answer 1

6

But sometimes, the above way of formatting, simple does not work!

What I see happening in the Suggested Edit queue, when people try to fix broken code formatting is:

  • There isn't a line break between regular text and the code so the code formatting is broken.

    It looks like this:
        code is indented but not formatted correctly
    
    Instead of like this:
    
        code is formatted
    
  • Sometimes, selecting the blank space above the code in addition to the code itself and then trying to format that throws things off. I'm not sure why but I've seen it get confused as to whether it should be formatting or un-formatting the code.

  • It doesn't play nicely with lists. Note that I had to use <pre> tags in the first bullet point and it still isn't indented as if it's in the list. Putting code in a list is hard to get right.

2
  • Re "It doesn't play nicely with lists.": Indeed. The workaround is to use an external text editor. Commented Jun 17, 2020 at 12:43
  • @CodyGray, you just made my day with that edit, I have been looking for so long on how to properly put code block into bulleted lists, without pre. Is there an article or meta post somewhere about this? Commented Jun 17, 2020 at 14:31

You must log in to answer this question.

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