Working with scriptcs in Atom on Windows

Atom is a text editor from the folks at GitHub. I’ve been intrigued, but up until now it has only been available on the OS X platform. And since I currently don’t have a Mac I haven’t yet taken it for a spin. But all that has changed … Atom is now available for Windows.

Atom - text editor

Installing Atom

Installing Atom on Windows is really easy. It is available as a Chocolatey package. If you don’t have Chocolatey on your Windows machine, install it as per the instructions on the Chocolatey website.

Then simply run the following command from the command line to install Atom.

 cinst atom 

And you’ll be greeted by your shiny new text editor when launching Atom.

Atom - welcome

Add C# language support

Support for the C# language is not provided out the box with Atom, but this is quickly solved with an Atom package.

Atom comes with the Atom Package Manager which is easily launched by issuing the following command at the command line:

 apm 

apm - Atom package manager

The Atom package manager allows you to install Atom packages that can be used to extend the functionality of Atom. You use the apm install command to install packages. You can get help for any command by using apm help <command> as shown below.

apm help install

We are interested in the language-csharp Atom package. This adds syntax highlighting and snippets for both C# and scriptcs specific grammars.

language-csharp

Install the package by issuing the following command at the command line:

 apm install language-csharp 

apm install language-csharp

Add support for running scriptcs

So now we have C# language support in Atom, but cannot yet run our C# script files using scriptcs. To enable this we require another Atom package – atom-runner. This package allows you to run code or scripts from within Atom.

atom-runner

Install the package by issuing the following command at the command line:

 apm install atom-runner 

apm install atom-runner

We then need to configure atom-runner and associate csx files with scriptcs. This will allow us to execute our csx files from within Atom. We need to add this configuration information to Atom’s config.cson configuration settings file.

The easiest way to open this file is to use Atom’s command palette. Press ctrl-shift-p to bring up the command palette and then type config. Hit enter to open the config.cson file for your user profile.

command palette

Add the following lines to the end of the file.

'runner': 
  'extensions': 
    'csx': 'scriptcs' 

config.cson

See scriptcs in action

Atom has now been configured to provide syntax highlighting and snippets for C# and scriptcs. It is also now capable of executing csx files from within Atom. So let’s see this in action.

Create a csx file and write a simple Console.WriteLine statement. I’ve created a file hello.csx in the C:\Labs folder and added the message “Hello from atom & scriptcs!” to the Console.WriteLine statement. Ensure that the file is saved.

hello.csx

Next bring up the command palette again (ctrl-shift-p) and type runner. Select the Runner: Run item and hit enter. This will invoke the Atom Runner and provide it with the path to the hello.csx file which is the active tab in the editor.

Runner: Run

The csx file will be run by scriptcs and the output captured in the Atom Runner window.

Run scriptcs csx file

Now you can write your scriptcs csx files in Atom with C# syntax highlighting and snippets. You can even execute your csx files from within Atom.

Add keybinding for Atom Runner

Starting the Atom Runner via the command palette just felt like too many keystrokes for me. So I decided to have a look at the keymap functionality within Atom in order to bind a set of keys to the run event of the Atom Runner.

Bring up the command palette again (ctrl-shift-p) and type keymap. Hit enter to open the keymap.cson file for your user profile.

keymap

Add the following lines to the end of the file.

'.platform-win32 .workspace .editor': 
  'ctrl-shift-r': 'runner:run' 

keymap configuration

This will map ctrl-shift-r to the Run event of the Atom Runner on the Windows platform. So this is all you need to use now to execute your csx files.

The Atom Runner has it’s own keymap file (%UserProfile%\.atom\packages\atom-runner\keymaps\atom-runner.cson) that is used by Atom but this is currently OS X specific.

Atom Runner - keymap

Acknowledgements

I’d like to thank Adam Ralph for doing the hard yards and documenting the steps on the scriptcs wiki for how to get this up and running quickly.

If you are interested in how to do the same with PowerShell and obtaining syntax highlighting, snippets and script execution within Atom then have a look at Doug Finke‘s blog post – PowerShell and The Github Atom Editor.