Sunday, October 16, 2011

vim plugins: snipMate vs xptemlate vs UltiSnips

Recently I wanted to add template support to my vim config.

There are at least 3 options: snipMate, xptemlate and last but not least UltiSnips

Before I picked one and use it for rest of vim life (lol), I tried all 3 of them. In this article I will tell how it went.


snipMate

SnipMate was first template plugin (of these three) that I tried. Snippets are in similar format to TexMate snippets. There is huge collection of predefined sippets for many different languages (check here).

New snippets are very easy to write.

However snipMate didn't satisfy my all of my expectations. It looks like of all three, snipMate suffers problems with opening snippet inside another snippet.

A minus for snipMate is that it can't include snippets from other snippet files.

But since it's so easy to use, on score 0 to 10, I'd give it 6

xptemplate

xptemplates is probably most advanced templates engine, however sometimes it felt slower than UltiSnips. Also there was no easy way to disable buildin templates and only use own templates.

Template files (analogue to snippets) are much more complex compared to snipMate and UltiSnips snippet format. Template file format somewhat reassembles


XPT memcpy " memcpy (..., ..., sizeof (...) ... )
memcpy(`$SParg^`dest^,`$SPop^`source^,`$SPop^sizeof(`type^int^)`$SPop^*`$SPop^`count^`$SParg^)


This is really, really ugly syntax if you ask me. all those ` and ^ reminds me of M4 preprocessor.

However I like how xptemplate can dynamically add parts of template when you type (for example in if, else if, else if,... bocks)

I think xptemplate would be fine for people who want to defaults and don't plan to write all templates from scratch (like me).

xptemlate is pretty configurable, however syntax scared me away. It probably has some nice features that I haven't discovered.


On scale 0 to 10, I'd give xptemplate 8.



UltiSnips

UltiSnips is another good snippet engine. snippet files are almost identical to snipMate, and with little work, you could convert snipMate snippets to UltiSnip snippets.

What I really like about UltiSnips is that you can make snippets read output of shell commands, python code, or VimL code. That gives already lots of things you can do.
There are also some nifty support for regexp (however I haven't used it, yet), and it supports callings snippets from snippets quite fine.

I wrote quite a lot snippets in past 48 hours for UltiSnips, and to work them as I want I figured out little hack.
You see UltiSnips have special $0 position, which marks end of last snippet, however, not always you want it at the end of snippet.

Everything is fine with this snippet

snippet ife
if (${1:expression}) {
..${2}
} else {
..$0
}
endsnippet

note: dots are simply to represent spaces, blogspot.com/blogger.com can suck sometimes

However if start ife snippet, and then in ${2} start another ife, you will not be able to get to $0 of inner ife snippet.

As a workaround you can write

snippet ife
if (${1:expression}) {
..${2}
} else {
..${3:$0}
}
endsnippet


This fixes the problem (in most cases)

For my snippets I wrote them like

snippet ife
if (${1:expression}) {
..${2}
} else {
..${3}
}${4:$0}
endsnippet


The advantage is that if I start ife in ife, I can navigate to end of inner ife, and then start new line and write start some other snippet while will being inside outer ife snippet.
It know it probably doesn't make much sense, but if you ever decide to try UltiSnips try my snippets.

Because of simple syntax, very flexible snippets and cool features, I give UltiSnips 9 on scale 0 to 10.

2 comments:

  1. Hi, I am the author of UltiSnips. The issue you describe and work around in your discussion is https://bugs.launchpad.net/ultisnips/+bug/438410 in our bug tracker. It is due to a wrong decision I made early in the development of UltiSnips.

    May I ask what UltiSnips need to get 10/10 in your rating?

    ReplyDelete
  2. Solving recursive snippets would be nice...
    Repeatable snippets....

    But mostly fixing: https://bugs.launchpad.net/ultisnips/+bug/883948

    All in All I totally love UltiSnips... Especially at work I use it a lot when I need to write I18n texts :D (remember: https://bugs.launchpad.net/ultisnips/+bug/876786 ?)


    P.S.
    I noticed that I actually use UltiSnips for complex tasks (like snippet above) and rarely use it for "if"s and such simple snippets :D

    ReplyDelete