How to keep indented under plain list items in org-mode

While regular paragraphs are happily indented under plain list items (using [SPACE] bar to indent the first line takes the rest along with) I have noticed that code blocks do not like to cooperate. Is there any way to fix this?

Right now, this is what I have:

``````- [ ] My plain list item

Nicely indented paragraph, blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah.

#+BEGIN_SRC elisp
; Uncooperative code block -> notice that only the first line indents.
(lambda () (do-something-amazing))
(blah-blah-blah)
#+END_SRC
``````

I would like to get this:

``````- [ ] My plain list item

Nicely indented paragraph, blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah.

#+BEGIN_SRC elisp
; Happily indented code block -> notice that all code follows with :)
(lambda () (do-something-amazing))
(blah-blah-blah)
#+END_SRC
``````
• I'd strongly consider the use of `org-indent-mode`. – wasamasa Aug 5 '16 at 17:54
• Thanks. I am using org-indent-mode. It works great for everything except blocks of source code... I could be doing something wrong... are you able to indent source code with org-indent? – Adam Aug 5 '16 at 19:57
• Let me modify that... source code block indent fine for me, but only under headers, not plain lists... – Adam Aug 5 '16 at 19:59
• `(org-list-in-valid-context-p)` in the src block will tell you that, by default, org doesn't want a src block to be the context where lists are allowed. Altering `org-list-forbidden-blocks` may change that, it's not a `defcustom` though. – mutbuerger Aug 6 '16 at 12:14

Try this

I use the following methods to fix the indentation of src blocks under plain lists. After indentation is setup correctly, always use M-S-left and M-S-right to maintain the list indentation.

Fix Formatting for New Code Blocks

First indent cursor and then use built-in template `<s` when creating a new src block under a plain list.

e.g. tab <s tab `language` return

``````- [ ] My plain list item

Nicely indented paragraph, blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah.

<s
``````

Fix Formatting for Existing Code Blocks

Step 1 - Figure out desired indent column number. For the example below use `2`.

``````- [ ] My plain list item

Nicely indented paragraph, blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah.

#+BEGIN_SRC elisp
; Uncooperative code block -> notice that only the first line indents.
(lambda () (do-something-amazing))
(blah-blah-blah)
#+END_SRC
``````

Step 2 - Place cursor at beginning of `#+END_SRC` line then do M-xindent-to-column `2` return. After which you should see the following:

``````- [ ] My plain list item

Nicely indented paragraph, blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah.

#+BEGIN_SRC elisp
; Uncooperative code block -> notice that only the first line indents.
(lambda () (do-something-amazing))
(blah-blah-blah)
#+END_SRC
``````

Step 3 - Open and close src block by placing cursor inside block and using C-' C-'.

``````- [ ] My plain list item

Nicely indented paragraph, blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah blah blah blah blah blah
blah blah blah.

#+BEGIN_SRC elisp
; Uncooperative code block -> notice that only the first line indents.
(lambda () (do-something-amazing))
(blah-blah-blah)
#+END_SRC
``````

• Alternative to figuring out the indent yourself: move to the beginning of the list (item), type `M-h` to mark the whole item and then `C-M-` to reindent that region. That sorts out the position of the `END_SRC` marker, but then you still have to do your trick for the indenting of the actual code. – Andrew Swann Nov 19 '18 at 7:49