Setting up vim autocomplete for the Go programming language

huangapple go评论99阅读模式
英文:

Setting up vim autocomplete for the Go programming language

问题

我对人们如何设置vim的自动补全功能很感兴趣,并且正在寻求相关建议。

关于我的原始问题,我知道我可以直接访问以下网址:

https://github.com/nsf/gocode

复制他们提供的文件,然后开始使用vim的自动补全功能。然而,我想知道其他人的想法,以及Go社区是如何设置vim的自动补全功能的。

此外,我已经按照那里发布的说明进行了操作,但我仍然无法使vim中的自动补全功能正常工作。因此,我正在寻找其他替代方案或者关于如何使其正常工作的想法。不确定问题出在哪里。目前,当我尝试自动补全时,它只是写出单词" PANIC",而不是显示自动补全的选项。

原始问题:

我试图设置我的vim,使其能够自动补全Go编程语言,然而,我按照以下页面中的说明进行操作:

https://github.com/nsf/gocode

我不确定他们的意思,而且我有点担心如果操作不当可能会损坏我的vim设置。

首先让我困惑的是它说:

安装官方的Go vim脚本,路径为$GOROOT/misc/vim。如果你已经安装了,继续进行第二步。

然而,我不确定那是什么意思。我在终端中进入了该目录并阅读了readme.txt文件,它说如何激活语法高亮,但我已经有了。这就是我需要做的吗?

在第二步中,它说:

执行以下命令:

vim/update.sh

他们实际上提供了update.sh的代码,但我不确定cp命令的开头是什么意思,即:

#!/bin/sh
mkdir -p "$HOME/.vim/autoload"
mkdir -p "$HOME/.vim/ftplugin/go"
cp "${0%/*}/autoload/gocomplete.vim" "$HOME/.vim/autoload"
cp "${0%/*}/ftplugin/go/gocomplete.vim" "$HOME/.vim/ftplugin/go"

但是${0%/*}部分是什么意思?即使我知道update.sh是什么,我应该在哪里运行它,因为vim/update.sh是相对路径?

我知道update.sh要求我将一些文件复制到$HOME/.vim/ftplugin/go和$HOME/.vim/autoload,但是我甚至在~目录下使用find命令也找不到这些文件,所以我不确定要复制什么。我知道它们应该放在哪里,但不知道文件在哪里。有人知道这些文件在哪里,或者有关如何使vim自动补全Go的更简单或更详细的解释吗?

提前感谢您的帮助! Setting up vim autocomplete for the Go programming language


我发现了一些问题,不确定是否正常,但在$GOROOT/misc/vim/ftplugin/go目录下根本没有gocomplete.vim文件。我有其他一些看起来不相关的文件,如fmt.vim、import.vim和test.sh文件。

但奇怪的是,在$GOROOT/misc/autoload目录下,我没有gocomplete.vim文件,而是有一个complete.vim文件。不确定它们是否相同,或者为什么文件名会改变。无论如何,如果有人设置了他们的go自动补全并且愿意分享他们的设置,以及他们遇到的差异是什么,我会很感谢的!


问题的补充

我也对其他人如何设置vim中go的自动补全功能感兴趣。欢迎分享!

英文:

EDIT:

I was generally interested to know how people had their go autocomplete set up for vim and was looking for advice on it.

Related to my original question, I think I know I can just go to:

https://github.com/nsf/gocode

copy the files that they provide there and just start using the vim autocomplete. However, I wanted to know what people thought and how the go community has the vim autocomplete set up.

Also, I have followed the intructions as were posted there and I still cannot make the autocompletion work for my go in vim. So I am looking for other alternatives or ideas on how to make it work. Not sure what the problem is. Currently when I try to autocomplete it simply writes the word PANIC instead of showing me the options for autocompletion.

ORIGINAL:

I was trying to set up my vim such that it could auto complete the Go programming language, however, I was following the instructions in the following page:

https://github.com/nsf/gocode

and I was not sure what they meant and I was a little scared of maybe changing my vim set up in a way that might be damaging by doing it wrongly.

The first thing that confused me is it says:

Install official Go vim scripts from $GOROOT/misc/vim. If you did that already, proceed to the step 2.

However, I was not sure what that even meant. I did go to that directory in my terminal and read the readme.txt file and I it said how to activate the syntax highlighting which I already had anyway. Is that everything I have to do for that step?

On step 2 it says:

do:

vim/update.sh

They actually provide the code that update.sh is but I was not sure what the beginning of the cp command meant i.e. its:

#!/bin/sh
mkdir -p "$HOME/.vim/autoload"
mkdir -p "$HOME/.vim/ftplugin/go"
cp "${0%/*}/autoload/gocomplete.vim" "$HOME/.vim/autoload"
cp "${0%/*}/ftplugin/go/gocomplete.vim" "$HOME/.vim/ftplugin/go"

But what does the ${0%/*} part do? and even if I know what the update.sh is, where do I even run this, since this vim/update.sh is done at a relative path?

I know update.sh wants me to copy some files to $HOME/.vim/ftplugin/go and $HOME/.vim/autoload, but I even did a find from ~ and couldn't find such files, so I am unsure on what to copy. I know where it should go, but not where the file even is. Does someone know where those files are or an easier or more detailed explanation on how to make vim auto-complete for go?

Thanks for the help in advance! Setting up vim autocomplete for the Go programming language


Some of the problems that I have discovered that I have, not sure if its expected, but in the $GOROOt/misc/vim/ftplugin/go I do not have the gocomplete.vim file at all. I have other stuff that seems irrelevent like an fmt.vim import.vim and a test.sh file.

But the odd thing is that at $GOROOt/misc/autoload I do not have the gocomplete.vim file but I instead have a complete.vim file. Not sure if that the same or why the name of the file would have changed. Anyway has their go autocomplete set up and mind sharing what they have and if they know what the differences might be with what I have encountered? Thanks!


ADDITION to Question

I am also generally interested in how other people have their auto-complete set up for their go in vim. Feel free to share that too!

答案1

得分: 3

你已经执行了update.sh命令吗?我非常有信心它会起作用。

你的所有Vim配置都存储在~/.vim/~/.vimrc~/.gvimrc中(在Vim 7.4中,你也可以将后两个文件放在第一个目录中)。要备份你的Vim配置,只需将它们存储在其他地方(或将所有的dotfiles放在版本控制下,像许多人现在所做的那样)。

${0%/*}操作脚本的文件名($0),就像dirname一样:它截取掉脚本文件名本身(从*到最后一个/的所有内容)。通过这样做,你可以从任何地方调用脚本,例如$GOROOT/misc/vim/update.shcd misc; vim/update.shcd misc/vim; ./update.sh

该脚本还确保autoloadftplugin子目录存在,并在它们不存在时创建它们。试试看吧!

英文:

Have you executed the update.sh command already? I'm pretty confident that it will work.

All of your Vim configuration is stored in ~/.vim/, ~/.vimrc and ~/.gvimrc (with Vim 7.4, you can put the last two also inside the first directory). To backup your Vim configuration, just store those somewhere (or put all of your dotfiles under version control, as many now do).

The ${0%/*} manipulates the script's file name ($0) like dirname does: It cuts off the script file name itself (everything at the end * until the last /). With this, you can invoke the script from anywhere, e.g. $GOROOT/misc/vim/update.sh or cd misc; vim/update.sh or cd misc/vim; ./update.sh.

The script also ensures that the autoload and ftplugin subdirs exist, and creates them if they don't yet. Just give it a try!

huangapple
  • 本文由 发表于 2014年1月28日 15:43:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/21399610.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定