Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On a Ubuntu installation I have both python-mode.el and the command in my .emacs file:

(global-set-key [f2] 'hippie-expand)

Thus keywords are highlighted and variables that I have defined already can be autoexpanded with [f2]. How can I get the autoexpand to work with the python keywords? For example, given the code

for x in enumera

By pressing [f2] how can I get this to expand to

for x in enumerate

(enumerate is a known keyword). Since I'm learning emacs, the more detail in the answer, the more it would help!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

First, you might prefer to use one of the packages auto-complete or jedi.

That said, hippie-expand works by trying to expand the current word using a list of functions, stored in the variable hippie-expand-try-functions-list (to view its contents, press C-h v hippie-expand-try-functions-list RET). For instance, the first function in the default value for that variable tries to expand a filename. hippie-expand asks for an expansion for each function in turn, moving to the next when a function has exhausted all of its possible completions. (That is, you may keep pressing [f2] to get more expansions.)

When you expand the name of a variable that already appears in your python code, hippie-expand is using the function try-expand-dabbrev, which searches for the current word in the current buffer. Another function on that list, try-expand-dabbrev-all-buffers, searches for the current word in all open buffers. Thus, a quick (and dirty) solution to your problem would be to create a file with all keywords and built-ins (enumerate is a built-in), and keep that file opened in emacs. That is, if you insist on using hippie-expand; clearly the two packages I mentioned before seem like better solutions if you can get them to work.

share|improve this answer
    
Thanks for the suggestions. I'll probably end up using one of the packages, but learn quite a bit from trying to do it myself! –  Hooked Jun 13 '13 at 23:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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