Category Archives: Emacs

Compiling Unity projects from within Emacs

I’m often asked how I get Emacs to compile up Unity projects*, and I thought it would be worthwhile writing a proper post on what I’ve done to make this a relatively painless process.

Once you’ve followed this post you should get syntax highlighting, in-Emacs compiles and on-the-fly syntax checking, as well as autocompletion and refactoring. I should say that this *doesn’t* stop Unity re-compiling your files when you switch back to it – I’ve not made any attempt to unify them, as I suspect it’s more trouble than it’s worth.

The key elements are:
– A ‘make’ script.
– Flycheck
– CSharp-mode.el
– Omnisharp-emacs

The make script

My make script is something I hacked together in python a couple of years ago, worked out from examining the compile commands Monodevelop emits, and it has grown to support various features. You can download it from here – be warned it’s not particularly pretty, and flits between about 3 different coding standards…

Drop it into the root of your Unity project and change it so it captures your source files. The arguments the make script expects are ‘fast/slow’ (explained in a moment), the working directory of the compile (usually the root of the Unity project) and, optionally, a file to exclude and a file to add (again, explained below).

The ‘fast/slow’ bit is something I added to allow for faster compiles. Once you’ve got a Unity project that includes lots of generic game libraries (your own utility functions, NGui, etc) compile times can slow down quite a bit. It therefore makes sense to partition your code into ‘stuff that’s rarely changed’ and ‘actual game stuff’, and only compile the rarely changed stuff… well, rarely.

If you look at the code of the make file there’s one function to get the source files for the rarely change library, and another function to get the source for the main game. If you pass down the ‘fast’ option, it the make script won’t bother compiling the library; if you pass down ‘slow’ it will. In this way it’s possible to keep your general compile times down in Unity to a couple of seconds, even for large projects.

Triggering a compile from within Emacs is fairly straightforward.

(defun unity-compile-game ()
(let ((cmd (concat "python " (project:project-root project:active-project) " fast " (project:project-root project:active-project))))
(compile cmd)))

(defun unity-recompile-game ()
(let ((cmd (concat “python ” (project:project-root project:active-project) “ slow ” (project:project-root project:active-project))))
(compile cmd)))

As you can see, all it does is create a command line of ‘python my-project-root/ my-project-root slow’ (or ‘fast’) and passes that down to the built in ‘compile’ command. I’ve got a custom project system I use in Emacs (I should probably move over to using something like projectile in the future) which I use to find the project root from a given source file – you should switch in whatever system you’re using instead.


I use the excellent flycheck to check my builds as I work. Because we’re able to keep our compile times nice and short it provides very fast feedback on code issues as you type, which I personally find invaluable. And, unlike alternatives like Monodevelop’s built in live ‘syntax checking’, it runs an actual build of the game so it’s always completely accurate.

