I have a directory "a"
with a set of templates, for instance
$ ls a
b bcc cc ccdd
I would like to implement a keyboard shortcut in Emacs that will show a buffer with the template names, similar to dired
or buffer-menu
and then be able to select a template name by using arrow keys or mouse. Then insert the selected template into the current buffer at point.
How can this be done?
insert-file
, bound to C-x i
by default, can insert a file into your buffer at point, but it doesn't give you a nice menu. Both helm
and ido
enhance this behaviour.
helm
does not come with Emacs, but it can be installed via MELPA. When helm-mode
is active, insert-file
uses Helm's narrowing features. Once you're in the a
directory, the up and down keys may be used to select a file, and Enter will insert it.
ido
is shipped with Emacs. When ido-mode
is active, C-x i
is rebound to ido-insert-file
. Once you're in the a
directory, the left and right keys may be used to select a file, and Enter will insert it.
Both tools are excellent, both can be used in many other situations, and both offer effective filtering and navigation. Try one or both and use whichever you prefer.
To augment Chris' answer with a little code, here is a small wrapper around ido-insert-file
:
(require 'ido)
(defvar so/template-directory "/tmp/templates"
"Directory where template files are stored")
(defun so/insert-template ()
(interactive)
(let ((default-directory so/template-directory))
(ido-insert-file)))
This allows you to run (or bind a key to) so/insert-template
no matter what directory you are currently in. Obviously set so/template-directory
to your preferred directory.