I am generating source code files from an org-mode file using org-babel-tangle.

How do I add headers to the files e.g. for python

# This is a generated file do not edit

for emacs lisp similar plus a header for lexical binding.

These would be different headers to a file I created directly in emacs so I think autoinsert.el won't have the flexibility.

2 Answers 2


You could use something this:

(defun add-tangle-headers ()
  (message "running in %s" (buffer-file-name))
   ((f-ext? (buffer-file-name) "py")
    (goto-char (point-min))
    (insert "# This is a generated file do not edit\n"))
   ((f-ext? (buffer-file-name) "el")
    (goto-char (point-min))
    (insert ";;; -*- lexical-binding: t -*-\n"))

(add-hook 'org-babel-post-tangle-hook 'add-tangle-headers)

The idea is to insert the desired line depending on the kind of src file you are tangling.

I would have thought you could use a :prologue header arg, but this seems to be only for execution.

Alternatively, you might try noweb:

First, name some source blocks like this with the header you want in each one.

#+name: elisp-header
#+BEGIN_SRC emacs-lisp
;;; -*- lexical-binding: t -*-


#+name: python-header
#+BEGIN_SRC python
# This is a generated file do not edit


Then, add a noweb yes header arg, and the target in the blocks where you want them.

#+BEGIN_SRC emacs-lisp :tangle test.el :noweb yes
(message "ok")

#+BEGIN_SRC python :tangle test.py :noweb yes

These will tangle as you want.

#+BEGIN_SRC python
# This is a generated file do not edit

or for lexical binding in an elisp file

#+BEGIN_SRC emacs-lisp
;;; -*- lexical-binding: t -*-

You will of course want to make sure that's the very first BEGIN_SRC block in the file.

  • 1
    I am looking for way of saying that anew file does this rather than hard coding it
    – mmmmmm
    Feb 23, 2020 at 17:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.