32

I'm not looking for an "Vim Plugin" for Eclipse. Instead, I'd just want a keyboard-shortcut to open the current Eclipse file within a new Vim instance.

Is there a quick and easy way to do that?

cmcginty
  • 113,384
  • 42
  • 163
  • 163

3 Answers3

53

There's two ways to do this, the canonical way, and the hack. First:

The Canonical Way

  1. Window > Preferences... Fold out General > Editors > File Associations.
  2. Choose the file type you want to edit
  3. Click the "Add" button beside the "Associated editors" box.
  4. Click "External programs" and "Browse...", then find gvim, or enter /usr/bin/gvim, or generally make it go to gvim.

Now, files of that type should show up in an external vim instance. Awesome, right? But that doesn't solve your problem of pushing the current buffer out to vim.


The Hack

Instead, we're going to set vim as a "build tool", and have eclipse send it the current file as arguments. This may have some unaffected side-effects, based on your project settings, but look into them carefully if you experience things like unexpected re-building of your files.

  1. Run > External Tools > External Tools Configuration...
  2. Select "Program" on the left and click the "New" icon.
  3. Enter "Send to vim" for the Name
  4. Enter "/usr/bin/gvim" (or whatever your path is) for the Location
  5. Under "Working Directory" enter ${project_loc} (this is a variable representing your project's top directory)
  6. Under arguments enter ${resource_loc} (this represents your current resource's path)
  7. On the "Build" tab, un-check "Build before Launch" (unless you really want that)
  8. On the Common tab, check "External Tools" under the "Display in favorites menu"

You should be all set! Now you can send your file to vim by using the menu

Run > External Tools > Send to vim

If you want to get fancy, you can even add a button to your toolbar.

Be advised, I've used gvim in the examples. If you'd like to use terminal vim, you'll have to call it appropriately based on the terminal you're using. For xterm, this would be /usr/bin/xterm -e /usr/bin/vim, instead of /usr/bin/gvim

sleepynate
  • 7,926
  • 3
  • 27
  • 38
  • 11
    Step 6. `--servername ECLIPSE --remote-tab "${resource_loc}"` will keep all files from eclipse in the same window instance – cmcginty Nov 03 '10 at 22:46
  • 12
    You can make a keyboard binding with 'The Hack' method from: Window -> Pref -> General -> Keys -> Run Last Launched External Tool – cmcginty Nov 04 '10 at 02:29
  • Both things that are totally valid and I didn't even think of. Good work Casey. – sleepynate Nov 04 '10 at 14:01
  • 1
    cool, thanks a lot for the tip! BTW, I wouldn't call it a hack, it is using Eclipse as intended, isn't it? Casey, nice improvement, just let's add some quotes to support file names with blanks inside: --servername ECLIPSE --remote-tab "${resource_loc}" –  Nov 23 '11 at 11:29
  • Well, it's kind of a hack, since it's meant to be a run configuration (thus the necessity to tell it not to build) and the fact that eclipse refreshes the file with changed disk contents is really the only thing that makes it useful. – sleepynate Nov 23 '11 at 20:28
  • This along with Step 6 has worked very well for me. Round-tripping seems to work very well. – Traveler Mar 31 '12 at 14:29
  • 5
    Using `--remote-tab-silent` instead of `--remote-tab` eliminates the error message you get if you don't already have a Vim instance running with the ECLIPSE servername. – jmohr May 09 '12 at 15:31
  • I used the first approach with runVim (http://www.fastnlight.com/runvim/) and that got me to the right place (couldn't get the second way working on OSX) – Joe May 28 '12 at 19:16
  • For binding this to a particular shortcut, look here: http://stackoverflow.com/a/8176077/26510 – Brad Parks Jan 04 '13 at 14:54
  • @Joe You would use /usr/bin/mvim as the path in step 4, assuming you're using MacVim with the associated command-line tool – sleepynate Jan 04 '13 at 19:37
  • Step 5. I like to set up Working directory : ${container_loc} to set it to $resource_loc's directory. – Bathz Jul 22 '14 at 07:37
2

You can also create a small shell script that simply opens whatever options are passed to it in named vim instance. For example, the shell script I use is simply:

#!/bin/bash

gvim --servername eclipse --remote-tab-silent "$@"

Then make the script executable and follow the canonical method described by sleepynate, using your script as the external editor.

Stephen
  • 21
  • 1
1

I'm aware there is a highly upvoted answer but I should say that what I found most convienent on OSX was to use QuickCursor to let me open any text in macVim.

Joe
  • 4,367
  • 7
  • 33
  • 52