A mode that comes with Emacs that automagically inserts text into new buffers based on file extension or the major mode. See GIT:autoinsert.el or the Info manual Autotyping.
Use it by running ‘M-x auto-insert-mode’
. Or manually insert new text in files with ‘M-x auto-insert’
. Try it with an elisp file, for example. The result is quite impressive: The entire GPL notice will be inserted for you.
A common way to configure what should be inserted for a type of file is by adding a skeleton to the ‘auto-insert-alist’
. The syntax is based on SkeletonMode.
Here is a skeleton for a C++ file that you can try by EvaluatingExpressions or by adding to you InitFile.
(eval-after-load 'autoinsert '(define-auto-insert '("\\.\\(CC?\\|cc\\|cxx\\|cpp\\|c++\\)\\'" . "C++ skeleton") '("Short description: " "/*" \n (file-name-nondirectory (buffer-file-name)) " -- " str \n " */" > \n \n "#include <iostream>" \n \n "using namespace std;" \n \n "main()" \n "{" \n > _ \n "}" > \n)))
Auto Insert will insert the following (where the |
is point).
/*
foo.cc -- Description of Foo.
*/
#include <iostream>
using namespace std;
main()
{
|
}
Here’s another skeleton, but for a C source file that you can try by EvaluatingExpressions or by adding to you InitFile.
(eval-after-load 'autoinsert '(define-auto-insert '("\\.c\\'" . "C skeleton") '( "Short description: " "/**\n * " (file-name-nondirectory (buffer-file-name)) " -- " str \n " *" \n " * Written on " (format-time-string "%A, %e %B %Y.") \n " */" > \n \n "#include <stdio.h>" \n "#include \"" (file-name-sans-extension (file-name-nondirectory (buffer-file-name))) ".h\"" \n \n "int main()" \n "{" > \n > _ \n "}" > \n)))
Auto Insert will insert the following (where the |
is point).
/**
* foo.c -- Description of Foo
*
* Written on Tuesday, 21 August 2012.
*/
#include <stdio.h>
#include "foo.h"
int main()
{
|
}
Here’s a skeleton for a Perl file that you can try by EvaluatingExpressions or by adding to you InitFile.
(eval-after-load 'autoinsert '(define-auto-insert '(perl-mode . "Perl skeleton") '("Description: " "#!/usr/bin/env perl" \n \n "use strict;" \n "use warnings;" \n \n _ \n \n "__END__" "\n\n" "=head1 NAME" "\n\n" str "\n\n" "=head1 SYNOPSIS" "\n\n\n" "=head1 DESCRIPTION" "\n\n\n" "=head1 COPYRIGHT" "\n\n" "Copyright (c) " (substring (current-time-string) -4) " " (getenv "ORGANIZATION") | (progn user-full-name) "\n\n" "This library is free software; you can redistribute it and/or" "\n" "modify it under the same terms as Perl itself." "\n\n" "=cut" "\n")))
Auto Insert will insert the following (where the |
is point).
#!/usr/bin/env perl use strict; use warnings; | __END__ =head1 NAME Description of Foo =head1 SYNOPSIS =head1 DESCRIPTION =head1 COPYRIGHT Copyright (c) <year> <name of author> This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut
Another way of using autoinsert is to define files that contain the stuff to be inserted and connect them with a filename extension:
(auto-insert-mode) ;;; Adds hook to find-files-hook (setq auto-insert-directory "~/.mytemplates/") ;;; Or use custom, *NOTE* Trailing slash important (setq auto-insert-query nil) ;;; If you don't want to be prompted before insertion (define-auto-insert "\.py" "my-python-template.py") (define-auto-insert "\.php" "my-php-template.php")
I use Yasnippet for initial skeletons:
(defun my/autoinsert-yas-expand() "Replace text in yasnippet template." (yas/expand-snippet (buffer-string) (point-min) (point-max)))
(custom-set-variables '(auto-insert 'other) '(auto-insert-directory "~/autoinsert/") '(auto-insert-alist '((("\\.\\([Hh]\\|hh\\|hpp\\)\\'" . "C / C++ header") . ["template.h" c++-mode my/autoinsert-yas-expand]) (("\\.\\([C]\\|cc\\|cpp\\)\\'" . "C++ source") . ["template.cc" my/autoinsert-yas-expand]) (("\\.sh\\'" . "Shell script") . ["template.sh" my/autoinsert-yas-expand]) (("\\.el\\'" . "Emacs Lisp") . ["template.el" my/autoinsert-yas-expand]) (("\\.pl\\'" . "Perl script") . ["template.pl" my/autoinsert-yas-expand]) (("\\.pm\\'" . "Perl module") . ["template.pm" my/autoinsert-yas-expand]) (("\\.py\\'" . "Python script") . ["template.py" my/autoinsert-yas-expand]) (("[mM]akefile\\'" . "Makefile") . ["Makefile" my/autoinsert-yas-expand]) (("\\.tex\\'" . "TeX/LaTeX") . ["template.tex" my/autoinsert-yas-expand]))))