Unity Development with Vim and Git in Windows – Part 1

Overview

Purpose

This guide is meant for developers who might have dabbled with the command line, Vim, and Git, and want to learn a little more about using it specifically with Unity. If you haven’t done anything with Vim, check out vimtutor or some online tutorials about Vim just to get the basics.

Background

Vim is a bare bones text editor. Vim users would say that with all the plugins and shortcut keys and how it’s designed to keep your fingers on the keyboard, that they find they can enter into a flow with writing their code, and the text editor itself takes more of a backseat. I find more featured IDE’s, like Visual Studio, don’t necessarily fade like that, and might get in the way. It’s really not such a bad thing. Visual Studio is extremely powerful; you can attach a debugger with Unity VS Tools, add Vim emulation with VsVim, and all sorts of code completion features with Resharper. And for the most part it just works.

Vim is much more hands on, has a much higher learning curve, and takes time to be intuitive and natural. The main reason I decided to learn more about it is that several plugins for Vim now try to emulate some of the features of Visual Studio but without getting in your way too much. And you can always customize Vim to do just about anything you really need it to do. And for me, I’ve found that the more I’m able to use the keyboard for things, the more I’m able to forgot about whatever particular IDE I’m using and just write code.

If you’re less interested in learning Vim for the sake of learning something new, and more interested in improving your productivity as quickly as possible, use Visual Studio + VsVim + Resharper.

Installations

Git

Git is what we will be using to manage and track all of the code we write. There are plenty of git tutorials you can go through, and it can get a little involved if you want to learn everything about it. It might be worth it if you plan on working in a team, and there is value is just knowing the commands, but to get you up and running as quickly as possible, we’re just going to go over what we need to know in order to make games.

All You Need To Know

Git is a type of source control or version control. You tell git that a particular folder on your computer is going to be a repository. Then as you create new files you tell git to add these files to this repository (meaning it will start tracking what happens to the file). When you are done making some changes, you can commit all your changes at once and give it a message describing what you’ve done. For example, if I was adding a new weapon to a game, I would write the new code for the weapon, add the art assets, sounds, animations, and anything else I needed. Then I’d tell git which files I created or changed, then commit them with message of “Added a new weapon”.

What’s the purpose of this? I can go back and see all my commits. So it’s kind of like saving everything at a particular point, and being able to go back to that point at any time. Like a super powerful undo/redo. But it also has the power to upload all of those things to the internet, so you’ve got a backup of everything you’ve ever done to your game, and if you have other people working on it, you can easily add in what they’ve done without losing anything you’ve done.

Install

First download git from the git homepage. There should be a Download for Windows link.

git download

Start installing it. Here are the settings I used:

git setup 1

git setup 2

Git Bash and Windows Command Prompt? Git has it’s origins in Linux. Git Bash is a way to have a ‘linux kind of command prompt’ where you can execute all the git commands and some linux commands. The second option is for adding git commands to the normal Windows command prompt. We’ll be using Vim and Git Bash, so select the first option. I typically try to stay away from the Windows Command Prompt, and it’s ugly.

The next option is about line endings, or what it looks like in code when you hit enter. I just select the first option and be done with it.

The final option is if you want to use a terminal emulator or the command prompt. Again, command prompt is pretty awful, and the MinTTY allows you colors, better looking text, resize options, etc. Select the that option.

Vim

If you aren’t familiar with Vim, I’m only going to explain the things that pertain to what we’re doing. That is, I won’t explain how to replace a word or record a macro, unless it’s specific to our setup. If you are completely new to Vim, once you’ve installed Git Bash and opened it, type “vimtutor” and that will walk you through the basics.

Go to the vim downloads page to download the latest version of vim. At the time of writing, it was 7.4.

vim download

Installation should be straightforward.

Configurations

One of the most powerful things about Vim are plugins. We’ll start by adding our first plugin called Pathogen. The purpose of this plugin is to make it super easy to install other plugins. It’s kind of like a plugin manager. And we’ll do this in our newly installed Git Bash, so we’ll be using Linux commands.

First open up Git Bash.

bash 1

I do some basic customization by right clicking on the title bar of this window and selecting options.

bash 2 custom

I set the transparency to low, the text to Lucida Console 12 pt, full font smoothing, no scrollbar, and I’ll drag out the borders to make the window bigger and set that as my default size.

On to Pathogen installation. Pathogen is popular enough where you probably would come across it just by looking into Vim. On Pathogen’s github page, it has instructions on how to install it, which is the following:

First we’ll make two directories in our home folder. In this case, the “~/” means home, which for Windows means “C:\Users\<your username>\” :

mkdir -p ~/vimfiles/autoload ~/vimfiles/bundle

bash 3 mkdir

The “-p” part is an option that just means “create the parent if it doesn’t already exist”. So with this command it’s creating the vimfiles directory, and the autoload and bundle directories beneath vimfiles. And since we use the “~/” that means it’s all created in the C:\Users\<your username>\ directory.

After you type the mkdir command, you get another empty prompt, where I type:

