Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

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).

share|improve this answer

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)

share|improve this answer

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.

share|improve this answer

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)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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