
This is the fourth post in the article series "Vim Plugins You Should Know About". This time I am going to introduce you to a plugin called "snipmate.vim".
If you are intrigued by this topic, I suggest that you subscribe to my posts! For the introduction and first post in this article series, follow this link - Vim Plugins You Should Know About, Part I: surround.vim.
Snipmate.vim is probably the best snippets plugin for vim. A snippet is a piece of often-typed text or programming construct that you can insert into your document by using a trigger followed by a <tab>. It was written by Michael Sanders. He says he modeled this plugin after TextMate's snippets.
Here is an example usage of snipmate.vim. If you are a C programmer, then one of the most often used forms of a loop is "for (i=0; i<n; i++) { ... }". Without snippets you'd have to type this out every time. Even though it takes just another second, these seconds can add to minutes throughout the day and minutes can add to hours over longer periods of time. Why waste your time this way? With snippets you can type just "for<tab>" and snipmate will insert this whole construct in your source code automatically! If "i" or "n" weren't the variable you wanted to use, you can now use <tab> and <shift-tab> to jump to next/previous item in the loop and rename them!
Michael also created an introduction video for his plugin where he demonstrates how to use it. Check it out:
How to install snipmate.vim?
To get the latest version:
- 1. Download snipmate.zip.
- 2. Extract snipmate.zip to ~/.vim (on Unix/Linux) or ~\vimfiles (on Windows).
- 3. Run :helptags ~/.vim/doc (on Unix/Linux) or :helptags ~/vimfiles/doc (on Windows) to rebuild the tags file (so that you can read :help snipmate.)
- 4. Restart Vim.
The plugin comes with predefined snippets for more than a dozen languages (C, C++, HTML, Java, JavaScript, Objective C, Perl, PHP, Python, Ruby, Tcl, Shell, HTML, Mako templates, LaTeX, VimScript). Be sure to check out the snippet files in the "snippets" directory under your ~/.vim or ~\vimfiles directory.
If you need to define your own snippets (which you most likely will need), create a new file named "language-foo.snippets" in the "snippets" directory. For example, to define your own snippets for C language, you'd create a file called "c-foo.snippets" and place snippets in it.
To learn about snipmate snippet syntax, type ":help snipmate" and locate the syntax section in the help file.
Have Fun!
Have fun with this time saving plugin!


