Add GoLang syntax highlighting for VIM

I am trying to add Go syntax highlighting to VIM on Ubuntu with the resources and directions here http://go-lang.cat-v.org/text-editors/vim/ .

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

Put $ 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 procedure for setting up the vim syntax that I saw elsewhere ( Vim 7.3 on Ubuntu 12.10 nowhere has the 'ftplugin' directory and https://github.com/jnwhiteh/vim-golang/blob/ master / readme.txt )

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

and follow the instructions above by adding

go.vim in ~ / .vim / syntax / and creating a go.vim file in ~ / .vim / ftdetect / that contains

 au BufRead,BufNewFile *.go set filetype=go 

However, syntax highlighting does not seem to happen. What do I need to do so that VIM looks at these new settings files?

+53
vim go vim-syntax-highlighting vim-plugin
Mar 22 '13 at 21:05
source share
10 answers

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

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

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

+18
Mar 22 '13 at 22:08
source share

UPDATE:

Go 1.4 Release Notes

Miscellaneous

The standard top-level repository directory used to store Go support for editors and IDEs: plugins, initialization scripts, etc. Maintaining them has become time-consuming and external assistance is needed because many of the editors listed were not used by members of the core group. It also required us to make decisions about which plug-in is best for this editor, even for the editors we do not use. The Go community as a whole is much better at managing this information. Therefore, in Go 1.4, this support was removed from the repository. Instead, there is a curated, informative list that is available on the wiki page.




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

readme.txt

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

To use all Vim plugins, add these lines to $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 .. β†’

+62
Mar 22 '13 at 22:20
source share

In 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 
+40
Jun 25 '14 at 20:17
source share

For better syntax highlighting, try https://github.com/fatih/vim-go

This is a new project that integrates many vim plugins and adds many features. From the readme file:

  • Improved syntax highlighting, for example, functions, operators, methods.
  • Support auto complete via gocode
  • Better gofmt when saving, saves the cursor position and does not violate the cancellation history.
  • Go to symbol / declaration with godef
  • Automatic package import via goimports
  • Compile and go, create your package, install it with go install
  • quickly run your current file / files
  • Run the go test and look for any errors in the quick delete window.
  • Draw your code with golint
  • Run your code trough to check for static errors.
  • Advanced oracle source code analysis tool
  • List all source files and dependencies
  • Error checking for unverified errors.
  • Integrated and improved fragments. Support for ultisnips or neosnippet
  • Share your current code with play.golang.org
+20
Apr 6 '14 at 17:18
source share

on 25 / Jan / 2015

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

+9
Jan 25 '15 at 16:30
source share

For some reason, outside of my own solution, we got Golang installed on our virtual machines using Debian packages. This particular vim distribution does not bring any benefits to vim, as far as I could tell from the search around it. In any case, I decided to follow the vundle path to quickly deploy goodies for all of these virtual machines. You could probably use this method in a puppet or something, if you want, we did not. Anyway, here is what I did:

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

Step 2: put this line in your .vimrc (This is from here, of course: https://github.com/jnwhiteh/vim-golang ) and then run vim from the command line, for example vim +BundleInstall +qall or from inside vim using :BundleInstall

 Bundle 'jnwhiteh/vim-golang' 

Step 3: Save this little bash script I hacked like govim.sh or something else, chmod +x govim.sh and ran it as ./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! Now you have all the useful properties, and someone will correct me if I am wrong about this, but maybe more than what comes with the official Golang distribution. I don't know about this without even trying it, but I think runtimepath / rtp gets clobbered if you use Vundle with other answers here anyway here anyway.

+3
Jan 02 '14 at 22:23
source share

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 

This worked for me, but I did not find the directory /misc/vim/go.vim. So I copied the files from another computer on which go / usr / local / go / ... was installed

+3
Jan 16 '14 at 2:36 on
source share

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

 $ mkdir ~/.vim/ftdetect/ 

Then create a file called go.vim in 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 desired system directory so that several users can share:

 $ sudo mv -i go.vim /usr/share/vim/vim74/syntax/ 
+1
04 Oct '18 at 7:05
source share

It should be easy, like 1, 2, 3:

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

  2. If you do not already have a ~/.vim/ftdetect/go.vim , create it (and a folder if necessary).

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

+1
Dec 24 '18 at 15:30
source share

The directions turned above were slightly ambiguous.

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

only ~/.vim/ftdetect/go.vim should be added using au BufRead,BufNewFile *.go set filetype=go .

Literally, the instructions tell you to create a ~/.vim/ftdetect/go.vim containing only

  au BufRead,BufNewFile *.go set filetype=go 

I guess when to use contextual knowledge. Only I had never done this before and had no such context. Thanks everyone!

-one
Mar 22 '13 at 21:23
source share



All Articles