dotfiles icon indicating copy to clipboard operation
dotfiles copied to clipboard

Indenting Org Babel's Source Blocks

Open beetleb opened this issue 4 years ago • 2 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.

beetleb avatar Feb 17 '21 06:02 beetleb

Thanks! I'll give it a shot

daviwil avatar Feb 17 '21 17:02 daviwil

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.

barrosfelipe avatar Jun 21 '21 21:06 barrosfelipe