2
  1. Lists are nice

Because they help bringing order and readability into a post.

  1. They may also be titles

Which helps with quickly skimming answers, too.

  1. However,

    The intendation is also needed for code blocks - but this block here was intendated with CTRL+k, which does not automatically adjust for the need for

    7 spaces before being formatted as code block, i.e. I added 3 more spaces here.
    One may break the need for 7 spaces with a horizontal line
    

but this interrupts the numbered item/ bullet point and it is unclear
whether the code is part of the bullet point or not.
  1. Backtics

do not work as they disregard linebreaks (this block has two of them).

  1. So the question is:

Is there a more elegant way to break out of the intendation for lists when introducing a code block?

I prefer having any code block not intendated despite being part of a list but ensure that it is clear that the code block is part of the current list item.

6
  • Not sure I'm understanding your question: are you asking if there is a way to align a code block to the text in a numbered list item (e.g. the first code block in your point 3) without manually adding spaces at the beginning of each line?
    – fra-san
    Dec 20, 2021 at 10:47
  • @fra-san Quite the opposite: how to not align the code block, like the part in point 3) after the horizontal line. It is not rendered into code if one uses the standard number of 4 spaces without introducing e.g. this horizontal line.
    – FelixJN
    Dec 20, 2021 at 10:48
  • 1
    Sorry, I wasn't reading carefully. This answer on the main Meta seems relating, even though it's old and doesn't deal with how not to break numbering.
    – fra-san
    Dec 20, 2021 at 11:11
  • Oh wow. I want the exact opposite: an easy way of having code blocks properly indented when they are part of a list. If it is not indented, then it will appear as though it is not part of the list. Isn't the current system what you want? Code blocks, unfortunately, do not get the right indentation at the moment without manual tweaking.
    – terdon Mod
    Dec 20, 2021 at 11:18
  • @terdon I just realized the block works if either <!-- --> is used as suggested by fra-san OR if there is prepending, non-intented text. Only if there is no prepenting text or this text is also intented, the code block breaks. But I agree, CRTL+k should be smarter in such cases.
    – FelixJN
    Dec 20, 2021 at 11:24
  • ... Also, adding to my previous comment, the CommonMark specification seems to state that a list is interrupted by non-blank lines that aren't properly indented, meaning that the item counter is reset by design in cases such as the zero-space-indented code block in your question.
    – fra-san
    Dec 20, 2021 at 11:37

2 Answers 2

3

Personally, I do indented code blocks in lists using the triple backtick syntax, and then I use Ctrl+K on them to do the indentation:

  1. So, first write the code block this way:

    * Some list item showing a way of writing a particular loop:
    ```sh
    unset -v err
    for name do
        [ ! -f "$name" ] && err=true
    fi
    "${err-false}" && exit 1
    ```
    ```shell
    $ ./myscript argument
    ```
    
  2. Then use Ctrl+K on the code block:

    * Some list item showing a way of writing a particular loop:
        ```sh
        unset -v err
        for name do
            [ ! -f "$name" ] && err=true
        fi
        "${err-false}" && exit 1
        ```
        ```shell
        $ ./myscript argument
        ```
    
  3. The result:

    • Some list item showing a way of writing a particular loop:
      unset -v err
      for name do
          [ ! -f "$name" ] && err=true
      fi
      "${err-false}" && exit 1
      
      $ ./myscript argument
      

In general, I treat Ctrl+K as "indent by 4 spaces" rather than as "format as code block". Using the triple backtick way of doing the code blocks and Ctrl+K for doing indentation by four spaces seems to do the right thing.

By the way, I formatted the dotted list item at the end there as a sub-item of the 3. by marking the dotted item and the already indented code blocks, and pressing Ctrl+K once.

0

Code blocks not intended in the following cases:

  1. Prepending, non-intended text

This text is not intended and thus

neither is this code block.
It is lead by 4 spaces
  1. A prepending, invisible HTML-comment
This code block is prepended by <!-- -->
and this works with 4 spaces and no previous text.

As taken form the meta-SO reference kindly pointed out by @fra-san.


Code blocks need 7-space indentation, and are intended themselves, in the following cases:

  1. No previous text

    intended code block with
    7 leading spaces
    
  2. Previous text is intended

    4-space intended text

    followed by a 7-space intended
    code block
    

As far as I can see, CTRL+k is not smart when it comes to automatically adjust to the need of 7 spaces for intended code blocks.

1
  • About Ctrl+K: it seems to be meant to work the other way round: first, use Ctrl+K to format a code block; then, select the whole to-be list item, including the code block, and format it with Ctrl+O.
    – fra-san
    Dec 20, 2021 at 11:40

You must log in to answer this question.

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