How to get code completion in emacs?

The basic way that will work across any sort of buffer in emacs is dynamic abbreviation used by hitting M-/ (aka alt+/)

That will expand text to duplicate other strings already existing in your current buffer or in other buffers currently open.

For more extensive code completion that is language specific for C++ or Java (or others), you'll want to look at a code templating or snippet package. There are some discussed in this related question on StackOverflow. One that is commonly mentioned in these discussions is yasnippet -- there's a screencast demo in this post on an emacs blog.


@Doug's answer is a little out of date and his focus is on code expansion by using pre-defined snippets.

I think the OP's question is about intellisense (auto complete method name of a class, for example).

  1. hippie expand used to be popular, the syntax analyzer is emacs tags. So it supports all languages ctags supports (including C++,Java). But the result is not perfect because ctags only uses regular expression to analyzed the code. See my init-hippie-expand.el for details. You could start by checking the value of hippie-expand-try-functions-list

  2. auto-complete is a modern one with fancy UI (dropdown menu, even in console window). If you use clang as a backend. The intellisense is much better. To get the clang working, you need install auto-complete-clang which is the plugin of auto-complete. It only supports the language clang supports (C/C++/Obj-C). As Sylvain Benner pointed out, you can use emacs-eclim as the java parsing back end which actually calls the executable of eclipse. So auto-complete can support java. See my init-auto-complete.el for details.

  3. semantic provides both the UI and the back end parsing engine. It's slow. The dropdown UI is OK but require X window. The intellisense result is better than ctags. And the documenation is too geeky for me. See init-semantic.el for details.

  4. company-mode is similar to auto-complete, the difference is it packages everything into one bundle. For example, C++ developers only need install company-mode (auto-complete need some 3rd party plugin). Another UI difference is the company will show the candidate automatically. So you press less keys in company. It's possible to do the same thing in auto-complete after customization.

In summary, auto-complete, company are most popular two choice. Semantic and hippie-expand are also welcomed but less used. Others are too trivial to mention and their features are already covered by the big four. For example, auto-complete could use yasnippet as input source (auto-complete provide the UI, but the actual candidates are produced by yasnippet back end).

I suggest using either auto-complete or company as major code completion plugin and either semantic or hippie-expand as complementary plugin.

I use company-mode and hippie-expand because,

  • Now (year 2017) company-mode is more actively maintained than auto-complete and has more 3rd party plugins (for example, latex plugin)

  • hippie-expand is not very active but it does not occupy TAB key which company-mode/semantic/auto-complete is using

If you use company-mode for C++/Java, you may want to modify company-backends.

A few more tips on company-mode,

You may need tweak the value of company-backends. For example, suppose its value is (company-eclim company-semantic company-clang (company-dabbrev-code company-gtags company-etags company-keywords)). Then if company-eclim can get non-zero candidates, company thinks you are writing java, so it will NOT run backends after company-eclim.

Now let's look (company-dabbrev-code company-gtags company-etags company-keywords). All these four backends will be used together. For example, even after company-dabbrev-code have fetched non-zero candidates successfully, the other three backends are still executed.

In real world, it's often necessary to re-adjust the order of backends or add new backend (company-rtags, for example).


Short answer: I know none for emacs that is useful for java. Dynamic expand does not care for the libraries in your class path, it does not know anything about the code you're typing it can only expand to text that already exists in one of the buffers. JDE and emacs-eclim try to provide this functionality but personally I was not impressed.

Tags:

Emacs