How To Run C# Code Inside Sublime Text

Hey folks, I want to share a tip. Sublime Text is my favorite for quick scripts, text editing and doing other scripting things or anything which doesn't require a whole IDE. I love the speed too much and it is super-fast. In the process, I found that you can run C# scripts too, right from inside the Sublime Text, like you can run JavaScript. This is what I will brief you about:
 
Sometime back, I wrote on configuring Sublime Text for JavaScript too. Here's the link, if you want to learn:
Ok, let's start. The process is simple. You need to have an executable file, which Sublime Text will use to execute the scripts. 
 
For C#, we will use scriptcs. Like NodeJS can run JavaScript, scriptcs runs C#. You can write and execute an Application with only one line of code.
 
Steps to configure Sublime Text for executing C# codes
 
Step 1: Install scriptcs

In order to install scriptcs, you need to have chocolatey. Chocolatey is a package manager for Windows.

 
 
You can install it in two ways:
  1. Using the command prompt (cmd.exe): Run the command, given below in the console:
    1. @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin   
  2. Using Powershell : Run either of the following commands
    1. iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))    
    2. iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex   
    There's a chance you will get an error while running the script due to a Powershell Execution policy. By default, it is set to "Restricted" to prevent the harmful scripts from running. You can check the current execution policy by running "Get-ExecutionPolicy" command in Powershell. 
If you encounter this error, set the execution policy to "RemoteSigned" and run the installation command again. Use the command given below:
  1. Set-ExecutionPolicy RemoteSigned  
 Make sure you start the Powershell console in Administrator mode, otherwise the change will not take place. 
 
After Chocolatey is installed, run the command, given below, in the console to install the scriptcs: 
  1. choco install scriptcs  
Note: If "choco" command is not being recognized by the console and it shows any error, restart the console and run the command again.
 
Step 2: Install Sublime Text Plugin for scriptcs
 
Here, I'm assuming that you already have the Sublime Text installed. Get it from sublimetext.com otherwise. Sublime Text has it's own Package Manager and doesn't come pre-installed with it. Skip this step, if you have already installed Sublime Text Package Manager.
 
To install Sublime Text Package Manager, head over to packagecontrol.io and follow the installation procedure.
 
Start the Package Manager by hitting "CTRL + SHIFT + P"  > Start typing  "install" and it should show "Package Control: Install Package".

Select it and hit Enter.


It will take a couple of seconds and will list all the available packages. Type scriptcs and the package we want will show up on the top. Select it and hit enter. Within the next few seconds, the plugin will be installed.

 
It actually creates a build file for scriptcs, which will be available after the plugin installation under Tools > Build System,


That's all it takes for the configuration.
 
With build system selected as "scriptCs", start writing C# scripts in a new file > save it  and press "CTRL + B", when you want to run them.





You can add many build systems to cater to your needs. In case you want your Sublime Text to run your HTML files in Google Chrome browser, here's another article, that you might want to read:
Hope you enjoyed reading. Please share your valuable feedback in the comments section.


Recommended Free Ebook
Similar Articles