views:

786

answers:

7

I write a lot of short throwaway programs, and one of the things I find myself doing repeatedly is typing out code like

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

int main(void){

}

To save some tendon hits I was wondering if it was possible to insert a simple template above whenever I create a buffer with the extension of .c.

+11  A: 

Put somthing like this in .emacs

(define-skeleton c-throwaway
  "Throwaway C skeleton"
  "#include <stdio.h>\n"
  "#include <stdlib.h>\n"
  "\n"
  "int main(void){\n"
  "\n"
  "}\n")

And eval (C-x C-e) it. That'll give you a function (c-throwaway) that inserts your template.

To get this inserting automaticly you'll need to activate auto-insert-mode. Once you do this you can describe-variable auto-mode-alist and read up on how emacs does some of its open file magic. Then define auto-insert-alist to apply it when you find a new file.

Maybe something like this

(define-auto-insert "\\.\\([Cc]\\|cc\\|cpp\\)\\'" 'c-throwaway)

More detail:

Tom Dunham
+2  A: 

The following function will ask for a filename and then insert a file and change to c-mode. The only problem is that you have to call this function to create the buffer instead of your normal way.

(defun defaultCtemplate(cfilename)
    (interactive "sFilename: ")
    (switch-to-buffer (generate-new-buffer cfilename))
    (insert-file-contents "~/Desktop/test.c")
    (c-mode)
)

P.S Thanks for the question, now I know how to do this for myself :)

Andrew Cox
+5  A: 

I use template.el from http://emacs-template.sourceforge.net/

Basically, I create a file called ~/.templates/TEMPLATE.c, and then that gets inserted into my .c files. You can also use special markup and arbitrary lisp expressions, if you don't just want to dump text into the buffer. I use this feature so that Perl modules start with "package Foo::Bar" when they are named lib/Foo/Bar.pm. Very handy.

jrockway
A: 

I use following code to create files from templates. There are several templates, that are substitutes with actual file names, etc

Alex Ott
A: 

Here's how I do it (because I didn't know about auto insert mode :-)

(require 'tempo)

(setq c-new-buffer-template 
      '(
        "#include <stdio.h>\n"
        "#include <stdlib.h>\n"
        "\n"
        "int main(void){\n"
        "\n"
        "}\n"
        ))

(defun my-c-style ()
  "My editing style for .c files."
  (c-mode)
  (if (zerop (buffer-size))
      (tempo-template-c-skeleton)))

(setq auto-mode-alist
      (cons '("\\.c\\'" . my-c-style) auto-mode-alist))

(tempo-define-template "c-skeleton" c-new-buffer-template
            nil
            "Insert a skeleton for a .c document")
Bill White
+1  A: 

You can also use the YASnippet template system for Emacs, which just has a builtin template called main. So while writing your code, just type main, hit TAB, and it will expand it to the form you want. (And you can always write your own snippet templates.)

Török Gábor
I prefer this method, that way i always have a pristine file to begin with, but I can easily fire in a template when needed.YASnippet also integrates nicely with Anything, which is antoher big win for me.
Jonathan Arkell
A: 

I use a combination of Defaultcontent.el and YASnippet.el. The former fills brand-new files with default content. The latter is a sort of lightweight code-gen macro thing. Key in "for" and hit TAB and the skeleton of a for loop is inserted. Etc. You can define your own snippets pretty easily. "swi TAB" gets you a complete switch statement. And so on.

Cheeso