Insert a common header into source file with Emacs

08 Dec 2008

Sometimes you want every source file to have a common header. Perhaps a copyright notice, perhaps specifying emacs local variables.

The following emacs command does just this that. It uses a end-of-header sentinel. If it's exists, the header is updated. If not, it is added at the beginning of the file

insert-header-core is the main program, and insert-header is your locally customized version. For whatever reason, make sure your headertag ends with a newline \n

(defun insert-header-core (headertag headerfile)
   "insert a source file header"
   (interactive)
   (goto-char (point-min))
   (kill-region (point-min)
                (progn (search-forward headertag nil t 1)
                       (point)))
   (insert-file headerfile)
)

(defun insert-header ()
  (interactive)
  (insert-header-core "/* HEADER END */\n" "header.txt")
)