19

I know that emacs supports code completion. But to be honest I have never been a good FAQ reader, at least for something that I'm completely new to. So I wonder what I need to download and how to make so emacs can do some smart code completion for me in C++, and Java. Or is there any package which does code completion for many of major languages?

24

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.

  • +1. You might also want to give standard library complete.el a try. It is very old and little known, but the kind of in-buffer completion it provides can be useful. M-x find-library complete, then read the Commentary in the file. – Drew Oct 17 '16 at 13:30
20

@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).

  • which is the best auto-completion package in Emacs 24 for latex? – doctorate Nov 21 '14 at 19:44
  • @doctorate I think you need use auto-complete and its source emacswiki.org/emacs/AutoCompleteSources, but I'm a programmer instead of latex user, so you'd better double check at Google plus Emacs community, lots of academic guys there. – chen bin Nov 22 '14 at 1:06
  • please note note company-mode also supports latex. So I suggest company instead of auto-complete – chen bin Oct 17 '16 at 8:59
2

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.

0

Short answer: M-/

Long answer: RTFM :-)

  • 6
    +1, god i hope the next question i see is "need help using RTFM command" – rownage Jan 10 '11 at 16:38
  • @rownage: oh, thats ``info-emacs-manual'` (C-h r) – SamB Jan 10 '11 at 18:51
  • 1
    How about C-h k M-/ ? – Doug Harris Jan 10 '11 at 20:07

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.