Add GoLang syntax highlighting for VIM

73,538

Solution 1

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.

Solution 2

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..>>

Solution 3

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

Solution 4

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

Solution 5

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)

Share:
73,538
rocketas
Author by

rocketas

Updated on August 22, 2020

Comments

  • rocketas
    rocketas over 3 years

    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?