Hacker Newsletter - a weekly newsletter of the best articles on startups, programming, and more. All links are curated by hand from Hacker News.
Twitter
Facebook
Plurk
more
GitHub
LinkedIn
FriendFeed
Google Plus
Amazon wish list
Comments
Very cool!
Oh, what would I do without my snippets :)
It looks very nice, but I do not like it at all because if you use too much of plugins or use plugins so big as this one, then you have no Vim anymore. You will get new editor which is not present anywhere except your own computer. It also encourages not to learn your editor but create your own. And that smells like emacs. Well, of course that is just my point of view.
Oh and thank you for your great blogpost :-)
"And that smells like emacs."
And it smells good. :)
http://www.youtube.com/watch?v=vOj7btx3ATg
Wow, that is truly amazing dude. Well done!
This is great thanks. What do you do if you just want a tab after a word which happens to be a keyword?
This is an upgrade for my simple abbrevs.
:ab for for(i=0; i}
Did you test the excellent snippetsEmu? How do the two compare?
The main difference is that snipmate is more robust when you are within a snippet, whereas snippetsEMU requires you to tab through the snippet before you can edit it. In other words, snippetsEMU gets confused if you enter and exit insert mode while processing a snippet. Also, the built-in completions for snipmate are simpler and the menu that allows you to choose multiple options for a given completion is nice so you don't have to remember a list of prefix words. Otherwise, they are very similar.
I would also like to know how snipmate compares to SnippetsEMU, as a dedicated SnippetsEMU user for well over a year now!
Snippets are indispensable to me at this point.
dum8d0g, yes, but you want to be effective. It's you who's working at the computer and you want to be fast as a japanese bullet train!
(the one who blogs), nice presentation. I like how it replaces the guards in header file.
Paul Oyster, I did not test the excellent snippetsEmu. I made my way from NERDsnippets to snipMate. I'll try it out and write an article snippetsEmu vs. snipMate.
Aaron, me too. No snippets == dead.
which colorschem are you using ?!
i use the default color scheme too!
fouad, I use the default color scheme.
great!! ..I've been using a similar setup I've written years ago, but it is not as nearly as complete as this, though faster to work with :)
will be patching this up and possibly releasing a mod :)
thanx!!! It's very cool
Very cool plugin.
By the way, as PHP files often mix HTML and PHP code, what would be the best way to include PHP snippets AND HTML snippets ?
i have same problem too. How to mix PHP, HTML, and JavaScript snippets ?
Add the following line to your .vimrc file toget php and html snippets:
au BufRead,BufNewFile *.php set filetype=php.html
Offtopic: Wich color scheme are you using in the screencast ?
PA, that is a good question. I don't have an answer at the moment.
benoror, that's not my screencast, so I don't know that either. Try to find that theme at vim color scheme project!
I use these
NERD Tree ( screencast )
NERD Commenter
The merits of Vim aside, dum8d0g makes a point worth considering with respect to *all* software. Too much customisation.
How much is "too much"?
Consider if you were by chance left without all your customisations, and you had to get things done. How long would it take you to get back up to speed?
Learning to work with default configurations and "base" software, and to use the simplest means possible to accomplish tasks can be a beneficial practice. Among other things, it makes recovery easier, reduces fear of data loss and encourages *your* portability.
We have all perhaps at some time become enthused with the potentials of customisation. Indeed it is a channel for one's creativity toward improving efficiency. But... do you agree that:
"Limitation breeds creativity"
For reasons I will not explain, not every computer will have Vim, perl, xargs, bash, etc. *But*... most all of them will have a very "old" line or stream editor and a very "old" shell. These programs are always present, decade after decade, for reasons I will not explain.
Personally, for text clips, I prefer the ::multiple copy-paste buffer:: of tmux (a GNU screen alternative). It is small, simple stable (more so than screen, in my experience), and it compiles easily. It allows, with some creativity, clips to be copied, stored, loaded and pasted across *all* applications, whether to or from the console or X windows. And of course one can set key bindings, just like Vim or screen.
I was at a seminar by Ken Sipe. One (actually many things) thing really made sense to me. For whatever technology you are using, your level of knowledge should be one level deeper. So, in this case, if you are using snippets, you should know vim, *well*. So, portability is not really a concern because the customization is an enhancement to your knowledge.
Ok, now. I'm trying to use it. But it conflicts with autocompletion. What now, should I remap the snip function to an "incompatible" key combo? Maybe I will try this way.
Neato! :)
Maybe Taglist should be the number V?:)
to get this working on you cygwin install do this:
1. start cygwin (cygwin.bat or something to that effect)
2. create a .vim directory in your $HOME
2a. create a .vimrc file in your $HOME
3. unzip the snipmate.zip in that such that all the subdirs are under .vim/
4. put the following line in your .vimrc file you created earlier:
5. restart Vim
NOTE: you're doing 2a. to make sure your Vim starts in 'nocompatible' mode as the snipmate plugin won't work in VI compatibility mode which was default in my Cygwin install.
HTH those who are trying this on a cygwin on XP setup.
This blog post is similar to - http://zinas.krabjiem.lv/blogi/vim-plugins-you-should-know-about-part-iv-snipmatevim/130463/ is this some form of plagiarism of your content?
Bart J, thanks for noticing, I just sent them a notice to remove all the articles.
If I modify a snippet file, how do I reload ?
snipmate plugin hasn't been updated for a long long time. thats why i started a friendly fork and i'm merging and coordinating further development of this (at least for me) essential plugin.
more here: https://github.com/garbas/vim-snipmate/blob/master/README.rst
You should also consider Ultisnips, which allows for embedded Python code inside snippets. this means you can dynamically evaluated expressions that are part of snippets.
Leave a new comment