Running C# scripts and snippets in Visual Studio Code with scriptcs

Recently Microsoft open sourced the Code editor and I think a lot of people would agree that it’s really awesome. The UI based on Electron and the Monaco editor previously used in Visual Studio Online is really snappy and works great.

Visual Studio Code can also now be extended via plugins – and when that was announced, I thought why not make something for scriptcs? There already is a scriptcs plugin for Sublime Text and for Atom, so it only makes sense to have one for Visual Studio Code.

Intellisense and C# language services

On top of that, because Visual Studio Code uses Omnisharp to provide C# languages, it also works with scriptcs (yes, yours truly made sure of that :)). Only thing you need is an empty project.json file (and by empty I mean the content should be an empty JSON object: {}) beside your CSX script and Visual Studio Code will give you intellisense, code navigation and other C# experience goodies.

Having an empty project.json will simply force Visual Studio Code to launch Omnisharp, which will detect CSX files and give you language services. This has tremendous value as it is very really productive when you work with C# scripts.

Screenshot 2015-11-25 23.26.46

scriptcs Runner extension

In addition to the aforementioned language services, over the weekend I wrote an extension for Visual Studio Code that allows you to run snippets of C# or entire C# scripts directly from Visual Studio Code.

The extension can be installed directly from VS Code:
– press F1
– type ext install scriptcsRunner
– choose “install”

The prerequisite is to install scriptcs first. The easiest way to install it is to install SVM first, and then just call svm install latest, which will work cross platform.

With the extension in place, you can simply highlight a snippet of C# code and send it be evaluated by scriptcs using the following sequence of actions:
– highlight some code
– press F1 and type execute with scriptcs
– or just press Cmd+Shift+R (or Ctrl+Shift+R on Windows)

This will send the selected snippet of C# code to scriptcs and you will see the output inside Visual Studio Code, in a new output window. You don’t even have to make a selection – if there is no selection made, the extension will simply try to execute the currently open CSX script.

This is still an early version and I have some good ideas about making it better and more advanced – and if you wish to contirbute, all the code is publicly available on Github. It’s written in Typescript and should be really easy to understand.

  • bit bonk

    Very cool, are you planning to add debugging support?

    • Filip W

      I’m gonna look into it, though VS Code atm supports debugging .NET only with mono debugger and I don’t think anyone tried to make it work with scriptcs yet

      • Glenn Block

        I have, and it doesn’t. Miguel and I paired
        up and he thinks it might be a bug in the Evaluator. He said he would have someone look into it.

        Great job Filip!

  • sakowiczm

    Small thing you need to type: ext install scriptcsRunner to install it :)

    • Filip W

      oh you are right, sorry, corrected

  • Filip W

    Thanks yeah you are right about everything – I already updated the installation instructions and will tweak the rest of the info!

  • Jerrie Pelser

    Wow, very cool Filip :)

  • Fagner Carvalho

    Very nice. I was trying to show the output in a new window. Can we do that?

  • hanssens

    Simply awesome! Using it on a daily basis now. Thanks.

  • Pingback: Interactive Coding with C# and F# REPLs (ScriptCS or the Visual Studio Interactive Window) - Scott Hanselman()

  • andyraddatz

    I have the same problem. Win 7 x64, if that matters?

  • Lindsay Fowler

    Didn’t do anything for me. I put an empty project.json file in my folder and Visual Studio code just ignored it totally..
    I have to ask, if Visual Studio Code is Microsoft and C# is Microsoft, why do we have to go through all this to make a C# project work in VS Code? ¯_(ツ)_/¯

  • Andrew Stanton

    Doesnt work for me either. No output, no problems, no debug, nothing.

  • Matt West

    Didn’t do anything for me. output window is blank.