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

Β· 430 words Β· 3 minutes to read

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.

About


Hi! I'm Filip W., a cloud architect from ZΓΌrich πŸ‡¨πŸ‡­. I like Toronto Maple Leafs πŸ‡¨πŸ‡¦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴󠁧󠁒󠁳󠁣󠁴󠁿.

You can find me on Github, on Mastodon and on Bluesky.

My Introduction to Quantum Computing with Q# and QDK book
Microsoft MVP