Unity3D with vim

Hey! Since I’m already working with unity on linux as a productive environment, I thought I’d share what makes the possibility to work under linux so good - Being able to use vim and a terminal emulator to work on your sources.

Why Vim?

Vim is fast an lightweight. Vim inside your favorite shell let’s you keep your hands on the keyboard and automate things with aliases and scripts. Vim can look great and no UI elements blocking you from viewing your code.

Should I use it too?

Only if you’re comfortable with vim keybindings and already heard of vim plugins. Visit

http://www.openvim.com/

For your pleasure.
You can always still use MonoDevelop with Unity, it’ll run in the background independent of Unity or MD.

How does it look and feel on linux?

Good question! That’s the most important part. It looks glorious on linux. I’m currently using xfce4 and a customized terminator and I love it.

How can I have it?

  1. Installing vim is easy, just get it with your favorite package manager.

  2. Using plugins in vim is also easy, but there are several ways. I personally prefer pathogen. Please take a look at GitHub - tpope/vim-pathogen: pathogen.vim: manage your runtimepath

  3. These are the plugins I use:

auto-pairs/
omnisharp-vim/
rainbow_parentheses.vim/
syntastic/
tagbar/
vim-airline/
vim-colorschemes/
vim-colors-solarized/
vim-dispatch/
vim-easymotion/
vim-multiple-cursors/
YouCompleteMe/

  1. For Unity AutoCompletion you’ll especially depend on omnisharp-vim, Syntastic and YouCompleteMe. Again, there are alternatives, but this is what I use.

There is a detailed documentation for omnisharp-vim which you’ll have to follow to install it. It’s something like that:

cd ~/.vim/bundle
git clone GitHub - OmniSharp/omnisharp-vim: Vim omnicompletion (intellisense) and more for C#
cd omnisharp-vim
git submodule update --init --recursive
cd server
xbuild

Which can possibly fail on xbuild. It depends on the mono project, which you can easily install via apt-get or dnf, but(!) can still fail if installed. The crucial part is that you’ll have like the new-new version of mono to compile it. Something like 3.2.4 or so should do the trick.

Also don’t forget to compile YouCompleteMe:

cd ~/.vim/bundle/YouCompleteMe
./install.py --clang-completer

If installed and compiled correctly, you can update your .vimrc to include omnisharp, syntastic and YouCompleteMe.

Here is my complete vimrc file: https://www.fumiko-game.com/cloud/index.php/s/REB6weWElTZUrU0/download
And (I guess) only the autocompletion stuff: https://www.fumiko-game.com/cloud/index.php/s/yD6wHIGbBrv3GEr/download

I’m not sure about everything in my .vimrc, so don’t expect a perfect example - It’s just what works for me and doesn’t let me down.

How do I know that it works?

Open a source .cs file in your project and see if vim asks you to open one of the solution files:

Just choose one (I experienced no difference, don’t know if there should be two or just one .sln) and if you’ve got AutoCompletion on objects and variables you’re good to go, that’s all we wanted - Proper auto completion for vim that includes Unity’s predefined classes.

ps aux | grep omnisharp

should show something like:

That shows us that the OmniSharp Server is running in the background.

Now just edit your files, mess with your code, save with :w and switch to Unity to auto-compile your efforts. Syntastic will tell you about any typos and bad practice and Unity will tell you if something has gone wrong.

I hope this helps some people setting up Unity with vim since I’ve always wanted to do it but I didn’t even want to try it under windows since the implementation of vim is so clunky.

I don’t know if I forgot anything important for the installation since I set it all up two weeks ago. Feel free to ask if there are any issues, maybe I can help.

17 Likes

Eres barbaro, que inteligente (Y)

Thanks for the great tutorial! I also use vim for nearly everything, thinking about the switch to it from MD, just after I set up the completion stuff.

