Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generally, how do I "go to definition" in VIM? Then how do I with golang?

Tags:

vim

go

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?

like image 697
JnBrymn Avatar asked Dec 16 '22 02:12

JnBrymn


2 Answers

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 CTRL-]` should works well.

Also check two useful plugins Tagbar and Easytags.

like image 171
dyng Avatar answered Feb 22 '23 23:02

dyng


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.

https://github.com/fatih/vim-go/blob/master/doc/vim-go.txt

like image 41
Breno Leitão Avatar answered Feb 22 '23 23:02

Breno Leitão