Tutorial Details
- Plugin: snipMate
- Screencast Length: 6 Minutes
The SnipMate plugin aims to mimic the popular snippets feature in TextMate, which allows you to type a sequence of characters, and then automatically expand to a full block of code or html by pressing Tab. It’s incredibly helpful.
Usage
Once installed, you only need to type the desired snippet trigger, and then press Tab. Keep in mind, though, that the available snippets are limited to the extension of the file you’re working on. For example, JavaScript snippets will not work within an HTML file, and vice versa.
Try visiting your ~.vim/snippets directory for an overview of all the available shortcut keys. You can even create your own!
snippet mysnippet
If the above snippet is placed within the HTML.snippets file, you’ll then be able to create a bla element by typing the shortcut, mysnippet.
Note: When creating new snippets, you’ll need to either restart Vim, or open a new window before testing/

SnipMate was definitely one of the essential plugins for me when moving from TextMate.
Does anyone know of a way to configure the plugin to load snippets for a different filetype? eg. I have a bunch of CSS snippets which work in .css files, but I’d like them to also work in my .less files.
Hey Dan – You can use
:set ft=css.lesswithin your Less file, so that everything within thesnippets/css.snippetsfile will be available.Awesome! Works a treat, cheers.
I ran into a similar problem – I wanted ragel files to be linked up to my C snippets. The problem with the solution suggested by Jeffrey Way (i.e., for my situation) was that when I changed the filetype to c.ragel my auto-indent changed. Thus, I searched the interwebs and found a solution at http://stackoverflow.com/questions/6916250/can-we-use-multiple-snippet-files-for-given-filetype-in-vim/6917150#6917150. Basically:
call ExtractSnipsFile(g:snippets_dir . ‘c.snippets’, ‘ragel’)
And voila, it worked! Hope this helps.
Hi, I have created some useful Vim commands for editing snippets:
https://github.com/epeli/vimconfig/blob/668bb691e4f2e039ef5fa4b929b0595263b4c9e0/vimrc#L361
They allow one to easily edit snippets and not worry about reloading them.
You don’t have to restart vim. Now I use snipmate plugin from https://github.com/garbas/vim-snipmate which updates snippets upon save, but in earlier version of the plugin from https://github.com/msanders/snipmate.vim you could do :call ReloadAllSnippets() or :call ReloadSnippet(filetype) to test your changes or new snippets.
It’s good to know that when creating snippets indenting is important. There are docs available with the plugin.
Is there snippets for latest jQuery?
This piece of code reloads the snippets without the need to restart vim:
fun! ReloadSnippets(snippets_dir, ft)
call ResetSnippets()
call GetSnippets(a:snippets_dir, a:ft)
endfun
map ,n :call ReloadSnippets(snippets_dir, &ft)
Then just map some key to trigger the function (I mapped ,n in that case) and you’re done.
One drawback of the SnipMate plugin is that it doesn’t support recursive snippets.. so if you’re in the last placeholder of the last typed snippet, a new snippet won’t work there… and that’s a bit annoying.
Unfortunately the last version of SnipMate dates back to 2009-07-13 and the plugin’s development is stalled. But that’s still the best option we have.
Great Tutorial As Usual..
But How did you maximize vim with keyboard shortcut?? or is it editing in play?? @ 1:55
Thanks.
recommend updating this post to link to the shiny new well maintained snipMate git source:
https://github.com/garbas/vim-snipmate
everyone, please use this instead.
also, if you write css go get:
https://github.com/tisho/css-snippets-snipmate/blob/master/css.snippets
css snippets were merged into snipmate…
Great tutorial Jeff!
Two quick suggestions:
1. When setting snippets for anything that has units (eg. 10px) its’ a good idea to set the units outside the selector scope, since we’ll always be changing the number of pixels but never the actual units (px)
2. You can create external selection scopes in case you don’t need every possible attribute.. say you want a class but don’t require an id.
eg. my header
I installed it on ubuntu vim but it doesn’t work when i press tab. HELP PLEASE!!!
You probably don’t have a filetype selected. Either try ‘:set ft=html’ (or whatever filetype you need), or save the file before trying the tab completion.
Please update the git link to use Garbas’ fork as others have suggested. Your link is no longer being updated: https://github.com/garbas/vim-snipmate
I’ve notice this plugin on some of your tutorials and I’ve been looking how you did it.Thanks for this sir!