Also, I see you use powerline too! Please mention it in the tutorial, since it really makes the whole experience much nicer. You may also want to look at powerline-daemon, which greatly increases powerline’s performance. I recently sent a PR to them with a systemd service for it. It got accepted, but will only come in the next release, so you’ll have to manually download it if you want :wink:

Also, for those looking for a good colourscheme, I definitely recommend Solarized. Check it out!

Hi @Odd-Redesign – thanks for your post! I use vim with omnisharp also (on OS X). I’ve been thinking of changing the omnisharp backend to the new roslyn service as that seems to be where things are headed. Have you done this? If so, would love to hear your experiences! The documentation is pretty sparse so far.

What would be the benefit of using roslyn?

Not 100% sure but it’s what the OmniSharp folks are most actively working on, it seems, as that is the latest and best tech that Microsoft open-sourced recently. It also apparently is less buggy, e.g. just filed a bug with rename refactoring and was told to switch to roslyn.

Hey Ghopper21!

Sounds like an interesting idea. I still cannot get omnisharp-vim working on my laptop, so maybe I’ll try out roslyn there. For now, I haven’t run into any major issue with omnisharp-server on my main machine (linux mint), so don’t see any reason to switch.

Seems like roslyn is already supported by the omnisharp-vim project: GitHub - OmniSharp/omnisharp-vim: Vim omnicompletion (intellisense) and more for C#

I fixed the screenshots in my initial post. Good luck getting roslyn to work, would be glad to hear about whether you have been successful or not.

@Ghopper21 hmm, good to know they are abandoning the old server.

I had troubles trying to use roslyn on Ubuntu. The server is like pre-beta-rc-candidate version. I think it’s too early to use it. In its readme GitHub - OmniSharp/omnisharp-roslyn: OmniSharp server (HTTP, STDIO) based on Roslyn workspaces you can see that it fails to build at the moment. Not good:

But I managed to get omnisharp-vim working. Even with Gtk#.

Hi vimmers –

Anyone having problems using vim with the latest 5.3.2 version of Unity?

I had omnisharp-vim more or less working and now it’s even flakier than before, sometimes working for autocompletion/rename/goto def/etc, sometimes not. Also I noticed this behavior that if vim saves a file, Unity will recompile in the background and cause vim to freeze up while it does so.

I wonder if this has something to do with Unity changing how they handle solution files – the “Sync to MonoDevelop project” menu command is now “Open C# Project” – dunno if that’s just cosmetic or some deeper change. I also recall seeing some changelog thing about a new way Unity communicates with MonoDevelop – maybe that is related to these new problems with vim?

Any thoughts/experiences very welcome!

@Odd-Redesign … dude… this was a nice write-up and definitely “buy me a beer worthy.” Thanks :slight_smile:

I know it’s been awhile but are you still using VIM with the latest version of Unity? Any issues? Thanks again.

1 Like

I am still using it with 5.6.3xf1Linux, but there shouldn’t be any issues with newer versions of Unity. The thing about this tutorial is just that it is becoming a bit old now and there might be even better and more complete methods of doing this. I would love to start fresh and come up with a new tutorial, but at the moment I am working on an xbox port, binding me to windows 10 and visual studio. As soon as that is done though… it might be a good time to get this done. I even have found solutions to double-clicking a .cs file in Unity and adding it to the VIM buffers correctly.

If you’re trying this out and there are any issues blocking your way, please let me know.

#!/bin/bash
if [ -n $(wmctrl -l | grep Terminator-Vim) ]; then
  mytitle='Terminator-Vim'
  terminator --working-directory="`dirname "$@"`" -x "echo -e '\033]2;'$mytitle'\007' && vim '$@'"
else
  wmctrl -a Terminator-Vim
  setxkbmap de # this might be optional if not on DE keyboard
  xdotool key colon
  filename=$(printf %q "$@")
  xdotool type --delay 1 "e $filename"
  xdotool key Return
fi

exit 0

