3

Vim can open a file under cursor using gf. For example, if I have the following under my cursor:

SensorManagementActivity.java

Hitting gf will open SensorManagementActivity.java.

The problem is that in Java, the references lack the java suffix, and often appear as SomeClass, SomeClass() or SomeClass.method().

  • How do I open SomeClass.java and jump to someMethod() when the cursor is on SomeClass.someMethod() in another file?
  • Is there a way to open a new file without saving the current one, and going back to the current one without losing changes?
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Adam Matan
  • 128,757
  • 147
  • 397
  • 562

1 Answers1

6

The 'suffixesadd' option allows gf to handle Java file extensions; it is already set by the java filetype that ships with Vim, like this:

:setlocal suffixesadd=.java

To jump to methods, Vim can use a tags file that must be (re-)generated first (there are plugins that can automate that). For Java, you can use the exuberant ctags tool.

:! ctags -R

For more information and alternatives, read :help ctags. Use the :tag command or the Ctrl-] shortcut to jump.

You can jump to a split window via Ctrl-W ]. To be able to leave a modified file and return back to it later, :set hidden in your ~/.vimrc.

PS: Though here they're kind of related, it's best to avoid asking multiple questions at Stack Overflow

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • +1 Thanks a lot for the answer. `:! ctags -R` does not work for me yet - trying to find a workaround. – Adam Matan Sep 23 '12 at 13:18
  • You may have to install `ctags`, it is not included in Vim. Also, the non-exuberant plain version does not understand Java. You can also invoke it directly from the console; after a successful run, there should be a file named `tags` in the current directory. – Ingo Karkat Sep 23 '12 at 15:04
  • `-R` is just for recursive processing of subdirectories, which is helpful, but not required. But I haven't come across an exuberant ctags that doesn't have that option, maybe you just have the plain ctags. Check the output of `ctags --help`, and install exuberant ctags (5.9 is current) if necessary. – Ingo Karkat Sep 23 '12 at 15:26