30

Is there a good emacs mode that will allow tab-completion of local python variables? I set up ipython.el but it will only tab-complete things in the scope of the interpreter. I'm looking for something that will let me tab-complete tokens in the local namespace of a function or file.

6 Answers 6

17

M-/ runs the command dabbrev-expand . This will complete local names in any mode. Also I bind meta f1 to hippie expand from all open buffers. This is very useful for me.

;; Bind hippie-expand
(global-set-key [(meta f1)] (make-hippie-expand-function
                               '(try-expand-dabbrev-visible
                                 try-expand-dabbrev
                                 try-expand-dabbrev-all-buffers) t))

Hope this is useful.

12

I use emacs-autocomplete.el (version 0.2.0) together with yasnippet. Works ok for me, although it isn't a complete auto-completion environment like eclipse+java is. But enough for a common emacs hacker like me :)

1) Download autocomplete from here (first link) and put it in your load-path directory. Also download the extensions you want to use (Attention: Ruby and etags extensions need additional stuff). Put them also in yout load-path dir.

2) Download yasnippet and install it like the instruction on that page says (including the (require ...) part).

3) Put these lines in your .emacs file and edit them for your needs (like all the extensions you want to use):

(require 'auto-complete)
(global-auto-complete-mode t)

(when (require 'auto-complete nil t)
  (require 'auto-complete-yasnippet)
  (require 'auto-complete-python)
  (require 'auto-complete-css) 
  (require 'auto-complete-cpp)  
  (require 'auto-complete-emacs-lisp)  
  (require 'auto-complete-semantic)  
  (require 'auto-complete-gtags)

  (global-auto-complete-mode t)
  (setq ac-auto-start 3)
  (setq ac-dwim t)
  (set-default 'ac-sources '(ac-source-yasnippet ac-source-abbrev ac-source-words-in-buffer ac-source-files-in-current-dir ac-source-symbols))

For more informations on options see the auto-complete.el file.

4) Restart emacs or do a M-x load-file with your .emacs file. Write some code and press TAB for auto completion.

10

The blog post describing kind of tab completion you want can be found at Python code completion in Emacs. There is a bit of installing packages, pymacs, AutoComplete, rope, ropemacs, rope mode, yasnippet and setting up, but in the end I hope it will pay off.

2
  • 1
    I like ropemacs. It provide tab-completion of local Python variables!
    – gavenkoa
    Jan 3, 2012 at 22:46
  • auto-complete-mode is worth it even without the full rope+ropemacs+ropemode+pymacs setup
    – unhammer
    May 25, 2012 at 9:23
5

Use Jedi!

It really understands Python better than any other autocompletion library:

  • builtins
  • multiple returns or yields
  • tuple assignments / array indexing / dictionary indexing
  • with-statement / exception handling
  • *args / **kwargs
  • decorators / lambdas / closures
  • generators / iterators
  • some descriptors: property / staticmethod / classmethod
  • some magic methods: __call__, __iter__, __next__, __get__, __getitem__, __init__
  • list.append(), set.add(), list.extend(), etc.
  • (nested) list comprehensions / ternary expressions
  • relative imports
  • getattr() / __getattr__ / __getattribute__
  • simple/usual sys.path modifications
  • isinstance checks for if/while/assert
4

I think that you may be looking for something like this. It uses Pymacs and python-mode to do just what you are looking for.

Let us know how it works out for you?

1
  • I set that up, and it seems like it doesn't do completion of variables in the local scope - is it supposed to? Can you give me the exact steps to follow once in emacs to turn it on if there's something I need to do after installing it? thanks
    – zak23
    Apr 15, 2009 at 5:08
3

If you just want to get it up and running with minimal fuss, try the emacs-for-python package.

Happy coding!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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