Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of "vim: set ft=cpp:" or "-*- c++ -*-" for Eclipse?

There are occasions when there is some code in a file that does not have any extension. Such files will have code written in a single language, but each such file may be written using a different language (like C, C++, assembly language, wiki markup, and HTML for example). Similarly, the problem also happens when there is a common extension in the file name, but different files use different programming languages.

The problem I want to solve is to avoid making each individual developer that has checked out a workspace from having to figure out what kind of file a source file is (there is either no extension, or the extension does not imply the source language used). To do so, the author of the file has the responsibility of putting the right magic in the file to let the file be opened properly. Since Eclipse has the ability to disable the formatter based on some embedded text in the file, I thought it should be possible to specify the file type as well. The Open With feature allows an individual developer to change how a file is interpreted, but must be done again each time a new workspace is checked out.

In Emacs or Vim, I can place magic strings at the top of the file to indicate how I want the file to be rendered within the editor. For example:

/* -*- c++ -*- */
/* vim: set ft=cpp: */

Is there some equivalent magic for Eclipse? My attempts at finding a solution constantly pointed out how to disable the formatter. Clearly, my inexperience with Eclipse is capping my Google-Fu.

like image 719
jxh Avatar asked Nov 05 '13 03:11

jxh


Video Answer


3 Answers

If there is no file extension, then things are a little harder. Instead of double-clicking to open the file, right-click -> Open with... -> other -> C++ editor. This will remember your choice for that file only.

ref:Quick way to set custom syntax highlighting in Eclipse

like image 58
Sach Avatar answered Nov 14 '22 23:11

Sach


Of all the free plugins providing Vim functionality within Eclipse that are available as of 8 November 2013, namely

  • Vrapper
  • ViPlugin
  • Vimplugin
  • Eclim

...none seem to support modelines. Nevertheless, a Vrapper user asked more than a year ago if such support existed.

Note that Eclim is a special case. It might solve your problem since it allows you to use Vim as an embedded Eclipse editor. However, from within the Vim instance, Eclipse's key bindings will not work. I guess it's a trade-off between access to features and convenience. From Eclim's website:

Please be aware that the embedded vim does not behave like a standard eclipse editor. It's a separate program (vim) embedded into eclipse, so eclipse features are provided by eclim's vim plugins and not the usual eclipse key bindings, context menus, etc. For those that just want vim like key bindings in their eclipse editors, vrapper is an excellent alternative which provides exactly that.

There used to be a commercial plugin called Viable but development seems to have stalled since 2011. Also, some users report that it breaks under Juno.

Finally, although your question is about Eclipse, it turns out NetBeans does have a plugin that supports modelines. It's called jVi. Another plugin for Netbeans is VIEX, in case you're interested.

Hope that helps!

Sources:

  • What vim plugins are available for Eclipse?
  • What are some good plugins for developing Java in VIM?
like image 20
HQCasanova Avatar answered Nov 14 '22 22:11

HQCasanova


I'm not aware of any Eclipse plugin able to deal with vim-style modelines. However, AnyEdit gives you a pretty precise control over tab size and related prefs. And you can always search the marketplace.

like image 30
romainl Avatar answered Nov 14 '22 23:11

romainl