ls ~/vimfiles

“ls” is a command to list the contents of a directory. The format is “ls <space> <the directory I want to list>”, and it shows that my two new directories have been created.

Now we’ll get the code for Pathogen and download it into the autoload directory:

curl -LSo ~/vimfiles/autoload/pathogen.vim https://tpo.pe/pathogen.vim

curl is a command to send http requests (like a web browser) from a command line. The -LSo are just options (you can google it if you’re really interested, but you won’t use them too much) that say “go to the https:// part, download whatever is there, and put it in the autoload directory”. You should see the following (I also did an ls command into the autoload directory to verify the file is there).

bash 4 curl

Configuration – .vimrc or _vimrc

This file (on Window’s it’s _vimrc, on mac/linux it’s .vimrc) is basically a configuration file for Vim. You can set all of your custom commands in Vim here. And that’s one of Vim’s features: I can custom tailor it to just the way I want it, and upload the vimrc somewhere so that any where I go I can just download my configuration and be up and running in no time.

So we have two different installations of Vim right now. We have the Vim that is installed with Git Bash, and the one we downloaded and installed called gVim. Normal Vim (not gVim) more a part of the terminal and gVim is more a standalone program. On Linux/Mac we would stick with Vim, but since we don’t have that terminal available (we have been using Git Bash as our terminal, which is an emulated and scaled down version of the mac/linux variety), we’ll be using gVim.

But that doesn’t mean we can’t use Vim to do some basic stuff to help us get up and running.

Pathogen + Plugins

First we need to create our _vimrc file, and we’re going to use it to tell Vim about Pathogen. Both the normal Vim and gVim use this _vimrc, by the way. While still in our terminal, type:

vim ~/_vimrc

This is going to open up normal vim, and create an empty file in our home directory called _vimrc (with no file extension). If you don’t know the basics of vim, type “:q” without the quotes (literally hit shift and make a colon, then hit the letter q, then hit enter) to quit vim, and then type vimtutor and go through that lesson.

Add the following to your opened vim:

" Pathogen
execute pathogen#infect()
syntax on
filetype plugin indent on

The first line is what hooks Pathogen into our vim. The ” are just the start of a comment line.

syntax is Syntax Highlighting which enables highlighting, different fonts, etc.
filetype plugin indent on turns on the ability to use plugins depending on their filetype, and indents for different file types or programming languages.

That should be enough so that we can start downloading plugins into that bundle directory we made, and Pathogen will detect them and add them to our Vim. The Vim plugins we’re going to use are hosted on github. This means we aren’t going to go to a website and click download plugin. We’re going to find them on github, and use the terminal to download them.

If you aren’t sure how to save this, type “:wq” which will write (aka save) your changes then quit vim.

NERDTree

Our first plugin we will download is called NERDTree, which adds a file browser in our Vim. Back in our terminal, type the following:

cd ~/vimfiles/bundle
git clone https://github.com/scrooloose/nerdtree.git

cd changes our current directory (we were at ~ which is also known as home, and on windows is C:\Users\<your username>\). So we’re just switching into the bundle directory.

git is always followed by a type of command, in this case we’re using clone and giving it a URL. If we were searching for a Vim file browser, we would come across NERDTree’s github page, which would say ‘here’s the URL to clone if you’re using Pathogen’, which is how we know to use that URL.

git nerdtree

This just takes all the files at that URL and downloads them in your bundle directory. It’s a convenient way to get all the files of a plugin.

Now we’ll start using gVim. Unfortunately, running Vim right now it wouldn’t recognize our new plugin because the Git Bash terminal uses a separate version of vim. We’ll keep our Git Bash open though, to download more plugins. For now we’ll open up a normal windows command prompt. If it doesn’t open to your Home directory, manually navigate there:

cmd 1

Now type “gvim _vimrc”. Yes, it looks horrible. We’ll fix that though. Let’s test to see if NERDTree works. There are a bunch of NERDTree commands, but we don’t want to memorize a bunch of them. By looking at the docs you can see that the :NERDTreeToggle should toggle the browser on and off. Type that now and you should see it open in the left hand size of your window:

nerdtree

Hit CTRL + W then W again to move the cursor between NERDTree and your _vimrc.

Again, here is another point where we can customize Vim however we want. We can have NERDTree open automatically, or make a shortcut and only toggle it then. It’s completely up to you. I like to assign it a shortcut and to make it a litte bigger, so add the following to your _vimrc:

" NERDTree Settings
map ,n :NERDTreeToggle<CR>
let g:NERDTreeWinSize=48

nerdtree vimrc

This maps the key combination “comma n” to open or close the NERDTree. The <CR> is the same as hitting the enter key. Type “:wqa” to save the changes and quit (the a is for all, so quit the _vimrc and the NERDTree). Open vim back up on the _vimrc file, and hit ,n and NERDTree should toggle on and off. Use the same Vim navigation keys in NERDTree and hit O to open a directory or file.

Right now we have just the bare installation of Vim with one actual plugin. Next time we’ll start making it not look so horrific and add some more plugins.