auto-insert-alist by default matches files created in .../bin/ directories and enables sh-mode.

There are examples on the wiki showing how to use a skeleton to insert some text, but I want to both insert some text and enable sh-mode. Some other examples show using a vector to run multiple actions, but not with a skeleton.

I'm unsure of the syntax. This doesn't work:

(auto-insert-mode +1)
(setq-default auto-insert-query nil)

  '("/bin/.*[^/]\\'" . "Shell-Script")
  ['("Default shell script: "
    "#!/bin/bash" \n \n
    > _)

Can you show the correct way to use vectors and skeletons?

1 Answer 1


This works for me:

  '("/bin/.*[^/]\\'" . "Shell-Script")
  [(lambda () (sh-mode))
   ("Default shell script: "
    > _)])

There are two issues here.

  1. auto-insert can call functions as a part of the action, but for some reason it has to be a lambda form. (I wonder why it checks for lambda instead of calling functionp...)
  2. The [...] format for creating vectors does not evaluate its contents. (See section Vectors of the Emacs Lisp Manual.)

The second issue means that these two expressions have the same value:

(vector (lambda () (sh-mode))
        '("Default shell script: "
          \> _))

[(lambda () (sh-mode))
   ("Default shell script: "
    > _)]

Note that in the second expression vector elements are not quoted.

In addition to this, one needs to be careful when using \n at the end of a skeleton, so I added newlines to the string.

The documentation for skeleton-insert states:

Note that \n as the last element of the skeleton only inserts a
newline if not at eol.  If you want to unconditionally insert a newline
at the end of the skeleton, use "\n" instead.  Likewise with \n
as the first element when at bol.
  • Very well explained. Extremely helpful.
    – Skeletor
    Nov 15, 2014 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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