4

How can I configure emacs to generate some code skeleton?

For instance, for C files:

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char **argv) {

    return EXIT_SUCCESS;
}

or something more dynamic in Python:

#!<insert result of `which python` here>

def main():

if __name__ == "__main__":
    main()

I would like to define 2 modes for inserting the templates:

  • create a function insert-template (or something), binding it to a certain key combination (C-c C-t) that would insert the correct template according to the major mode.

  • create an option to automatically insert the template upon file creation, detecting the correct type by the filename suffix (similar to how major mode is detected). This option should be easily set on/off in the .emacs file.

Ideally I'd like to avoid 3rd party modules/packages. But I'm not completely against it.

3

Please check yasnippet. It is a great Emacs template system, easily installed and customizable to what you want to do. Also, check the documentation (with how to use sections and a video demo).

2

Why avoid third party emacs modes if they're there already? The EmacsWiki has a long list of solutions to your problem. Of these, abbrev-mode has been with us for decades. It can be very gently abused to do what you need. auto-insert-mode is even better, and a part of emacs.

I suspect you may have to write a tiny bit of emacs Lisp to search the path for the Python (or whatever else) interpreter binary.

And there are even power powerful options out there (check the EmascWiki page — there's bound to be one that covers all your needs)

1

For automatically inserting these on file creation, I think you'd need to add hooks to the appropriate modes. For example

(add-hook 'python-mode-hook '(lambda () (when (empty-buffer?) (insert-python-template))))

(defun empty-buffer? () (= (buffer-end 1) (buffer-end -1)))

(defun insert-python-template ()
  (interactive)
  (insert "Template stuff goes here"))

Now, whenever you open a new buffer in python-mode, you'll automatically get it populated with that template.

If you're open to third-party modules, and want to add templates/partial templates to an open buffer, check out the yasnippet minor mode. I haven't actually gotten around to using it myself, but I keep hearing good things about it.

0

What about template mode? I have used it at times when I had to create lots of new files.

This doesn't handle your desire to be able to insert functions from a template, but it handles creating new files fairly flexibly.

Check out http://www.emacswiki.org/emacs/TemplatesMode

I just downloaded version 3.1c, and it seemed to work with my emacs (version 22.0.90.1 darwin)

0

You can use the built-in auto-insert-mode. Add the following to your .emacs file:

(auto-insert-mode)
(setq auto-insert-directory "~/.emacs.d/templates/")  ;; trailing slash IMPORTANT
(define-auto-insert "\.py" "python-template.py")

and then put the template content in python-template.py in the directory ~/.emacs.d/templates/.

If you don't want to be prompted for the insertion each time, then you can also add the following line to the .emacs file:

(setq auto-insert-query nil) ;; don't prompt before template insertion

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.