As great an IDE Visual Studio is; I’ve never been able to be as comfortable in it as I am when coding in emacs. Over the years I’ve made various attempts to code C# from Emacs and make Visual Studio as comfortable to use as Emacs, to no avail.

Finally Emacs has a mode that supports C# well enough for me to give it a go again. Let’s open some minds by slinging some C# goodness from emacs.

Mono

Now I’m running artegos an archlinux derivative on my personal computer, so I need to setup mono.

$ sudo pacman -S mono

Unfortortunately the dotnet cli project is still in the very early stages of development and has yet to be ported support archlinux.

Omnisharp-Server

First of all we need to install OmniSharp-Server. This will run in the background, providing us with that VS goodness that has been missing all these years.

$ git clone [email protected]:OmniSharp/omnisharp-server.git ~/.omnisharp
$ cd ~/.omnisharp/
$ git submodule update --init --recursive
$ xbuild

Now OmniSharp-Server is in process of being ported to use roslyn as its backend but again at the time of writing it’s still to early to use on archlinux.

OmniSharp-Emacs

Finally let’s setup emacs, let’s open up our ~/.emacs.d/init.el. My init.el is based off technomancy’s emacs-starter-kit. First off we’re going to add OmniSharp to the list of packages that will automatically be install on start.

(require 'package)
(add-to-list 'package-archives
             '("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives
             '("marmalade" . "https://marmalade-repo.org/packages/"))

(defvar my-packages '(better-defaults paredit idle-highlight-mode
                                  helm expand-region company
                                  git-gutter company-tern tern
                                  editorconfig markdown-mode
                                  omnisharp))

(package-initialize)
(dolist (p my-packages)
  (when (not (package-installed-p p))
    (package-install p)))

Sometimes emacs will fail to find any packages, I don’t know why this happens but I just run: M-x package-list-packages to remind it of all the packages available in the repo.

Next there is a bug in the emacs version / cc-mode version that I’m using where it fails to include cl-mode but makes use of it.

;; cc-mode bug: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=18845
(require 'cl)

Finally we configure OmniSharp to start when we open up .cs files, where to find the OmniSharp.exe and add the OmniSharp backend to our autocomplete framework (company-mode)

;; OmniSharp
(setq omnisharp-server-executable-path "~/.omnisharp/OmniSharp/bin/Debug/OmniSharp.exe")
(eval-after-load 'company
  '(add-to-list 'company-backends 'company-omnisharp))
(add-hook 'csharp-mode-hook 'omnisharp-mode)

And we are done! For now, time to start hacking C# in emacs.

We still need to come up with some keybindings for all the OmniSharp goodness but I’ll leave that for another post (after I’ve had some time to figure what works for me).