Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

(X)Emacs Skeletons and Auto-Insert

by stefan k (Curate)
on Oct 05, 2001 at 14:09 UTC ( #116951=snippet: print w/ replies, xml ) Need Help??

Description: Yes, this ain't perl code, it's elisp, but it is for perl coders, since it shows how to auto-create some often used fragments.
The auto-insert feature come into action whenever you load an empty file for the according mode. It then uses a skeleton. You can define your own skeletons and bind them to keys for several constructs.
1. Auto-Insert
1.1 you need to enable it:
(add-hook 'find-file-hooks 
         '(lambda ()
1.2. Now define the auto-insert template/skeleton (this is only a snippet of my setup which features other languages, too)
(setq auto-insert-copyright (user-full-name))

(setq auto-insert-alist
      ((perl-mode . "Perl Program")
      "#! /usr/bin/perl -w\n\n"
      "# File: " (file-name-nondirectory buffer-file-name) "\n"
      "# Time-stamp: <>\n#\n"
      "# Copyright (C) " (substring (current-time-string) -4)
      " by " auto-insert-copyright "\n#\n"
      "# Author: "(user-full-name) "\n#\n"
      (progn (save-buffer)
        (shell-command (format "chmod +x %s" (buffer-file-name)))
      "# Description:\n# " _ "\n"
2. Skeletons
2.1 you need to define some skeletons:
(define-skeleton ska-skel-perl-sub
  "Insert a perl subroutine with arguments."
  "Subroutine name: "
  "sub " str " {"
  \n "my (" ("Argument name: " "$" str ", ") -2 ") = @_;"
  \n _
  \n "}" '(progn (indent-according-to-mode) nil)

;; any other way to get a time format string?
(require 'time-stamp)
(define-skeleton ska-skel-perl-prog-id
  "Insert perl program identification."
  (insert-char ?# 60) \n
  "#" (insert-char ? 18) "PROGRAMM IDENTIFICATION" (insert-char ? 17)
  (insert-char ?# 60) \n
  "my $program = \"" (file-name-nondirectory
                      (file-name-sans-extension buffer-file-name))
  "my $filedate=\"" (time-stamp-strftime "%y-%m-%d") "\";"\n
  "my $fileversion=\"0.01\";"\n
  "my $copyright = \"Copyright (C) " 
       (substring (current-time-string) -4)
  " by " (user-full-name) "\";"\n
  "my $title = \"$program $fileversion, $filedate - $copyright\";"

(define-skeleton ska-skel-perl-options
  "Insert perl program getopt stuff."
    (if (re-search-backward "^use" (beginning-of-buffer) t)
        (progn (end-of-line)
      (progn (beginning-of-buffer)
                (while (string-match "^#" (char-to-string (char-after)
    (insert "use Getopt::Long;") 
    (insert "\n")
  (insert-char ?# 60) \n
  "#" (insert-char ? 26) "OPTIONS" (insert-char ? 25)
  (insert-char ?# 60) \n
  "my $opt_help = 0;"\n
  "my $opt_version = 0;"\n
  "my $ret = GetOptions("\n
(this last uses some kind of heuristic to place the use-statement, this may be buggy) 2.2 now bind to some keys, e.g.:
  (local-set-key '[(control b) (control s)]  'ska-skel-perl-sub)
  (local-set-key '[(control b) (control i)]  'ska-skel-perl-prog-id)
  (local-set-key '[(control b) (control o)]  'ska-skel-perl-options)
(this assumes that you call it from the cperl-mode-hook (so that the local keymap is the correct one) and that you may use Ctrl-b as a prefix.

3. Documentation
For more info and docs see:

C-h f skeleton-insert
and the files skeleton.el, sh-script.el

Shameless Self Ad: the full code of my newly created xemacs config will be available on, soon. I'm still in the process of testing things, then it's time to update my pages. Don't despair: though the pages are german the code is english. :) I hope this helps.

Regards... Stefan

Comment on (X)Emacs Skeletons and Auto-Insert
Select or Download Code

Back to Snippets Section

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://116951]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2015-12-01 08:06 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (796 votes), past polls