2

I start my python files (.py) with

#!/usr/bin/env python3

and sometimes

if __name__ == '__main__':
    somefunc()

having done it some hundreds of time manually I decided to automate it in Emacs.

(eval-after-load 'autoinsert
  '(define-auto-insert
     '("\\.\\py\\'" . "python skeleton")
     '(""
       "#!/usr/bin/env python3" \n
       "# ")))

I thought that putting the above snippet in the .emacs file would be enough. A new .py file is still empty.

I temporarily removed all Elpy related setups from my .emacs file having proved (I guess) that Elpy doesn't contribute to the problem.

Using Emacs 24.5.1, Python 3.5.3, Debian 4.9.130-2, Elpy 1.28.0

1 Answers1

2

Run the command auto-insert with Meta+x auto-insert. This of course defeats the purpose of "auto-inserting". What you want to do is enable auto-insert-mode, in addition to defining your new auto-inserts. You can do this by adding (auto-insert-mode 1) to your init file. (I'd put it just above your template)

(auto-insert-mode 1)
(eval-after-load 'autoinsert
  '(define-auto-insert
     '("\\.\\py\\'" . "python skeleton")
     '(""
       "#!/usr/bin/env python3" \n
       "# ")))

You can view the documentation for auto-insert-mode with Ctrl+h v auto-insert-mode.

auto-insert-mode is an interactive autoloaded compiled Lisp function
in ‘autoinsert.el’.

(auto-insert-mode &optional ARG)

Toggle Auto-insert mode, a global minor mode.
With a prefix argument ARG, enable Auto-insert mode if ARG is
positive, and disable it otherwise.  If called from Lisp, enable
the mode if ARG is omitted or nil.

When Auto-insert mode is enabled, when new files are created you can
insert a template for the file depending on the mode of the buffer.

[back]

Also, the Emacs Wiki as a has a good page of inspiration on auto-insert-mode.

nega
  • 3,091
  • 15
  • 21