Re: [code] Silly little templating snippets system

From: Mitchell <>
Date: Mon, 27 Jun 2016 21:46:42 -0400 (EDT)


On Sun, 10 Jan 2016, Michael Richter wrote:

> I've come up with a really stupid little templating-based snippets system
> for Textadept. It works like this:
> - Put all the templates into _USERHOME .. '/templates/'
> - The files should be named according to this pattern:
> "<language>.<snippets key>". So for example if you want "mod<tab>": to
> expand into a full Rexx module, you'd put the code for the Rexx module into
> the file "rexx.mod". The boilerplate for a Prolog predicate might be found
> in "prolog.pred" to let "pred<tab>" expand into the stuff you want at the
> head of all your predicates.
> - Inside those files you put in exactly the same stuff you'd put into
> the snippets string you'd otherwise have put into your init.lua (or
> whatever) file.
> - Inside init.lua add the following code:
> local templates_dir = _USERHOME .. '/templates/'
>> local function load_snippets(language)
>> for template in lfs.dir(templates_dir) do
>> b, _, key = string.find(template, '^' .. language .. '%.(.+)$')
>> if b then
>> file =, 'r')
>> snippet = file:read('a')
>> file:close()
>> snippets[language] = snippets[language] or {}
>> snippets[language][key] = snippet
>> end
>> end
>> end
>> events.connect(events.LEXER_LOADED, load_snippets)
> (As an alternative you could just call load_snippets inside your existing
> LEXER_LOADED event should you have one, which is in fact what I do.)
> Now any snippets you want to add to any language can be easily maintained
> in a separate snippets database.
> Motivation:
> - I like Textadept's snippets a lot (simple, yet powerful), but I really
> don't like constantly fiddling with the Lua code in init.lua. Too many
> chances for totally screwing up my configuration.
> - Some of my "snippets" are pretty damned big. My Rexx module
> "snippet", for example, is 204 lines of error handling boilerplate. When
> this was in my init.lua it was a bear to navigate and find the specific
> snippets I was looking for.
> - It's easier to share snippets that aren't married to your
> idiosyncratic init.lua organization.
> In case anybody else finds matching or overlapping motivations for this
> feature, I thought I'd share it.
> Tested on Textadept 8.5.

Thanks to Michael for the idea. I've committed something similar[1] that
will be in Textadept 9. As an example, if you had the same template
location as mentioned above, you'd add the following to your

   local paths = textadept.snippets._paths
   paths[#paths + 1] = _USERHOME..'/templates'

This implementation does not load snippets into memory, but reads from the
filesystem as needed. It also allows for arbitrary (and optional) file
extensions for syntax highlighting as you're editing your snippet files.



You are subscribed to
To change subscription settings, send an e-mail to
To unsubscribe, send an e-mail to
Received on Mon 27 Jun 2016 - 21:46:42 EDT

This archive was generated by hypermail 2.2.0 : Tue 28 Jun 2016 - 06:26:09 EDT