Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs - tab-completion of local Python variables

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.

like image 698
zak23 Avatar asked Apr 15 '09 04:04

zak23


5 Answers

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.

like image 59
Amol Gawai Avatar answered Nov 12 '22 22:11

Amol Gawai


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.

like image 26
dermatthias Avatar answered Nov 13 '22 00:11

dermatthias


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.

like image 10
boskom Avatar answered Nov 12 '22 23:11

boskom


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
like image 5
Dave Halter Avatar answered Nov 12 '22 22:11

Dave Halter


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?

like image 4
Shane C. Mason Avatar answered Nov 12 '22 23:11

Shane C. Mason