Snippets are code templates that you can have inserted into your code. Its kind of like copying and pasting from a file with a bunch of common code blocks in it, but faster. Visual Studio has support for snippets, but they require bouncing through menus to find what you want, so they're not that convenient. As such, I don't know anyone who uses them. The only snippet Visual Studio does have that I use is the one for summary comments. When you type '///' it expands to a full summary comment block. It even includes your parameter names and everything, which is pretty sweet.
The TextMate editor on the Mac recently brought a lot of attention to the concept of snippets, and so now everyone loves them. Mostly because the way TextMate implemented them was easy to use, and because it came with a lot of good predefined templates for Ruby on Rails.
Not surprisingly, someone wrote a script for Vim that mimics TextMate's snippet support. Follow that link, then follow the instructions and you'll have it all installed.
I then created a cs_snippets.vim file in ...\vimfiles\after\ftplugin to define a few C# snippets. This is what my file looks like now:
if !exists('loaded_snippet') || &cp
finish
endif
" summary comment
Snippet /// ///<summary><CR><{summary}><CR></summary>
" object declaration
Snippet dec <{Type}> <{VarName}> = new <{Type}>();
"foreach
Snippet foreach foreach ( <{Type}> <{Var}> in <{Coll}> )<CR>{<CR><CR>}
"try/catch
Snippet try try<CR>{<CR><{}><CR>}<CR>catch<CR>{<CR>}
finish
endif
" summary comment
Snippet /// ///<summary><CR><{summary}><CR></summary>
" object declaration
Snippet dec <{Type}> <{VarName}> = new <{Type}>();
"foreach
Snippet foreach foreach ( <{Type}> <{Var}> in <{Coll}> )<CR>{<CR><CR>}
"try/catch
Snippet try try<CR>{<CR><{}><CR>}<CR>catch<CR>{<CR>}
With these snippets you can type: ///<Tab> and it will expand to:
///<summary>
///<{summary}>
///</summary>
///<{summary}>
///</summary>
The summary tag in the middle will be highlighted and you can simply type whatever you want to write over it. Then hit tab to commit the value you typed and move to the next tag, if there is one. If you have the same tag name in more than one place in your Snippet all occurrences will be replaced.
Check out :help snippet for more details.
Let me know of any useful snippets you use too.
UPDATE 12/23/2009:
I recently switched from using the SnippetsEmu plugin I wrote about in this post to using SnipMate. The main reason I switched is that SnipMate allows you to define your snippets on more than one line, which makes them much easier to get right and keep up to date. SnipMate also seems to work a little better when you're filling in your snippets. I highly recommend you check it out.
Thanks to Dan Andreescu for sending me an email about an error in the foreach snippet!
ReplyDeleteIt's been corrected in the post.
He also had this to add:
Also, in case people don't know how to source a vimball (I didn't), once you download the script that is referenced here, just go to vim and run
:so %
That will install the snippet script. Again, thank you and keep up the great work. I now have a really good C# development environment using vim