I'm using .org files to keep track of my system configuration across different machines and I want certain code blocks to be tangled only if certain conditions are met.

Let's say I have something like this in my .init.el

(defvar is-desktop (equal (system-name) "desktop"))
(defvar is-laptop (equal (system-name) "laptop"))

How would I set a specific src block to be tangled only (when is-desktop)?

Something like this:

#+begin_src shell :tangle ~/.shells/exports.sh

export EDITOR="emacsclient"


#+begin_src conf :tangle (when is-desktop)"~/.gnupg/gpg-agent.conf")

  default-cache-ttl 172800
  max-cache-ttl 282800
  pinentry-program /home/user/.stumpwm.d/modules/util/pinentry/stumpwm-pinentry


When org-babel-tangle the first block will be 'tangled' but the second will not.

Is there a way to do that?

  • 1
    According to the answer here, you can just use elisp directly in the source block its header. So, adapting from that answer, you would get :tangle (when is-desktop ~/.shells/exports.sh) Aug 23, 2023 at 10:26
  • What happens if is-desktop is nil?
    – NickD
    Aug 23, 2023 at 12:05
  • 1
    Assuming that is-desktop is defined, then :tangle (if is-desktop "~/.shells/exports.sh" "no") should work: the expression has to return a string in all cases (in this case, either the pathname or "no").
    – NickD
    Aug 23, 2023 at 12:06
  • The answer under the link mentions that if the expression returns nil, then the block does not get tangled. As the answer got 12 upvotes, I assumed it was correct. I did not test it, therefore I wrote 'according to the answer...'. Aug 24, 2023 at 7:56
  • Using :tangle (when (string= (system-name) "desktop") "yes) works when t, but when nil it will fail to tangle all blocks. Is there a way to just not tangle a single block? Aug 27, 2023 at 4:56


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.