如何在Vim实现go to definition?

如果在views.py文件中有一处用到了User类,当cursor在User()下面的时候请问有什么方式能快速跳转到models.py下面的User类的实现处?

# views.py
from models import User

u = User()

使用CtrlP打开一个文件然后跳到相应的实现处的方式还是太慢了,在阅读大一点项目的源代码的时候这样思维很容易被打断。

查到了一款插件tagbar,通过:TagbarToggle<CR>可以起到下面这样的效果,但是这样的话还是只能浏览到本文件下面的所有class以及function。
68747470733a2f2f692e696d6775722e636f6d2f5366394c7332722e706e67

答案

jedi-vim

Completion <C-Space>
Goto assignments <leader>g (typical goto function)
Goto definitions <leader>d (follow identifier as far as possible, includes imports and statements)
Show Documentation/Pydoc K (shows a popup with assignments)
Renaming <leader>r
Usages <leader>n (shows all the usages of a name)
Open module, e.g. :Pyimport os (opens the os module)


ycm

The GoToDeclaration subcommand
Looks up the symbol under the cursor and jumps to its declaration.

Supported in filetypes: c, cpp, objc, objcpp, cs, go, python, rust

The GoToDefinition subcommand

Looks up the symbol under the cursor and jumps to its definition.

NOTE: For C-family languages this only works in certain situations, namely when the definition of the symbol is in the current translation unit. A translation unit consists of the file you are editing and all the files you are including with #include directives (directly or indirectly) in that file.

Supported in filetypes: c, cpp, objc, objcpp, cs, go, javascript, python, rust, typescript

The GoTo subcommand

This command tries to perform the "most sensible" GoTo operation it can. Currently, this means that it tries to look up the symbol under the cursor and jumps to its definition if possible; if the definition is not accessible from the current translation unit, jumps to the symbol's declaration. For C/C++/Objective-C, it first tries to look up the current line for a header and jump to it. For C#, implementations are also considered and preferred.

Supported in filetypes: c, cpp, objc, objcpp, cs, go, javascript, python, rust

阅读 10k
4 个回答

个人推荐 jedi-vim
它有几大功能:

  • 补全

  • 跳转到定义

  • 显示帮助文档

  • 重命名

使用感觉是性能跟得上去!

分享一下, 本人使用的:

  1. taglist: 第三方插件了, 觉得和你的应该差不多.

  2. ctags -R * # 这个需要运行在源文件的目录里面, 然后会生成一个tags的文件,里面包含了当前项目的所有符号. 某些IDE的跳转就是借用这样的文件. 然后,跳转就和vim帮助文档中的Ctrl+]一样了,回退Ctrl+t.这个你应该知道. 如果文件没有打开, 也会跳转的. 唯一的缺点, 就是不能实时更新. 但是, 你可以设置一个快捷,来更新这个文件即可.

我和10086用的一样,用ctags,就是不知道python能用不,我是用C。

居然没有人推荐 YouCompleteMe ?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题