jim jarnac
jim jarnac

Reputation: 5152

Vim and python - jump to definition key binding

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.

Upvotes: 11

Views: 14438

Answers (4)

Hazem Elmahy
Hazem Elmahy

Reputation: 111

If you're using YouCompleteMe there is a command for that

:YcmCompleter GoToDefinition

if you want to add a shortcut for doing that in a new tab

nnoremap <leader>d :tab split \| YcmCompleter GoToDefinition<CR>

Upvotes: 2

daliusd
daliusd

Reputation: 1165

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).

Upvotes: 2

Meitham
Meitham

Reputation: 9670

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"

Upvotes: 7

Ingo Karkat
Ingo Karkat

Reputation: 172570

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.

Upvotes: 5

Related Questions