Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim and python - jump to definition key binding

Tags:

python

vim

The following youtube video shows that it is possible to jump to definition using vim for python.

However when I try the same shortcut (Ctrl-G) it doesnt work... How is it possible to perform the same "jump to definition"?

I installed the plugin Ctrl-P but not rope.

like image 481
jim jarnac Avatar asked Jun 25 '17 01:06

jim jarnac


3 Answers

This does not directly answer your question but provides a better alternative. I use JEDI with VIM, as a static code analyser, it offers far better options than ctags. I use the spacemacs key-binding in vim so with localleader set to ','

" jedi
let g:jedi#use_tabs_not_buffers = 0  " use buffers instead of tabs
let g:jedi#show_call_signatures = "1"
let g:jedi#goto_command = "<localleader>gt"
let g:jedi#goto_assignments_command = "<localleader>ga"
let g:jedi#goto_definitions_command = "<localleader>gg"
let g:jedi#documentation_command = "K"
let g:jedi#usages_command = "<localleader>u"
let g:jedi#completions_command = "<C-Space>"
let g:jedi#rename_command = "<leader>r"
like image 153
Meitham Avatar answered Nov 03 '22 23:11

Meitham


Vim's code navigation is based on a universal database called tags file. It needs to be generated (and updated) manually. :help ctags lists some applications that can do that. Exuberant ctags is a common one that supports many programming languages, but there are also specialized ones, like ptags.py (found in your Python source directory at Tools/scripts/ptags.py).

Plugins like easytags.vim provide more convenience by e.g. automatically updating the tags file on each save.

The default command for jumping to the definition is CTRL-] (not CTRL-G; that prints the current filename; see :help CTRL-G), or the Ex command :tag {identifier}; see all at :help tag-commands.

like image 27
Ingo Karkat Avatar answered Nov 04 '22 00:11

Ingo Karkat


Some suggestions for people reading other answers to this question in the future:

  • tags file has one limitation. If in your code multiple objects has the same name you will have problem using ctrl-] as it will jump to first one and not necessary correct one. In this situation you can use g ctrl-] (or :tjump command or :tselect command) to get selection list. Potentially you want to map ctrl-] to "g ctrl-]"

  • It is possible that you want to have possibility to jump to correct object. In that case you might want to use jedi vim and if you are used to c-] you might want to use this mapping for jedi goto let g:jedi#goto_command = ""

  • Lastly you want to use universal ctags instead of excuberant ctags because of better new files support (not necessary python).

like image 22
daliusd Avatar answered Nov 03 '22 23:11

daliusd