python - 如何在Vim实现go to definition?
高洛峰
高洛峰 2017-04-18 10:17:47
[Python讨论组]

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

# views.py
from models import User

u = User()

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

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

答案

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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(4)
天蓬老师

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

  • 补全

  • 跳转到定义

  • 显示帮助文档

  • 重命名

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

迷茫

分享一下, 本人使用的:

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

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

黄舟

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

高洛峰

居然没有人推荐 YouCompleteMe ?

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号