Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up vim autocomplete for the Go programming language

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! :)


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!

like image 351
Charlie Parker Avatar asked Jan 28 '14 07:01

Charlie Parker


People also ask

How do I set autocomplete in Vim?

Enter into the vim command mode by pressing the *Esc* key and Enter the below command. Now press the *Enter* key. Autocomplete is enabled for Javascript. .

Is Vim good for Golang?

vim-go offers most of the features you may need when developing in Go, like syntax highlighting and folding, autocompletion support via gopls, code linting, quick execution using :GoRun , and more. Next, you'll need to install all the necessary binaries using the :GoInstallBinaries command.

Is NeoVim better than Vim?

The plugin architecture of NeoVim is a lot better than Vim. Apart from plugin implementation in Vimscript (VimL), we can also use the Lua programming language. In addition, NeoVim has a lot more powerful plugins, which are not compatible with Vim.

How to autocomplete the word in insert mode how does Vim decide which word to suggest?

Autocomplete word You'll have to press Ctrl + y or Enter key to choose a different completion text. If you were satisfied with the first match, typing any character will make the popup menu disappear and insert whatever character you had typed. Or press Esc to select the first match and go to Normal mode.


1 Answers

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!

like image 84
Ingo Karkat Avatar answered Oct 13 '22 07:10

Ingo Karkat