Seemy integration of Vim in Eclipse
Posted by: whaledawg on
Aug 2nd, 2009 |
Filed under: general
So in my last post I mentioned that I want to start doing mobile apps and I chose to work with Android(I’ll explain why later). And Android has proven to be great, but developing in it has forced me to use Eclipse, which is…less than great.
Now when I say forced it’s not like anyone from Google came to my house and threatened me but all the learning material I’ve come across, from the free stuff on the web to published books to what Google puts out themselves, assumes that you are using it. There are some explanations of how to do things without Eclipse but when you’re learning something you should always try as best you can to mirror the environment of who ever’s teaching. It helps narrow down the origin of errors.
And when I say it’s less than great I don’t mean it’s bad. I just don’t like IDEs, and for 2 main reasons. They hide things I should know from me and force me to do things their way. As an example of the first one, Eclipse has this nice feature that allows you to hit CTRL-SHIFT-o and it will automatically import any packages you need for a java file. Very convient. Except now I don’t know what packages corrolate with which objects. Even the most dynamic of languages force you to import foreign code expliciatly, that tells me that this is information the programmer should know.
As an example of the second one, it’s a pain in the ass to edit files in Vim. Every other text editor is notepad to me. I tried googling around and there are a few ways. The first if viPluggin, which seems good but it’s not free and to be honest I’m going to try and ditch Eclipse as soon as possible. Theres vimpluggin which I installed but it doesn’t work right/well. I couldn’t get arguments to Vim and I kept having to right-click and open-as to switch between Vim and the native text editor. And cosidering I sent a question to the support list and was promptly offered a deal on pills to make it ‘Thicker, longer, better.’ I’m going to assume it’s not supported anymore. I didn’t try Eclim but that seemed to be going backwards, adding Eclipse features to Vim. I need to make sure my developement environment is as close as possible to standard.
So what am I really looking for? I’d like to be able to edit files with full Vim functionality(including using my .vimrc file) and use the full feature set of Eclipse at the same time. And I’d like to be able to jump from one to the other seemlessly. That I did not accomplish(hence the title) but I have a hybrid approach that comes pretty close.
HowTo
So my two pronged approach involves installing VRapper so I can quickly switch to a Vim like navigation scheme within Eclipse. 4 keyboard strokes and I’m there. But it doens’t have the full functionality I need or take rc files(I can’t live without the elflord) so I used Eclipses built in ability to use an external editor. It’s a bit slower to start and switch back and forth but it works adequatly.

Setting up VRapper
VRapper is an open source project that is constantly evolving and adding new features, but currently missing quite a few. Still, being able to navigate with the home keys and yank lines makes installing this worthwhile.
First, you have to add the repo path to your software updates. Click on Help->Software Updates:
You should the software window. Click on the tab Available Software and the button Add Site:
You’ll get a popup window that let’s you…add a sight. Go ahead and put in http://vrapper.sourceforge.net/eclipse-update-site
Then click OK and you’ll have this repo added to your list. Select Vrapper and then press Install
Then just click through until it starts installing and restart. You should now notice a new button on your toolbar:
Pressing the button toggles Vim edditing mode on and off. Congratulations, you now have some vim functionality available to you. However, it’s still kind of a pain to use. You have to close whatever you’re working on, click the button then open it again. I could open it in Vim for that much effort.
So what we’re going to do is set up a hot key to toggle Vim mode, then we can switch back and forth from VRapper to native Eclipse editing with a few keystrokes.
First we need to open up the preferences. Go to Window->Preferences
Then expand General, select Keys and type vrapper into the filter.
Then type any key combo you want into the binding box(I chose VV) and done. Now typing those keys toggles Vim mode on and off. You still have to close and reopen the file, but I can do entirely from the keyboard(CTRL-W closes, F3 opens a file). So I type CTRL-W SHIFT-v SHIFT-v F3 and I can jump from one mode to the other. There’s probobly a way to macro that all into one keypress for Eclipse but that’s fast enough for my needs.
However, VRapper is still missing a lot of Vim functionality. No macros, no rc files, etc. So I also want to be able to open files from Eclipse directly in gVim.
Setting up gVim
This is actually pretty easy using Eclipse’s ability to call external editors. If I could find a way to do this with keystrokes instead of using the mouse I wouldn’t even bother with VRapper.
optional
I don’t call gVim directly from Eclipse because I want it to behave slightly differently when I’m editing files from Eclipse. This means I set up a little bash script in my home directory that looks like this:
/usr/bin/gvim -U /home/whaledawg/.vimeclipserc -- $1
That calls gVim and gives it an rc file I use just for Eclipse. The rc file looks like this:
set noswapfile
set nobackup
set noswapfile
set nobackup
Note: if you do this then the bash script must be named gvim. gVim checks the name of whatever calls it and it must be gvim or it won’t work.
/optional
OK so again we need to go to Window->Preferences, expand Gernal but now we’re going to choose File Associations. If you click add you can add new file associations here(you may need to do this for *.java files) and if you select a file type you can see all the associated editors in the bottom window:
Clicking Add(next to the associated editors) allows us to add a new editor to that file type(these are the editors that appear when you right click a file in Eclipse and choose “Open with”) From the pop up window select External Programs and then click Browse. If you created my bash script above, then you would browse to tha script(make sure it’s permissions are executable) or if not you will browse to wherever gvim is on your system.
Now if you right click on a file of that type and mouse to Open With you’ll see gvim as an option. Hurray.
There’s still a minor problem though. If you have a file open in Eclipse and open it again with gVim it will either not update the changes to Eclipse or will give you an annoying prompt. Let’s fix that.
Again under Window->Preferences->General, we’ll find Workspace. You don’t need to expand Workspace, just select it and you’ll see some options. Check Refresh Automically and it will automatically update your open files in Eclipse if you edit them with an external editor.
Click OK and that’s it.
Now you have a quick way to get most of the functionality in Vim and a slightly more cumbersome way to get the full functionality. While far from perfect, this approach has worked for me so far, at least until I’m able to dump Eclipse completely(which I hope to do in the near future).










Be the first!
Tags: 