domingo, 13 de mayo de 2012

imap for emacs (insert templates on emacs)

You just miss good old :imap for your temporary configuration of your editor (remember vim?).  When you know you'll be going to insert similar strings over and over.

And you love the way vim allowed you to do crazy stuff inside a map.  But now you're in emacs, so let's see how we can scratch your itch.

My concrete case was inserting strings like (?* ?x) where x is a parameter.

  •  Skeletons. Skeletons can do that and much more. it's a classical template inserter.  For example, you can setup a skeleton of the desired output and interleave str symbol, that will be replaced for the input you'enter.
    Skeleton can do more sophisticated things, like inserting multiple lines while user keeps entering data.  I can imagine myself using this when entering lots of data with some desired format.

  • Yasnippets. Yasnippets are skeletons on steroids. Sad thing is that there's no direct way to bypass the file used to define snippets. I guess you can open a new buffer, put it into yasnippet-mode, and c-c c-c to activate the binding for just this session, but maybe it's too much of a hassle.

  • defun+insert.  The dumb way to do it, but the most straightforward if you tend to forget rarely used functionalities like skeletons.  Just define an interactive function that inserts your shit there, and call it at will.  That's the option I go when I don't wanna spend a minute looking for skeleton docs.
Then to call the functions/skeletons, you have different ways, if you want to global-set-key, or even just set the key in insert mode (using evil), reminding of imap style,

I'd love to hear other solutions on that same issue. Any suggestions?

3 comentarios:

Anónimo dijo...

You can explicitly insert an ad-hoc yasnippet template with (yas/expand-snippet "template contents").

Sacha Chua dijo...

If I'm working with just one repetitive pattern, I might use keyboard macros instead. =) They're easy to set up, and you can use them together with registers for some pretty cool tricks (incrementing counters, reformatting text, etc).

Raimon Grau dijo...

Thanks for passing by and comment!

Probably keyboard macros are the way to go. I rarely use them and feel I'm missing something important.