英文:
Generally, how do I "go to definition" in VIM? Then how do I with golang?
问题
首先,使用VIM时,要执行什么过程和按下哪些键才能"跳转到定义"或"跳转到声明"等操作?这个文档可能是我问题的答案,但我无法使其工作,所以我不确定。它似乎只是简单地匹配字符串而不是找到真正的定义。如果我能让它工作,那么我能否跳转到当前文档之外的定义/声明?或者它只能在单个文档内起作用?
其次,如何使其与Go编程语言一起使用?如果能够"点击"下面的Client
:
clnt := &http.Client{Transport: tr}
并跳转到定义http.Client的实际代码,那将非常方便。
可能吗?如何实现?
英文:
Two part question:
First, when using VIM what process do I take and what keys do I type to "go to definition" or "go to declaration" etc.? This document might be the answer to my question, but I can't get it to work, so I'm unsure. It looks like its merely text matching the string rather than finding the true definition. If I can get this to work, then will I be able to jump outside of the current document to a definition/declaration? Or does this only work within a single document?
Second, how do I make this work specifically with the Go programming language? It sure would be nice to "click" the Client
in
clnt := &http.Client{Transport: tr}
And be taken to the actual code that defines an http.Client.
Possible? How?
答案1
得分: 9
如你所猜测的那样,gd
(以及其他命令)只是文本匹配,vim并不理解语法,因为它只是一个文本编辑器。:h gd
会解释gd
的工作原理。
通常,通过使用CTRL-]
和标签文件来实现“转到定义”。关于这个主题的用户手册可以通过:h 29.1
来阅读。
首先,你需要为你的项目生成一个标签文件,最新的Exuberant Ctags已经支持golang(可以从这里获取),使用以下命令:
cd /path/to/your/project
ctags -f tags -R --fields=+K+a
就可以完成这项工作。
其次,打开vim,默认情况下vim会在工作目录下查找标签文件(根据'tags'
选项),如果成功找到标签文件,那么CTRL-]
应该可以正常工作。
英文:
As you guess, gd
(and other commands) is merely text matching, vim doesn't understand the syntax as it is just a text editor, :h gd
will explain how gd
works.
Usually, 'go to definition' is brought by using CTRL-]
and tag files. A user manual about this topic can be read by :h 29.1
.
First you need to generate a tags file for your project, as latest Exuberant Ctags has supported golang (from here), command
cd /path/to/your/project
ctags -f tags -R --fields=+K+a
will do the job.
Second, open vim, by default vim will find tag files under working directory (according to 'tags'
option), if the tag file is found successfully, then <kbd>CTRL</kbd>-<kbd>]</kbd>` should works well.
答案2
得分: 4
对于golang,你可以使用应用程序godef来实现。插件vim-go可以帮助你设置一切,所以你只需在定义处键入'gd',它就会跳转到确切的定义。
https://github.com/fatih/vim-go/blob/master/doc/vim-go.txt
英文:
For golang, you can use the application godef to do it. The pluging vim-go helps you on setting everything, so, you just type 'gd' in a definition and it goes to the exact definition.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论