Add GoLang syntax highlighting for VIM
Asked Answered
I

11

65

I'm trying to add Go language syntax highlighting to VIM on ubuntu with resources and direction supplied here http://go-lang.cat-v.org/text-editors/vim/.

Go comes with a go.vim file that contains syntax settings for VIM and the above page offers the following instructions

Place $GOROOT/misc/vim/syntax/go.vim in ~/.vim/syntax/ and put the following in ~/.vim/ftdetect/go.vim:

au BufRead,BufNewFile *.go set filetype=go 

This is more or less the same vein of procedure for customizing vim syntax I've seen elsewhere (Vim 7.3 on Ubuntu 12.10 doesn't have 'ftplugin' directory anywhere and https://github.com/jnwhiteh/vim-golang/blob/master/readme.txt)

So I think I'm doing the right thing when I create directories:
~/.vim
~/.vim/syntax
~/.vim/ftdetect

and follow the above instructions by adding

go.vim to ~/.vim/syntax/ and creating a file, go.vim, in ~/.vim/ftdetect/ which contains

au BufRead,BufNewFile *.go set filetype=go

Yet syntax highlighting does not seem to occur. Is there something I need to do to force VIM to look at these new settings files?

Intensity answered 22/3, 2013 at 21:5 Comment(5)
For whoever gets here from Google, nowadays there is this nice package for Vim: github.com/fatih/vim-go that wraps several goodies for Go.Commutative
Just wanted to document here that the official go documentation at code.google.com/p/go-wiki/wiki/IDEsAndTextEditorPlugins points to github.com/fatih/vim-go as well. It includes go.vim, if you didn't get it in your installation for whatever reason (e.g. you got it from a package repo that didn't include it) and is easy to install with Vundle.Aney
thx @Aney the code.googe now also points to github.com/golang/go/wiki/IDEsAndTextEditorPluginsRisteau
Also to Google arrivals, the page in the question is no longer updated, and (on my distribution at least) there is no go.vim file under {go}/misc/vimFargone
The /misc/vim solution no longer works. Use Vim-go instead. Beginners may want to follow medium.com/@hackintoshrao/…. Make sure that you are using using Vim 7.4.1689 or newer.Epidiascope
T
25

you can just add these lines to your ~/.vimrc:

set rtp+=$GOROOT/misc/vim
filetype plugin indent on
syntax on

EDIT This assumes filetype plugin indent off before these lines (i.e. beginning of .vimrc file) and may cause problems if it's not. See @peterSO's answer below for the safer version.

Tectonic answered 22/3, 2013 at 22:8 Comment(8)
I am not sure if anyone else has had this problem but, I tried inserting those lines in my .vimrc and it didn't work. I tried echoing $GOROOT and a empty line came out. What is the path for $GOROOT suppose to be? It should be some directory but I am not sure why mine failed to have any content, did you encounter this problem?Housewarming
@charlie-parker: how did you install go? I recently installed it on an Ubuntu 12.04 via apt-get install golang. It appears that /misc/vim is not included in the subsequently installed packages. A solution is to use the vim-golang repository (preferably in conjunction with something like pathogen).Evangel
@CharlieParker I use a manual installation and have $GOROOT set manually in ~/.profile. Since it's a manual installation I set it to /usr/local/go. Alternatively you can use @Evangel 's solution which should work as well.Tectonic
I did the automatic install. However, I think I understand that its ok for Unix not to know about $GOROOT. I figured that it was set correctly since go env showed how it was set. I didn't know that. Thanks though!Housewarming
As of 2015 January (maybe earlier), the vim directory has been removed from $GOROOT/misc/.Democratize
Got: E79: Cannot expand wildcards error message probably because $GOROOT/misc/vim doesn't longer exists. This answer solved for me: https://mcmap.net/q/296413/-add-golang-syntax-highlighting-for-vimSolecism
syntax on is the keyCarburize
The first 5 or so answers are way too old. Save yourself a headache and look at https://mcmap.net/q/296413/-add-golang-syntax-highlighting-for-vim.Altogether
B
65

UPDATE:

Go 1.4 Release Notes

Miscellany

The standard repository's top-level misc directory used to contain Go support for editors and IDEs: plugins, initialization scripts and so on. Maintaining these was becoming time-consuming and needed external help because many of the editors listed were not used by members of the core team. It also required us to make decisions about which plugin was best for a given editor, even for editors we do not use. The Go community at large is much better suited to managing this information. In Go 1.4, therefore, this support has been removed from the repository. Instead, there is a curated, informative list of what's available on a wiki page.