It’s a bit hacky but I am using this as an executable bash script named terminator-vim, which I set as the external editor in Unity. This makes double clicking files open correctly in my current vim-session. Just wanted to add this in case someone wants to reproduce this or wants to write his/her own script.

It tries to find a window called Terminator-Vim and if it finds it, simulates key input with xdotool to open the file with “:e $filename”. If it doesn’t find a window, it opens one and initializes Terminator to use the correct directory and uses the file name to start a vim session. Sometimes I have to start Terminator first to initialize a shell session, but after that it works great.

1 Like

@Odd-Redesign I should have mentioned that I was on a MAC. That said, I pretty much had all of the same plugins you suggested and then some due to developing against Node.js / React Stack. The only thing that I needed to do was actually update my version of mono and reinstall YouCompleteMe with the proper interpreter flags to support Csharp.

So with that said I’m also using MacVim. But the setup you broke down worked with ITerm2, Hyper, and my native terminal. I love VIM. It makes coding a joyful experience for me.

Below is a screenshot of a tutorial file running in VIM on the latest version of Unity. Autocomplete works, IntelliSense works, … everything works. I’m good. Thanks!

1 Like

I’m using nvim on macos. Also it’s not compiled using Python. Can these plugins be used with nvim(not compiled using python)?

❯ nvim
Error detected while processing /Users/abhimanyuaryan/.dotfiles/config/nvim/plugged/omnisharp-vim/plugin/OmniSharp.vim:
line 8:
Error: OmniSharp requires Vim compiled with +python
YouCompleteMe unavailable: requires Vim compiled with Python (2.6+ or 3.3+) support.
Error detected while processing /Users/abhimanyuaryan/.dotfiles/config/nvim/plugged/ale/after/plugin/ale.vim:
line 28:
E605: Exception not caught: ALE conflicts with Syntastic. Uninstall it, or disable this warning with let g:ale_emit_conflict_warnings = 0 in your vimrc file, before plugins are loaded.
Press ENTER or type command to continue

I used Unity3D with vim for like 3 years. I’ve been using Rider with IdeaVim recently but I’m not liking it as much. Unfortunately SDB still doesn’t have an “attach process” facility, so I might continue to use Rider in the background for when I need debugging, unless you guys are having success with something else.

However, I’m switching to emacs (with evil of course), so I’ll keep you guys posted on my results. Hopefully by next week I have my whole env setup. I tend to use a highly customized config so it’ll take a while. Please note I don’t mean to troll, instead I think emacs and vim users should unite so we can help each other since both are unix style editors and there’s a lot of overlap.

Thanks for posting this(and starting the thread!). I got this to work wthout xdotool, wmctrl, etc.

Basically, just put something like this in your shell script.:

#!/bin/bash
termite  -e "vim --servername unity $@"

I’m using termite obviously, but notice the --servername parameter. See https://vi.stackexchange.com/questions/317/can-i-open-a-file-in-an-existing-vim-instance-from-an-external-command for more details.

Then in unity, just pass in the same args that are set by default for gvim:

--remote-tab-silent +$(Line) "$(File)"

Voila. All scripts opened from unity, will open as buffers in the “unity vim” instance.

edit: oh, actually they open as tabs, not buffers. For buffers use the following instead.

--remote-silent +$(Line) "$(File)"

eidt2: The above args will also allow for double clicking an error message in the console and jumping to the appropriate line the the script.

1 Like

I can’t seem to choose my Neovim executable as the external script editor in Unity 2017.3.1f1 Personal. It looks like Unity is only allowing me to choose .app bundles. Has anyone run into and overcome this?

@nscheurich You can easily fake an OSX app bundle by simply creating a folder of the same name as your executable bash script, and append an .app extension to it.

Say you have an executable bash script called MyScript, just create a folder named MyScript.app and put your script inside, it’s simple as that.

1 Like

This could be a good starting point to open terminal vim from Unity (macOS only)