Setting it up is fairly simple:
(require 'flycheck)
(flycheck-define-checker csharp-unity
"Custom checker for Unity projects"
:modes (csharp-mode)
:command ("python" (eval (concat (project:active-project-root) "")) "fast" (eval (project:active-project-root)) source-original source)
:error-patterns((warning line-start (file-name) "(" line (zero-or-more not-newline) "): " (message) line-end)
(error line-start (file-name) "(" line (zero-or-more not-newline) "): " (message) line-end)))

Again, switch in whatever you need to get the root of your project. Flycheck works by saving out a temporary version of the file being edited, which can then be switched for the original file in the build – this is what the ‘exclude’ and ‘extra’ parameters of the make script are for. Thus the command line looks something “python SomeFolder/ SomeFolder SomeSourceFile.cs SomeSourceFileFlycheck.cs”.

I should add that the error patterns I’ve given to flycheck are probably monodevelop specific and have only been tested on my OSX machines.


CSharp-mode provides C# syntax highlighting and other things for Emacs. It looks like it has been abandoned, but it’s complete enough that I’ve not hit any major issues with it.

I tend turn off csharp-mode’s (dodgy) imenu support and turn on it’s brace matching:

(setq csharp-want-imenu nil)
(local-set-key (kbd "{") 'csharp-insert-open-brace)

Omnisharp for Emacs
Completing the picture is Omnisharp for Emacs, which I’ve blogged about before. It’s awesome, and gives you auto-completion, refactoring and all sorts of other loveliness. I personally use it in tandem with company-mode.

Oh, one more thing. Get annoyed on OSX because double clicking a file in Unity doesn’t open it at the right line number in Emacs? Some smart chap worked out how OSX handles line numbers (the command line isn’t good enough for some operating systems, apparently) and wrote Sublime Proxy to intercept the appropriate events. I’ve (badly) hacked it to work with Emacs. Get it here and make it less bad if you want.

That’s it for now. I hope someone finds it useful, let me know if something doesn’t work.


* Okay, not really that often.

‘var’, ELDoc and Omnisharp

A couple of weeks ago in the office we were having a conversation about the pros and cons of using ‘var’ in C#. If you’re not aware of it, ‘var’ is the C# equivalent of ‘auto’ in C++11; it tells the compiler work out the type of a variable declaration for you, based on the type of its initialising value. So for example:

List<GameObject> someList = new List<GameObject>();

can be replaced with:

var someList = new List<GameObject>();

The compiler can automatically work out that someList should be a list of GameObjects based on the initial assignment. Moreover because it does so at compile-time, there’s no runtime overhead, unlike with ‘real’ dynamic typing. Over time this can save a fair amount of typing, and reduces silly typo-based compiler errors.

It’s also a useful tool when refactoring. Consider the following:

foreach(SomeType loopVar in ImportantList)

This can be replaced with with:

foreach(var loopVar in ImportantList)

Now, if you change the type of ImportantList, the loop will ‘just work’ (as long as the loop body still contains valid code, obviously).

The concern in our office was regarding code readability. The more you use ‘var’, the harder it can be to work out what’s going on. For example:

var inventory = GetInventory();
var slot = inventory.GetSlot();
foreach(var item in slot.GetItems())
{ ... }

If you’re new to this code it’s hard to know what slot or item is without jumping to another file. Although I’m personally happy with the tradeoff, the concern is a valid one and I wanted to try and develop a tool to address the issues.

Thankfully a dash of Emacs, a bit of ELDoc and a giant pinch of OmniSharp provided a pretty comprehensive solution. ELDoc shows information about the symbol at point in the echo area. It didn’t take long to get OmniSharp and ELDoc working together, and the result is very handy even if you don’t use var.

Some examples:


Here we can clearly see the type that has been assigned to myTween in the echo area, even though we’ve declared it with var.


And here we can see the complete function signature and return type of, which makes understanding the code around the call easier, especially in the presence of heavily overloaded methods.

And that’s it! ELDoc support is in my OmniSharp fork on github, and it will hopefully get pulled across to main soon. I hope someone finds it useful.

My new +3 helm of swooping

I’ve been working on a post about game design. I’ve recently (re)learned lots of fun things about game prototyping, the importance of identifying and maintaining a game’s design ‘promise’, and how critical it is to keep the design/implementation/feedback loop tight. In short, it’s a post that’s not about Emacs.

This is not that post.

I’ve recently discovered helm-swoop. Helm-swoop is an Emacs helm extension that displays a list of matches for a search term in a popup helm buffer. You can navigate the list of matches with the cursor keys, and point will jump around in the buffer being searched to the relevant line. You can also continue typing to narrow down the search term further.

I’ve nabbed a gif from helm-swoop’s github page to make it clearer:


I’ve always been quite satisfied with the various incremental searching systems in Emacs; i-search and evil-mode’s vim-style search are excellent, best-in-class systems. But I love the way that helm-swoop shows you all the matches to your search, with some context, without you having to move point. You start typing, look to see if you get the hit you want, change your mind, retype. Once you’ve got something close to what you want you move down the list of hits, keeping your eye on the main buffer, and hit enter when you’re there. It’s very slick.

On top of this, you can live-edit the helm-swoop buffer, in a manner reminiscent of Emacs 24’s occur-edit-mode. Search for ‘color’, C-c C-e to go into edit mode, change the occurrences you care about to ‘colour’, C-c C-s, and the original buffer gets your changes and proper spelling. Fantastically powerful stuff.

i-search and the evil-equivalent still have their place, of course, and I use them regularly, but I’m using helm-swoop more and more.

That’s it! Try out helm-swoop, and I promise my next post will be something a bit less Emacsy.

Somehow I managed to make that sound like a threat…

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.

Emacs and Unity Every Day

As I may have mentioned before, I like using Emacs. When working on Fable we were of course entirely based around the glorious Visual Studio, since the XBox and the 360 required it, and I started out using Emacs largely for editing Python and Lua code; we used those languages for various tools and scripting duties and Visual Studio at the time didn’t have very good support for ‘other’ languages. Gradually I drifted into using org-mode, then into eshell, and eventually I got to the point where I started missing certain features in other editors. Yes, even the glorious Visual Studio.

These days I’m working on a Mac, using Unity. I tried to use MonoDevelop but found it to be slow and clunky, and not being able to view two files at the same time was very frustrating. Eventually I switched entirely over to using Emacs. It wasn’t easy at first, and I still lament the lack of a decent autocompletion system, but in general I’m very happy with the workflow.

You don’t spend numerous years using Emacs without picking up a few customisations and I thought it would be worth documenting a few of the ones I use most often. If anyone finds it useful, I might dip further into the recesses of my dotemacs file.

So, here we go; Emacs things that I use every day.

Evil mode
evil-mode is a fabulous vi emulation layer in Emacs. I’m pretty new to it, and I originally turned to it in a last ditch attempt to cope with some RSI issues I’d been developing (at this point I have probably bought pretty much every ergonomic keyboard in existence). I’m not sure if it helps the RSI, but it has certainly helped my coding speed. A side benefit is that, while Emacs is incredibly flexible, it’s very easy to run out of keybindings for all your custom functionality. Evil-mode and it’s modal system largely makes that problem go away.

Ace jump
Inspired, I believe, by a vim plugin called from Easy Motion, ace-jump allows you to jump to any character on screen in (usually) 3 keypresses. It’s a little difficult to explain how, so I’ll skip straight to a demo of it in action. I hit space (my personal activation key in evil-mode), select a character to jump to (in this case ‘p’). Ace-jump highlights every ‘p’ there is and gives it a unique letter. I choose the one I want (in this case ‘n’) the character jumps to the correct location.

In that particular example, I’m jumping to any character on the screen which can create alot of hits, but there are other ways to call it which only highlight characters at the beginning of words.

It’s looks a bit mental at first, but once you’ve used it there’s no going back.

Mark Multiple
Another toughie to describe in text, mark-multiple allows you to select a section of text and then select further matching sections and edit them all simultaneously. If you’ve ever used Sublime Text you’ll be familiar with it. Here’s an example:


Here I hit v to select the terminating semi-colon, M-j to select further semicolons below, c to change the selection, and then type in the new code. There are other ways to achieve the same thing, of course, but mark-multiple is nice and tactile.


This used to be called ‘Anything’, and it’s essentially a generic quick jump system for Emacs, and it’s easily extended. For example, I have a simple custom project management system to allow me to easily compile projects, select which files go into a tags file, etc, and I use Helm to select files anywhere in my project, regardless of whether or not they are already open. Again, a simple example should suffice:


In this I hit C-x f to trigger the helm file finder, and type in part of the file I’m looking for. It shows files in my project, recently accessed files, open buffers; once I’ve narrowed down the selection enough I can then select using the cursor keys and hit enter to select the file.

Alot of coding is repetitive stuff, and TextMate’s ‘snippets’ innovation was a great way of tackling some of the more tedious bits. The canonical Emacs implementation yasnippet expands on that, allowing you to create some very sophisticated code templates. Previously I’ve struggled to get it in my workflow – remembering the exact trigger phrase for an expansion never worked for me, and autocompletion often got in the way. I’ve recently discovered that you can trigger a snippet manually, however, and then use fuzzy-matching via ido to choose the right snippet. Example below:


In this example I’m hitting C-c s to trigger yasnippet, typing ‘fore’ to find the foreach expansion and hitting enter – it creates the skeleton and then I tab through the editable sections, filling them in as I go, with the last tab placing me in the braces. When you’re spending all day writing classes with conditions and loops and methods, this can be a big time saver.

Something you may have noticed in one or two of the examples above is that I get syntax errors highlighted on the fly. This is the magic of flymake, which continually compiles the project in the background. I’ve got it hooked up with and a script to compile the Unity project, and it makes for a very smooth Unity coding experience. Errors underline in red as you type, the error message shows up in the minibuffer when you move the cursor into position, and it disappears when you fix it. Hardly revolutionary, but a nice productivity boost. It also seems to catch more genuine issues than the ‘on-the-fly’ error checker built into MonoDevelop, which is nice.

Shaderlab mode
Shaderlab-mode is a custom mode for editing shaders for Unity. It’s relatively basic, but it handles syntax colouring and indentation, which is all I really care about.

Wrapping it up
The upshot of this is that I spend 90% of my day in Emacs – from the start of the day when I check my list of tasks, to the edit-compile cycle for csharp, to editing shaders; I rarely leave Emacs, and when I do it’s usually just to tab on over to Unity to test out my changes.

I believe all of these extensions are available via ELPA, in marmalade or milkbox. Regardless, pulling it all together can take a little time, so I thought it might be worth sharing the details of how that’s done in my config. I’ve cobbled alot of this from various places on the intermawebs, notably” title=”here”>here and here.

(require 'evil)

;;This gives you all the normal Emacs editing commands when in insert-mode
;;This eases the transition somewhat.
(setcdr evil-insert-state-map nil)
(define-key evil-insert-state-map
(read-kbd-macro evil-toggle-key) ‘evil-emacs-state)

(define-key evil-insert-state-map [escape] ‘evil-normal-state)

;;Not sure why, but semicolon started misbehaving for me when in insert mode.
(define-key evil-insert-state-map (kbd “;”) ‘self-insert-command)

;;Make evil-mode up/down operate in screen lines instead of logical lines
(define-key evil-normal-state-map (kbd “j”) ‘evil-next-visual-line)
(define-key evil-normal-state-map (kbd “k”) ‘evil-previous-visual-line)

;;Exit insert mode by pressing j and then k quickly
(setq key-chord-two-keys-delay 0.2)
(key-chord-define evil-insert-state-map “jk” ‘evil-normal-state)
(key-chord-mode 1)

;;If you work in camelCase or use underscored_names, this is very helpful
(evil-define-motion evil-little-word (count)
:type exclusive
(let ((case-fold-search nil))
(search-forward-regexp “[_A-Z]\\|\\W” nil t)

(evil-define-motion evil-little-word-backward (count)
:type exclusive
(let ((case-fold-search nil))
(search-backward-regexp “[_A-Z]\\|\\W” nil t)))

(define-key evil-normal-state-map (kbd “, b”) ‘evil-little-word-backward)
(define-key evil-normal-state-map (kbd “, w”) ‘evil-little-word)
(define-key evil-operator-state-map (kbd “, w”) ‘evil-little-word)
(define-key evil-operator-state-map (kbd “, b”) ‘evil-little-word-backward)

;;Not sure why this isn’t the default – it is in vim – but this makes C-u to go up half a page
(define-key evil-normal-state-map (kbd “C-u”) ‘evil-scroll-up)

;;Allow quick manual triggering of snippets
(define-key evil-normal-state-map (kbd “C-c s”) ‘yas-insert-snippet)
(define-key evil-insert-state-map (kbd “C-c s”) ‘yas-insert-snippet)

;;Toggle comments
(define-key evil-visual-state-map (kbd “, c”) ‘whole-line-or-region-comment-dwim)
(define-key evil-normal-state-map (kbd “, c”) ‘whole-line-or-region-comment-dwim)

;;Easy access to the mark multiple library when in visual-mode
(define-key evil-visual-state-map (kbd “M-j”) ‘mark-next-like-this)
(define-key evil-visual-state-map (kbd “M-k”) ‘mark-previous-like-this)

;;Ace jump stuff. Honestly, I just use Space 99% of the time
(define-key evil-normal-state-map (kbd “SPC”) ‘ace-jump-char-mode)
(define-key evil-normal-state-map (kbd “, , w”) ‘ace-jump-word-mode)
(define-key evil-normal-state-map (kbd “, , c”) ‘ace-jump-char-mode)
(define-key evil-normal-state-map (kbd “, , l”) ‘ace-jump-line-mode)

(define-key evil-operator-state-map (kbd “, , c”) ‘ace-jump-char-mode)
(define-key evil-operator-state-map (kbd “, , l”) ‘ace-jump-line-mode)

;;Personally I like ace-jump to be limited to the window I’m working in
(setq ace-jump-mode-scope ‘window)

;; Remap org-mode meta keys for convenience
(mapcar (lambda (state)
(evil-declare-key state org-mode-map
(kbd “M-l”) ‘org-metaright
(kbd “M-h”) ‘org-metaleft
(kbd “M-k”) ‘org-metaup
(kbd “M-j”) ‘org-metadown
(kbd “M-L”) ‘org-shiftmetaright
(kbd “M-H”) ‘org-shiftmetaleft
(kbd “M-K”) ‘org-shiftmetaup
(kbd “M-J”) ‘org-shiftmetadown))
‘(normal insert))


Most of that should be self explanatory with the comments. One thing to mention is that this makes use of the excellent keychord.el and comment-dwim both also available via ELPA repository. Keychord allows you to combine arbitrary keypresses into triggers, and it’s what allows the ‘jk’ trick above. There’s lots of other stuff I use too – org, nav, bm, window-number, winner – and lots of personal customisations, but I’ll leave those for another day.

The main thing I really miss is proper ‘correct’ autocompletion for C#, which Visual Studio spoiled me with for years. Someone has created a plugin for Sublime, called CompleteSharp, which uses a standalone commandline program to load up your assemblies and provide completion information, and I’m sorely tempted to write some Emacs hooks for it. If only I had the time…