daviwil / dotfiles

[MIRROR] The path to GNUrvana

Home Page:https://config.daviwil.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Indenting Org Babel's Source Blocks

beetleb opened this issue · comments

I've noticed in a lot of your videos you copy/paste code into an Emacs lisp source block and then manually fix the indent.

This is Emacs lisp - it's trivial to autoindent it! Below is a function that will do this for you:

(defun mn:indent-src-block ()
  (interactive)
  (org-edit-special)
  (indent-region (point-min) (point-max))
  (org-edit-src-exit))

Simply invoke it while the point is in a source block.

Thanks! I'll give it a shot

indent-region is already bound by default to C-M-\. The problem with just pressing that command at an org source code block without a region is that, ironically, sometimes it breaks the indentation of the block delimiters (#+end_src). A way around that is to bind C-M-\to org-indent-region when inside org (I don't know why it isn't by default). This way you can just press that command and it will do the right thing (at least in my tests, it does).

Still, I've got used to either use M-h to org-mark-element (or mark-paragraph elsewhere) and follow that with C-M-\ or, more often, just do a few C-M-u to backward-up-list until I reach the scope that needs indentation (usually one or two times) and then invoke C-M-\. I like that it is one workflow that fits every scenario, including editing source code blocks, and doesn't require any extra configuration.