Emacs - tab-completion of local Python variables
Asked Answered
H

6

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.

Holeandcorner answered 15/4, 2009 at 4:4 Comment(0)
M
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.

Meldameldoh answered 15/4, 2009 at 8:2 Comment(0)
N
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.

Neb answered 19/4, 2009 at 13:9 Comment(0)
W
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.

Westney answered 15/4, 2009 at 9:24 Comment(2)
I like ropemacs. It provide tab-completion of local Python variables!Brahma
auto-complete-mode is worth it even without the full rope+ropemacs+ropemode+pymacs setupTanana
S
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
Stapleton answered 10/1, 2013 at 8:31 Comment(0)
S
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?

Senzer answered 15/4, 2009 at 4:9 Comment(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? thanksHoleandcorner
S
3

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

Happy coding!

Surovy answered 8/6, 2011 at 15:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.