views:

681

answers:

7

Vim's Ctrl-n generally works like this: I type few letters, hit Ctrl-n, and Vim provides me with completions based on words in my all opened buffers.

Solution for Emacs doesn't have to be identical. I mainly use it like this: declare variable, then use it in later code. But I like the lightweight approach of not parsing the source code.

+2  A: 
;; Allow tab to autocomplete

 (defun indent-or-expand (arg)
   "Either indent according to mode, or expand the word preceding point."
   (interactive "*P")
   (if (and
        (or (bobp)      (= ?w (char-syntax (char-before))))
        (or (eobp) (not (= ?w (char-syntax (char-after))))))
       (dabbrev-expand arg)
     (tab-to-tab-stop)))

 (defun my-tab-fix ()
   (local-set-key [tab] 'indent-or-expand))

 (add-hook 'as-mode-hook         'my-tab-fix)
 (add-hook 'java-mode-hook       'my-tab-fix)
 (add-hook 'c-mode-hook          'my-tab-fix)
 (add-hook 'sh-mode-hook         'my-tab-fix)
 (add-hook 'emacs-lisp-mode-hook 'my-tab-fix)
+6  A: 

try hippie-expand, bound to your favorite key

(global-set-key (kbd "M-/") 'hippie-expand)

Instead of presenting a completion-list, repeatedly hitting the bound-key cycles through the completions in-place.

Why "hippie"-expand? I have no idea, and I actually avoided looking at the function because the name was uninformative and off-putting, until I read the write-up at 'Life Is Too Short For Bad Code'. (The EmacsWiki entry on hippie-expand also asks "why 'hippie?'" but can't answer it, either.)

Michael Paulukonis
+9  A: 

You want dabbrev-expand, bound to M-/ by default. I haven't used Vim, but from your description, it does the exact same thing.

Vladimir
It's worth noting too that if M-/ doesn't give what you want the first time, pressing it repeatedly will cycle through the matches.
Boojum
+2  A: 

Possibly useful existing SO question: http://stackoverflow.com/questions/129257/eclipse-sytle-function-completions-in-emacs-for-c-c-and-java.

dmckee
A: 

I personally use AutoComplete It gives you a nice dropdown box. You can select how many letters you want to type before it activates and customise what you want to show up, including stuff in dabbrev-expand.

A: 

The matter, in my opinion is that emacs completion I tryed doesn't complete regarding the context.

For instance, if you write some OOP with a method foobar() and an argument foo, M-/ will suggest you both foo and foobar.

But it would be great if you are calling an object method, not to provide just "foo" completion.

Has anyone a solution?

Aif
Not a big problem for me. Reliable completion for C++ is very hard.
phjr
A: 

Aif> This requires much more than what "hippie expand" has to offer. If you code C/C++ you COULD use ECB http://ecb.sourceforge.net/ but frankly, the project is quite dead and this addon is not very reliable. If you need really good intelligent completion you should try Eclipse (CDT). But if you code python then Emacs (rope + flymake) is just as fine as Eclipse (PyDev).