C# autocompletion in Emacs

Just a quickie. The family are in Cornwall, so I was going to take the opportunity to knock up some kind of C# autocompletion – intellisense is the only thing I miss from my days of working with Visual Studio (though it’s not enough to make me want to embrace the horrors of Monodevelop). I was intending on glueing SublimeText’s CompleteSharp binary into Emacs with a bit of elisp and unicorn dust when I noticed on Reddit that someone had created Omnisharp, which integrates NRefactory, the code analysis and completion backend for Monodevelop, into Vim. Rather splendidly, they’d implemented it via an frontend-agnostic http server, allowing other editors to call to use it.

When I originally checked there were integrations for Vim and SublimeText, so I started working out how to wrangle http POST requests in lisp, but by the end of the week I noticed that someone had beaten me to it and created the rather wonderful Omnisharp-Emacs. Fortune was on my side.

I’ve put in a bit of work integrating it with my preferred completion system, company-mode, and now the full glory of C# completion is mine. Couple of screenies:

autocompletion popup

Here you can see the popup menu with the relevant completions, and more detailed information in the minibuffer.
autocompletion parameters

And here we’ve selected a candidate and company-mode is allowing us to fill in the parameters by tabbing through them and typing in text.

The company-mode integration is sitting in a fork in github, and I’m hoping to get it pulled upstream soon.

Omnisharp has lots of other tricks too, such as showing help documentation, renaming variables, jumping to definitions and lots of other fun stuff. I’m probably going to be using this on a daily basis.

There is a minor downside to all this, which is that NRefactory works in terms of Mono/Visual Studio solution files and their associated project files, and I’ve moved my workflow entirely over to my own, Emacsy, concept of projects which doesn’t require manual registration of files and things like that. I don’t like having to add files manually, but nor do I like having two systems to maintain, so I’m going to have to work out a way of getting them to play nicely together.

Besides that, very minor, wrinkle, I’m a happy man, and I’m hugely grateful to the authors of Omnisharp, Omnisharp-Emacs and company-mode – all three are fantastic.

Which leaves the rest of the weekend free to decide whether to buy an Ergodox keyboard with Cherry Blues or Cherry Browns, and then I can wear my geek badge with pride.


After a busy weekend of github-based collaboration, company-mode is now properly integrated into omnisharp-emacs. Head on over to the github page to have a look at the awesome features we’ve piled in – return type information, documentation buffers, tabbing through method parameters, smart completion detection, it’s all there.

8 thoughts on “C# autocompletion in Emacs

  1. Paul New

    Cherry Browns. For the sake of anyone sharing a house/office with you, Cherry Browns!

    Also, do you have any kind of fancy Emacs-y/vim-y solution for Unity script debugging? I didn’t think that anything could make me nostalgic for text mode gdb until I started messing about with Unity.

    1. bbbscarter Post author

      Oddly, it turned out that they didn’t offer Browns. I was curious to try Blues anyway so I’ve ordered them for now, and a pack of o-rings if they really are too noisy. Ultimately though, if I don’t like Blues, I’ll buy some Browns or Clears separately and get out the soldering iron. That’s the beauty of an open-source keyboard, I guess.

      Regarding Unity/Emacs and debugging, I’ve taken the slightly controversial route of not ever using the debugger. It’s not as weird as it sounds – Debug.Log() gives a complete callstack trace, and debug mode in the inspector shows up all your private variables. Between the two, I’ve not needed to use the debugger in 2 years but, if I do, I’ll just fire up Monodevelop, wince, and jab keys.

      People at work are using UnityVS, but they are neither leet nor extreme.

  2. Jason Imison (@JasonImison)

    Thanks for the kind words. I’m the author of OmniSharp and the Vim plugin. I’ve been playing around with emacs for the last couple of days for the first time just so that I could try the plugin out! I’m actually quite a bit envious of how much nicer the code looks in elisp than in the awful vimscript / python mash up.

    It would actually be a pretty easy change to make the OmniSharp server work without csproj files (I think)… somebody else requested it here https://github.com/nosami/Omnisharp/issues/66

    1. bbbscarter Post author

      Hi Jason – thanks for the awesome work that started all this! I haven’t been this girlishly excited about a project in a while.

      I think the SublimeText integration wins for sheer readability, but then again elisp still looks like an explosion in a staple factory to me.

      I’m not quite sure how I’m going to sort out my project integration yet, but having the facility to not require a solution or csproj would be immensely useful, and allows lots of flexibility.

      Thanks again!

  3. Pingback: Compiling Unity projects from within Emacs | Nerdgasm

  4. Hi-Angel

    It would be great if you shared your «.emacs» file. E.g. I am trying to get work autocompetition of company, and I am hardly found that I was needed to add «company-omnisharp» to a backends. And now it is impossibly lags (it freezes for a few seconds after an every typed character).
    I guess I am just doing something wrong and probably I am not the first one. If you share your working configuration, it would be much easier for a readers to see how the configuration should be done.

    1. Hi-Angel

      I am found how the code should look like. It is
      (add-hook ‘csharp-mode-hook ‘(lambda () (interactive)
      (set (make-local-variable ‘company-backends)
      About the lags — that is a problem with flycheck, I hope I will find the reason later. For the present moment I just disabled it.

      1. bbbscarter Post author

        Yup, that’s pretty much the setup I use. I’ve not had the flycheck problem you mention, although recently I had some problems with lagginess which I tracked down via (profiler-start) and (profiler-report) to garbage collection – (setq gc-cons-threshold 80000000) fixed it for me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s