Howto: use gVim with Unity

I want to mention how to get gVim working with unity (so that double clicking an error in the error console, for example, opens the offending script at the correct line number in a tab in an existing gVim window (or a new window, if none exist))

I know that the wiki would be a better place for this, but I’m swamped with work atm and don’t want to spend time learning how to edit wikis. I hope it’s ok to just make a post here :slight_smile:

To set up Vim for use with Unity, a wrapper application is necessary. Unity does not pass information about the line an error has occured on, when using external editors. The only time Unity passes on line information to an editor, is when the built-in UniScite option is used.

UniScite receives 2 params. Param 1 = script filename, param2 = “-goto:”, followed by a line number. So the wrapper application must parse the 2nd argument and then execute:

<path_to_gvim>\gVim.exe --remote-tab-silent "%1":%2

where %1 is the script filename and %2 is the parsed line number. (You can replace --remote-tab-silent with --remote-silent to open the file in existing vim window but not in a new tab. Or remove it altogether to open the file in a new window)

Furthermore, to have vim actually scroll to the specified line, you need the following plugin:
http://www.vim.org/scripts/script.php?script_id=2184

Without the plugin, gvim interprets the line information as part of the filename.

So there you have it, that’s the information you need to set up gVim with Unity. I have attached the source to the wrapper application (in C++) that I used, so you can just grab it and use that. I’m not very proficient with C++, though, so don’t expect anything elegant :slight_smile:

Alternatively, after getting it to work, I noticed that this thread (http://forum.unity3d.com/viewtopic.php?p=225663) has C# code attached for a UniSciTE wrapper that could be modified for use with gVim.

I hope this is helpful to someone. gVim is a great editor :slight_smile:

240501–8605–$main_103.cpp (1.52 KB)

Okay - I’ve been working to get this fixed for a while. I’ve attached my slimmed down version of things that doesn’t rely on as much windows cruft. Thus it might work for OSX too. I compiled this using msys’s g++ in Windows since I find Visual Studio painfully bloated ( get the installer/downloader here here and install the g++ package)

… ah screw that, forum doesn’t let you upload .cc - whut?

Here’s a cut and paste instead:

#include <windows.h>
#include <sstream>
#include <string>
#include <iostream>
#include <algorithm>

int main(int argc, char** argv) {
     std::stringstream a;
	 
	 a << "--remote-tab-silent ";
	 
	 if (argc > 1) {
		a << "\"" << argv[1] << "\"";
	 }

	 if (argc > 2) {
                std::string str(argv[2]);
                a << ":" << str.substr(6, 14);
	 }
	 
	 ShellExecute(NULL, "open", "C:\\Program Files (x86)\\vim\\vim73\\gvim.exe", a.str().c_str(), NULL, true);
     return 0;
}

But here comes the important bit. Since Unity preferences don’t give you anyway to specify the command line used to open a file, and by default it just sends the file name with no line information, you need to trick Unity into thinking it is using UniSciTE… otherwise it won’t add the “-goto:” bit. To do that, just copy the vimlaunch program into your Unity\Editor\Data\Tools\UniSciTE directory. Rename UniSciTE.exe to UniSciTE.exe.old (so you can still revert later) and rename vimlaunch to UniSciTE.exe.

Hope that helps people to use the best text editor to ever exists. ;p

oops, update the following if block so that it ignores the stupid -1 line number (why??) that Unity sends when opening a file from the project tree.

 if (argc > 2) {
        std::string str(argv[2]);
        std::string lineno = str.substr(6, 14);
        if (lineno != "-1") 
            a << ":" << lineno;
 }

this sounds great guys! i would really like to use this but unfortunatley have no idea how to manually compile it on windows.

could someone tell us the commands used to compile it with miniGW on windows?

or even better, would some kind person upload a compiled version? (windows 7 64 bit)

thanks,
meta~

I am not too familiar with Windows programming environment / windows console, but can this code be written in some scripting language in Windows comparable to bash?
Just curious, if I have time, I’ll make solution that does not require compilation.

Since Unity stopped sending filename/line numbers to any editor other than MonoDevelop, I had to replace MonoDevelop.exe with my own wrapper (code below).

#include <windows.h>
#include <sstream>
#include <string>
#include <iostream>

int main(int argc, char** argv)
{
           	std::stringstream a;	
	   	a << "--remote-tab-silent ";	
		std::string s = argv[3];
 	 	std::string delimiter = ";";
		std::string fToken = s.substr(0, s.find(delimiter));
		std::string linToken = s.substr(s.find(delimiter) + 1, s.length() - 1);
		a << "\"" << fToken;
		if (linToken != "-1")
		{
			a << ":" << linToken  << "\"";
		}
		else
		{
			a << ":1" << "\"";
		}
	   	ShellExecute(NULL, "open", "C:\\Program Files (x86)\\Vim\\vim74\\gvim.exe", a.str().c_str(), NULL, true);
         	return 0;
}

I was unable to get the --remote-tab-silent flag to work with the file:line plugin (1.0). Might be doing something wrong myself, but at least Vim opens up the buffers at the appropriate line, so that works, I’ll check what I’m doing wrong later.

Looks like it’s working out of the box now? This is what I did, no wrappers or plugins

Edit->Preferences->External Tools
Browse for your gVim executable
External Script Editor Args: --remote-tab-silent +(Line) "(File)"

1 Like

So I’m having an issue where Unity doesn’t seem to want to remember that I want Gvim as my external editor. It keeps selecting Visual Studio. Any ideas?

Why don’t you use VsVim instead of gVim?
https://visualstudiogallery.msdn.microsoft.com/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329