Is there a way to disable indentation for one specific org-mode src block at a time?

470 Views Asked by At

I'm using most of the standard features of org-mode that come with the spacemacs develop branch, but I haven't been able to find a way to disable the automatic indentation for source code blocks on a case by case basis. I use tangle and I'm writing Dockerfile's in the same file that I'm writing groovy code or javascript for example. The Dockerfile's are the only ones I want not to be indented so I can get syntax highlighting. Here's what it looks like without the indentation:

![Unindented Dockerfile

And here's what it looks like with the indentation that automatically happens if I edit the text:

Indented Dockerfile

The automatic indentation is fine for groovy for example, so I have no issue with the automatic indentation here. (in fact, if I still got the syntax highlighting for Dockerfile's I probably wouldn't mind too much except the weird word wrapping not respecting the background face). Here's the example with groovy:

Groovy Source

As you can see, I tried a :noindent property I found in the org-mode docs that's usually in a #+STARTUP directive. I also searched stack overflow, but I didn't find anything fruitful that didn't disable indenting for all source blocks or for the entire file.

1

There are 1 best solutions below

0
c4710n On

In my practice (I'm not sure it is a good practice or not, just share it to you):

  • I setup org-src-tab-acts-natively to true for using the language’s major-mode indentation. In your case, it will format the src block with the formatting rules of dockerfile-mode.
  • I created a new major-mode plain-mode for the content which should not be indented automatically. Personally, I use this mode for something like:
    • shell outputs
    • ASCII diagrams
    • ...
#+begin_src plain
this will not
  be 
    auto
        indented
#+end_src

Above src block will not be indented when you select them as region, and TAB on them.

In this way, the src blocks will always behavior as expected when formatting them.


Aside, the example code of plain-mode:

(define-derived-mode plain-mode
  clean-mode "Plain"
  "Major mode for plain text."
  ;; preserve the auto indentation on line
  (setq-local indent-line-function 'indent-relative)
  ;; disable auto indentation on region
  (setq-local indent-region-function (lambda (start end))))