I use auctex to edit my .tex files. Each time I create a new file .tex file via C-x C-f, I want to insert two local variables informations:

% Local Variables:
% coding: utf-8-unix
% TeX-engine: luatex
% End:

Until now, I do M-x add-file-local-variable two times. Is there a way to automate this?

  • First question here. Hope the formating is ok. Thanks :)
    – cjorssen
    Sep 16, 2016 at 9:07
  • Do you prefer setting this as file local variables or setting a global default value?
    – giordano
    Sep 16, 2016 at 11:09
  • 1
    @giordano On my computer, those settings are default settings. But I want to keep those settings in my files, in case I work on an other computer. (If I understand correctly your question)
    – cjorssen
    Sep 16, 2016 at 11:15
  • Yes, that was my question, and your answer is perfectly fine :-).
    – giordano
    Sep 16, 2016 at 11:20

1 Answer 1


You can use a hook to achieve this. Use the one you need as described in auctex documentation. Here we check the file extension so that we don't do that for eg .sty files.  Also, we test the buffer-read-only variable before modifying the buffer.

The goto-char at the end ensures point is set at beginning of buffer.

(defun my/add-auctex-file-variables ()
  (if (and (not buffer-read-only)
           (string= (file-name-extension (buffer-file-name)) "tex"))
     (add-file-local-variable 'coding 'utf-8-unix)
     (add-file-local-variable 'TeX-engine 'luatex)
     (goto-char (point-min)))))

(add-hook 'LaTeX-mode-hook 'my/add-auctex-file-variables)

Also, as can be seen on add-file-local-variable docstring (display it with C-h f add-file-local-variable) any setting of the variable is first deleted, so there's no risk of accumulating such definitions.

A possible pitfall is that the file is marked as modified even if nothing has actually changed.

  • @cjorssen Sure, just add (goto-char 1) at end of my/... function (before the closing )).
    – JeanPierre
    Sep 16, 2016 at 12:06
  • @cjorssen The docstring of add-file-local-variable (use C-h f add-file-local-variable) says any setting of the variable is first deleted, if that's what you ask.
    – JeanPierre
    Sep 16, 2016 at 12:10
  • @cjorssen Yes, see updated answer, as well as pitfall.
    – JeanPierre
    Sep 16, 2016 at 12:34
  • The first character of a buffer is (point-min), not necessarily 1.
    – giordano
    Sep 18, 2016 at 17:31
  • @giordano Good point.
    – JeanPierre
    Sep 18, 2016 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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