The standard Go distribution includes Go files for Vim in go/misc/vim/. This directory contains a readme.txt file which contains installation instructions.

readme.txt

Vim plugins for Go (http://golang.org)

To use all the Vim plugins, add these lines to your $HOME/.vimrc.

" Some Linux distributions set filetype in /etc/vimrc.
" Clear filetype flags before changing runtimepath to force Vim to reload them.
filetype off
filetype plugin indent off
set runtimepath+=$GOROOT/misc/vim
filetype plugin indent on
syntax on

If you want to select fewer plugins, use the instructions in the rest of this file.

<<..SNIP..>>

Bismarck answered 22/3, 2013 at 22:20 Comment(4)
Also check out vim-golang, which is a mirror of the official Vim plugins for golang that can easily be installed/updated via Vundle, etc. github.com/jnwhiteh/vim-golangInelastic
I am not sure if anyone else has had this problem but, I tried inserting those lines in my .vimrc and it didn't work. I tried echoing $GOROOT and a empty line came out. What is the path for $GOROOT suppose to be? It should be some directory but I am not sure why mine failed to have any content, did you encounter this problem?Housewarming
@CharlieParker I found that $GOROOT does not get defined after installing go on ubuntu (via apt-get) so I manually added this to my .bashrc file (export GOROOT="/usr/lib/go") - in addition I had to download the tar.gz from code.google.com/p/go/wiki/Downloads?tm=2 and extract the misc folder into place (/usr/lib/go/misc/) as this is not done when installing via ubuntu. ref: groups.google.com/forum/#!topic/golang-nuts/zQhCYp0BZp8Hubbard
See answers below, /misc/vim has been deprecated and you should use vim-go (or other options) instead.Bughouse
T
40

On Debian, I suppose it's the same on ubuntu, you just :

sudo apt-get install vim-gocomplete gocode vim-syntax-go
vim-addon-manager install go-syntax
vim-addon-manager install gocode
Tenfold answered 25/6, 2014 at 20:17 Comment(3)
My Wheezy install has no vim-gocomplete or gocode packages.Eyeball
You're right : packages.debian.org/vim-gocomplete only for jessie and sid. But golang is only available in jessie and sid ... with .deb package.Tenfold
Works in ubuntu 15 as wellOssy
T
25

you can just add these lines to your ~/.vimrc:

set rtp+=$GOROOT/misc/vim
filetype plugin indent on
syntax on

EDIT This assumes filetype plugin indent off before these lines (i.e. beginning of .vimrc file) and may cause problems if it's not. See @peterSO's answer below for the safer version.

Tectonic answered 22/3, 2013 at 22:8 Comment(8)
I am not sure if anyone else has had this problem but, I tried inserting those lines in my .vimrc and it didn't work. I tried echoing $GOROOT and a empty line came out. What is the path for $GOROOT suppose to be? It should be some directory but I am not sure why mine failed to have any content, did you encounter this problem?Housewarming
@charlie-parker: how did you install go? I recently installed it on an Ubuntu 12.04 via apt-get install golang. It appears that /misc/vim is not included in the subsequently installed packages. A solution is to use the vim-golang repository (preferably in conjunction with something like pathogen).Evangel
@CharlieParker I use a manual installation and have $GOROOT set manually in ~/.profile. Since it's a manual installation I set it to /usr/local/go. Alternatively you can use @Evangel 's solution which should work as well.Tectonic
I did the automatic install. However, I think I understand that its ok for Unix not to know about $GOROOT. I figured that it was set correctly since go env showed how it was set. I didn't know that. Thanks though!Housewarming
As of 2015 January (maybe earlier), the vim directory has been removed from $GOROOT/misc/.Democratize
Got: E79: Cannot expand wildcards error message probably because $GOROOT/misc/vim doesn't longer exists. This answer solved for me: https://mcmap.net/q/296413/-add-golang-syntax-highlighting-for-vimSolecism
syntax on is the keyCarburize
The first 5 or so answers are way too old. Save yourself a headache and look at https://mcmap.net/q/296413/-add-golang-syntax-highlighting-for-vim.Altogether
U
21

For the best syntax highlighting try https://github.com/fatih/vim-go

It's a new project that consolidates many vim plugins and adds a lot of features. From the readme:

  • Improved Syntax highlighting, such as Functions, Operators, Methods..
  • Auto completion support via gocode
  • Better gofmt on save, keeps cursor position and doesn't break your undo history
  • Go to symbol/declaration with godef
  • Automatically import packages via goimports
  • Compile and go build your package, install it with go install
  • go run quickly your current file/files
  • Run go test and see any errors in quickfix window
  • Lint your code with golint
  • Run your code trough go vet to catch static errors.
  • Advanced source analysis tool with oracle
  • List all source files and dependencies
  • Checking with errcheck for unchecked errors.
  • Integrated and improved snippets. Supports ultisnips or neosnippet
  • Share your current code to play.golang.org
Unbelievable answered 6/4, 2014 at 17:18 Comment(3)
In CentOS, I succeed to install vim-go with yum. #yum install vim-goCockle
This project is sunsetPerfumer
@ColeBittel, did you read the blog post you linked? vim-go is actively maintained, as announced in the very blog post you linked to. The project founder no longer works on it, that's all.Cannot
T
9

on 25/Jan/2015

Please see https://github.com/golang/go/wiki/IDEsAndTextEditorPlugins as now all editor & shell support in Go repo is removed (https://codereview.appspot.com/105470043)

Toilette answered 25/1, 2015 at 16:30 Comment(0)
W
5

I did not find instructions on turning on vim syntax highlighting for CentOS 7. Have tested the ensuing instructions to work on CentOS 7.3.1611. First, create the following directory in your home directory:

$ mkdir ~/.vim/ftdetect/

Then, create a file named, go.vim inside the above directory with the contents:

au BufRead,BufNewFile *.go set filetype=go

Download the syntax definition file for Go: vim.go. Transfer it to the right system-wide directory so that multiple users can share:

$ sudo mv -i go.vim /usr/share/vim/vim74/syntax/
Woolsey answered 4/10, 2018 at 7:5 Comment(3)
Thank you for this. Upvote this, people! This is the most recent answer and it works like a charm on CentOS 7.5.1804.Serinaserine
I would just like to clarify the above "Download the syntax definition file for Go: vim.go into /usr/share/vim/vim74/syntax/go.vim". The above solution does work on CentOS 6 and 7Altogether
Fantastic, most other resources all end up not working for anything remotely old, this does the trick.Deil
A
4

For whatever reason outside of my own decision making, we got Golang installed on our dev VMs by Debian packages. This particular distribution of vim doesn't come with any of the goodies for vim as far as I was able to tell from a search around for it. Anyways, I decided to go the vundle route in order to rapidly deploy the goodies to all these dev VMs. You could probably work this method into puppet or something if you'd like, we didn't do that though. Anyways, here's what I did:

Step 1: Install vundle: https://github.com/gmarik/vundle

Step 2: put this line in your .vimrc (It's from here, of course: https://github.com/jnwhiteh/vim-golang ), and then run vim from the command line like vim +BundleInstall +qall or from within vim with :BundleInstall

Bundle 'jnwhiteh/vim-golang'

Step 3: Save this little bash script I whipped up as govim.sh or whatever, chmod +x govim.sh, and run it like ./govim.sh

Script as follows:

#!/bin/bash
mkdir $HOME/.vim/ftdetect
mkdir $HOME/.vim/syntax
mkdir $HOME/.vim/autoload
mkdir $HOME/.vim/autoload/go
mkdir $HOME/.vim/ftplugin
mkdir $HOME/.vim/ftplugin/go
mkdir $HOME/.vim/indent
mkdir $HOME/.vim/compiler
mkdir $HOME/.vim/plugin
mkdir $HOME/.vim/plugin/godoc
ln -s $HOME/.vim/bundle/vim-golang/ftdetect/gofiletype.vim $HOME/.vim/ftdetect
ln -s $HOME/.vim/bundle/vim-golang/syntax/go.vim $HOME/.vim/syntax/
ln -s $HOME/.vim/bundle/vim-golang/autoload/go/complete.vim $HOME/.vim/autoload/go/
ln -s $HOME/.vim/bundle/vim-golang/ftplugin/go.vim $HOME/.vim/ftplugin/
ln -s $HOME/.vim/bundle/vim-golang/ftplugin/go/*.vim $HOME/.vim/ftplugin/go/
ln -s $HOME/.vim/bundle/vim-golang/indent/go.vim $HOME/.vim/indent/
ln -s $HOME/.vim/bundle/vim-golang/compiler/go.vim $HOME/.vim/compiler/
ln -s $HOME/.vim/bundle/vim-golang/plugin/godoc/godoc.vim $HOME/.vim/plugin/godoc/
ln -s $HOME/.vim/bundle/vim-golang/syntax/godoc.vim $HOME/.vim/syntax/

Kaching! You now have all the goodies installed, and someone correct me if I'm wrong on this but perhaps more than what comes with the official Golang distribution. I don't know about this either having not tried it yet, but I think that the runtimepath/rtp gets clobbered if you use Vundle with the other answers here anyways.

Approachable answered 2/1, 2014 at 22:23 Comment(0)
S
4

It should be easy as 1, 2, 3 :

  1. Download the file vim.go and place in in ~/.vim/syntax under the name go.vim (create the directory syntax if you don't already have it).

  2. If you don't already have the file ~/.vim/ftdetect/go.vim, create it (and the folder if necessary).

  3. In .vim/ftdetect/go.vim, add the following line : au BufRead,BufNewFile *.go set filetype=go

Soldier answered 24/12, 2018 at 15:30 Comment(2)
"place in in ~/.vim/syntax under the name vim.go" should instead be "under the name go.vim". Then it works :)Winwaloe
This is the best AnswerIdolatrous
P
3

This page says that:

Place $GOROOT/misc/vim/syntax/go.vim in ~/.vim/syntax/ 
and put the following in ~/.vim/ftdetect/go.vim:

au BufRead,BufNewFile *.go set filetype=go

It worked for me, only i did not find the /misc/vim/go.vim directory at first. So i copied the files from another computer that had installed go on /usr/local/go/...

Polyclinic answered 16/1, 2014 at 14:36 Comment(0)
N
0

This is what worked for me in MAC

let g:tagbar_type_go = {
        \ 'ctagstype' : 'go',
        \ 'kinds'     : [
                \ 'p:package',
                \ 'i:imports:1',
                \ 'c:constants',
                \ 'v:variables',
                \ 't:types',
                \ 'n:interfaces',
                \ 'w:fields',
                \ 'e:embedded',
                \ 'm:methods',
                \ 'r:constructor',
                \ 'f:functions'
        \ ],
        \ 'sro' : '.',
        \ 'kind2scope' : {
                \ 't' : 'ctype',
                \ 'n' : 'ntype'
        \ },
        \ 'scope2kind' : {
                \ 'ctype' : 't',
                \ 'ntype' : 'n'
        \ },
        \ 'ctagsbin'  : 'gotags',
        \ 'ctagsargs' : '-sort -silent'
\ }

let g:go_highlight_structs = 1
let g:go_highlight_methods = 1
let g:go_highlight_functions = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
syntax on
  • Install gotags - e.g. brew install gotags
  • Generate ctags - e.g. gorags -R . > ./tags
  • Open vim from the new shell
Newspaperwoman answered 22/8, 2020 at 6:11 Comment(0)
I
-1

Turns out the directions above were slightly ambiguous.

~/.vim/syntax/go.vim should have the same contents as ~/.vim/ftdetect/go.vim

only ~/.vim/ftdetect/go.vim must be appended with au BufRead,BufNewFile *.go set filetype=go.

If taken literally, the directions tell you to create a file ~/.vim/ftdetect/go.vim containing only

         au BufRead,BufNewFile *.go set filetype=go

I suppose that's where contextual knowledge should kick in. Only I'd never done this before and had no such context. Thanks all!

Intensity answered 22/3, 2013 at 21:23 Comment(2)
No, that's not right. Only the single line must indeed be placed into the ftdetect/go.vim file. Something else is missing. Do you have both :filetype on and :syntax on in your ~/.vimrc?!Flossieflossy
Huh, I'll revisit, but syntax highlighting only worked when I copied ~/.vim/syntax/go.vim to ~/.vim/ftdetect/go.vim and appended 'au BufRead,BufNewFile *.go set filetype=go'. I do not have a ~/.vimrc file. It looks like it would make things convenient, as the above answer suggests but do you know what negative effects having redundant code in ~/.vim/ftdetect/go.vim might cause and why, in the absence of ~/.vimrc configuration this seems to work?Intensity

© 2022 - 2024 — McMap. All rights reserved.