20

What is the easiest way to configure Emacs to be able to look up where a function is defined? I want to highlight it in Emacs and press a key combo to look up where the source of the function is.

Can you also find out where a specific function is being used?

This is easily done in PyCharm, but can it also be easily done in Emacs?

12

Did you try Elpy? It binds elpy-goto-definition to the key M-..
If you have a recent Emacs, I think you can also jump back with xref-pop-marker-stack (M-,). Elpy has some other nice features as well.

To install it, follow the instructions in the link.

11

I use Jedi mode for Python and I'm very happy with it. Not only does it do accurate look ups for function calls, it resolves functions to the correct definition even in complicated situations (like inheritance). Comes with auto complete to boot. http://tkf.github.io/emacs-jedi/latest/

8

The feature of PyCharm that you mention was one of my favourites, and one of the first things I looked for in trying to replace PyCharm completely with Emacs (which I am happy to say I have been able to do).

As mentioned in another answer the elpy package has this feature (in addition to many other useful IDE features). However, if you use elpy-goto-definition to look up the symbol at point, you will find sooner or later that in some cases where PyCharm would succeed in finding what you are looking for, elpy will not. As the docs say:

the backends can not always identify what kind of symbol is at point. Especially after a few indirections, they have basically no hope of guessing right, so they don’t

In this case, invoking elpy-goto-definition will do nothing and show a little error message in the echo area. When this happens, of course you still want to find what you are looking for, and elpy doesn't leave you hanging. In this kind of situation I always use C-c C-s (elpy-rgrep-symbol) to rgrep for the symbol at point in whatever project I am in. At least for me, the thing I am looking for more or less always shows up in the rgrep results. Actually, I think this is similar to what PyCharm does too: if you try to look up the definition of something and PyCharm is not sure where it is defined, it will have you choose from a list. I actually prefer elpy's implementation, because rather than a transient list that disappears when you go to the first option, elpy-rgrep-symbol will leave the list of possibilities open in another buffer so that you can jump back and forth between them if necessary.

Since my workflow is almost always to first call elpy-goto-definition and if it fails, call elpy-rgrep-symbol, I decided to hack together a simple function that just calls elpy-rgrep-symbol automatically if elpy-goto-definition fails. That function is below:

(defun goto-def-or-rgrep ()
  "Go to definition of thing at point or do an rgrep in project if that fails"
  (interactive)
  (condition-case nil (elpy-goto-definition)
    (error (elpy-rgrep-symbol (thing-at-point 'symbol)))))

Then I bind this to M-. (the default binding for elpy-goto-definition).

(define-key elpy-mode-map (kbd "M-.") 'goto-def-or-rgrep)

This way I can use one of my favorite PyCharm features in my favorite editor. I know this is kind of an old question and you may have already solved this problem or moved on, but I hope you or someone else finds this useful!

4

python-mode.el provides a command py-find-definition - see in menu Help.

2

M-. works in anaconda-mode (which I switched to after having problems with elpy).

Note that you need proper python environment, in my case I do the following: - mkvirtualenv someenv - python setup.py develop for all modules develop or use (or prefer to reach source instead of installed version) - M-x venv-workon someenv to enable paths in emacs.

Then I configure anaconda-mode to be active whenever I edit python and voila, M-. works

1

Good question, that isn't available out of the box and is a handy feature of full blown IDEs. I do that with cscope, with a nice helm interface: http://wikemacs.org/wiki/Python#cscope It can answers questions like "where is this symbol defined ? Where is it used ?".

ps: in evil-mode we can go to the definition of a symbol in the same file with gd.

  • I believe gd in evil only finds the first occurrence of the symbol in the file (despite the docstring: "Go to definition or first occurrence of symbol under point.") – Gordon Gustafson Jun 14 '16 at 2:27
0

M-. which is bound to elpy-goto-definition will do the trick. Use M-* will let you jump back to the previous location int he buffer. This allows you to quickly check something and go back to the previous location.

I also use C-c C-o often (elpy-occur-definitions). This will open a window with all class and function is the current buffer. From that buffer, pressing Enter will land you on the definition in the source code.

  • Actually, AFAICT, M-. is bound to xref-find-definitions and elpy-goto-definition is not on any key by default (although it is on menus). Finding definitions in site packages does not seem to work at all well. – Robert P. Goldman May 21 at 19:08
0

Jedi is an awesome autocompletion/static analysis library for Python.I used it a lot back to when I was using vim.

Just like the golang autocompletion engine(i.e. gocode) works both for vim and emacs, the same Jedi engine works both for vim and emacs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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