daviwil / dotfiles

[MIRROR] The path to GNUrvana
https://config.daviwil.com
MIT License
829 stars 177 forks source link

Indenting Org Babel's Source Blocks #6

Open beetleb opened 3 years ago

beetleb commented 3 years ago

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.

daviwil commented 3 years ago

Thanks! I'll give it a shot

sexptherapy commented 3 years ago

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.