C# and Sublime Text 2 PROTIPS

Given how powerful, fast and easy Sublime Text 2 is I find myself editing a great deal of my C# & XAML code in it and then switching to Visual Studio to launch the application.  Along those lines I’ve picked up a few C# Sublime tips.

1. Update Your C# Bundle

The syntax and snippet bundle that comes with Sublime is a bit out of date and more importantly it doesn’t correctly identify methods so you are missing a huge chunk of the power of the Ctrl+R “jump to method” hotkey.  Grab an updated bundle over on GitHub.

This is a TextMate bundle so you’ll need to do some slicing and dicing to get it to play nicely with Sublime, most notably move the files in the Syntax and Preferences folders into the root of the C# package.  If you need more details than that just hit me up and I’ll add the step-by-step.

2. Add msbuild as a Build System

I do a build sometimes as a quick and dirty syntax & sanity check and it’s nice to be able to trigger it from Sublime vs. having to swap back to VS.  I whipped this up and dropped it into my /Sublime Text 2/Packages/User folder as “msbuild.sublime-build”.

F7 to compile, F4 to jump to each compiler error (I know, I know, you don’t have bugs but that other coder…).

I’m using a hard-coded msbuild location, that’s stinky and a better solution would be to use environment variables and such but… Works On My Two Machines That Are Sync’d With Dropbox.

If you have a Sublime project open (which is really a collection folders that act as a scope for searches, I resisted them foreva but am now an addict) this will call msbuild at the project root, otherwise it’ll look in the same folder as the file you’re editing.

3. Create Some Snippets

Visual Studio 2010 ships with some awesome and must-have C# snippets.  I haven’t tried to recreate them all in Sublime (I really do like VS you know) but there are some tricks Sublime can do that Visual Studio just can’t.  One nice one is being able to whip up some regex magic on replacements which comes in handy for things like changing the casing between private and public variables.  An example will work better:

Also VS doesn’t support XAML snippets and that about makes me cry every time I have to type yet another angle bracket so go ahead and create some tasty XAML snippets.  I dropped a few into a zip here.

Have any other protips for working with C# or XAML inside of Sublime? I’d love to hear them and if you have any questions about my workflow don’t be shy.

  • ShaneFulmer

    Are you still using Sublime with C#? How has your workflow improved since you’ve started?

    • http://shawnoster.com/ Shawn Oster

      Yup, still using it and loving it. I use it in conjunction with Visual Studio and find myself moving back and forth between the two constantly. I find the snippet system in Sublime to be easier to work with and create snippets on the fly. Also certain refactorings are faster with Sublime than VS, especially when working with XAML.

      • ShaneFulmer

        Awesome - that’s the road I’m going down. Have you found any easy way to add files to csprojs, other than just editing the XML? I’m also loving the snippet system so far!

  • http://www.facebook.com/myxaxaxa Алексей Макаров

    How do you set definition finder in Sublime or autocomplite?

  • http://twitter.com/Raylinth Alexander Neng

    How do i get the TM-Bundle Files work with Sublime?

  • http://ajryan.github.com/ Aidan Ryan

    Hi all, anyone doing C# in Sublime Text might be interested in my CSharpreter plugin. You can write a fragment of C# code and the plugin will build and run a console app with your code injected inside. You can install it with Package Control - just search for CSharpreter.

    Also, for those trying to install the C# Textmate bundle — just drop the tmPreferences, tmLanguage, and tmSnippet files from the bundle into your Sublime Text2PackagesC# folder. Replace anything that’s already there.

  • Nobody Important.

    Any alternative to msbuild for linux?

    • http://shawnoster.com/ Shawn Oster

      Wish I knew! If you find something let me know.