Vim for Golang

07/07/2013

Developping with Go is easy, a simple text editor and you’re ready to work. But it’s very confortable to have syntax highlight, checker and outline view. There are lot of possible configurations for a lot of editors. My preference is Vim + some plugins. Let me show you how I configured my “Go vim IDE”

At first, be sure you’ve installed a recent Go implementation. My configuration works with Go version > 1.x.

I’m working on Fedora (18) with a vim version 7.3.

What we will do:

  • install pathogen (or Vundle)
  • install syntastic
  • install gocode
  • install tagbar + configuration
  • install nerdtree (optional)

Pathogen

Pathogen is like Vundle, it allows you to install plugins inside a deported directory that is easier to manage. To install pathogen:

mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \
https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim

Then, in your ~/.vimrc, append this line before any “filetype” directive or “syntax”:

execute pathogen#infect()

You’ll see my own vimrc plugin at the end of post.

Ok, you’re done, now to install plugins, you just have to put it in ~/.vim/bundle - this is what we will do.

Syntastic

I will be honest, I don’t know if syntastic is required, but if you develop with other langages (as Python, C, Java…) with vim, you will love this plugin.

Syntastic is a vim plugin that is very powerfull. It will show you errors and warnings for a large variety of langage when you save your vim buffer. TO install this plugin, now you’ve got pathogen installed, is easy:

cd ~/.vim/bundle
git clone https://github.com/scrooloose/syntastic.git

That’s all

Vim for go

Before installing Gocode, let’s install the given vim configuration from your Go installation.

Be sure that your environment variable contains somthing:

$ echo $GOROOT 
/home/patachou/src/go

Then type:

ln -s $GOROOT/misc/vim ~/.vim/bundle/go-vim

That link configuration to the “bundle” directory for vim.

Now, the entire vim configuration is ready to use (thanks again to pathogen)

Gocode

Now, let’s install the Go part. Gocode is a “service” that will be connected to your vim instance. It will add lot of very interessing functionnalities as:

  • code check
  • code completion

In your terminal, execute this:

go get -u github.com/nsf/gocode

This add the “gocode” command. Vim will use if needed. The deamon will start at time.

Some vim configuration

We will need some additional vim configuration. Open your ~/.vimrc and append:

set omnifunc=syntaxcomplete#Complete

This activate “omnifunc” for the whole possible languages that implement completion.

So to conclude, this is my (cleaned for the article) vimrc file:

execute pathogen#infect()
set modeline
set number
syntax on
set omnifunc=syntaxcomplete#Complete
set mouse=a
set autoindent
set ts=4 sts=4 sw=4 expandtab
filetype plugin indent on     " required! 
filetype plugin on

With this lines, I’ve got:

  • pathogen that checks my ~/.vim/bundle directory
  • modeline that can read some “vim directives in comments” (optionnal)
  • line number
  • syntax is on
  • completion ok
  • mouse that can be used to navigate in NerdTree, and Tagbar (after the test, be patient ;) )
  • indentation auto
  • tab makes 4 spaces
  • plugin for indentation + autocommand (required by gocode)

Ok, make a test now

Testing installation

Before to try, keep the shortcuts in mind: CTRL+x - o ==> autocompletion from “omnifunc”

Now, go to a temporary directory, for example /tmp and make a test directory:

cd /tmp; mkdir testgo; cd testgo

And try:

vim main.go

You will have vim opened, append this lines:

package main

import "fmt"

func main() {
     // place your cursor after the "."
     fmt.
}

After dot (.) press “CTRL+x” then “o”, and you will see a menu that lets you choose the method to use.

This works with any variables, package, builtins…

Append 2 plugins that will change your life

There are 2 plugins that can be very cool to use: Nerdtree and tagbar.

Nerdtree is a directory viewer that shows you your project directory. There are a lot of options, but you can use “as is”. NERDTree is made by the same author that syntastic.

Let’s install:

cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git

Now install the second plugin, tagbar that is an outline viewer that works with a lot of languages (excpeting go… buuuuuuut):

cd ~/.vim/bundle
git clone git://github.com/majutsushi/tagbar

To let tagbar useable with Go, we need to install “gotags” and append a vim configuration…:

go get -u github.com/jstemmer/gotags

And append this to the file ~/.vim/ftplugin/go.vim (theorically, gocode has created this file and append a omnifunc method on top, don’t remove this line, append this after):

" gotags
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'
\ }

Right ok !

Test tagbar and NERDTree

You can do:

  • type :NERDTree in vim
  • open vim with +NERDTree argument

To open Tagbar, type :TagbarOpen

This is what I’ve got:

One shot

Let’s resume what we’ve made:

mkdir -p ~/.vim/autoload ~/.vim/bundle; \
curl -Sso ~/.vim/autoload/pathogen.vim \
https://raw.github.com/tpope/vim-pathogen/master/autoload/pathogen.vim

ln -s $GOROOT/misc/vim ~/.vim/bundle/go-vim

cd ~/.vim/bundle
git clone https://github.com/scrooloose/syntastic.git
git clone https://github.com/scrooloose/nerdtree.git
git clone git://github.com/majutsushi/tagbar

go get -u github.com/nsf/gocode
go get -u github.com/jstemmer/gotags

In ~/.vimrc:

execute pathogen#infect()
set modeline
set number
syntax on
set omnifunc=syntaxcomplete#Complete
set mouse=a
set autoindent
set ts=4 sts=4 sw=4 expandtab
filetype plugin indent on     " required! 
filetype plugin on

Open one time a gocode, use completion, that creates ~/.vim/ftplugin/go.vim

Then append to ~/.vim/ftplugin/go.vim:

" gotags
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'
\ }

That’s all folks

Ça peut vous intéresser aussi


Installer localement Golang

Toujours en cours d’écriture d’un livre sur le ...


Mise à jour du blog en Go

Si vous connaissiez mon blog, vous avez remarqué qu’à ...


Golang, résoudre le souci d'indexation de type défini

Golang permet de créer ses prores types et notamment de ...


Créer une API en GO avec Gorilla

Bon, on va parler Go ou Golang (faudra me dire ...

Merci de m'aider à financer mes services

Si vous avez apprécié cet article, je vous serai reconnaissant de m'aider à me payer une petite bière :)

Si vous voulez en savoir plus sur l'utilisation de flattr sur mon blog, lisez cette page: Ayez pitié de moi

Commentaires

Ajouter un commentaire

knibals - 06/05/2014

Merçi mec! Ca m’a été utile ;)

blecgard - 03/01/2014

very nice. Thanks alot!

Ajouter un commentaire

(*) Votre e-mail ne sera ni revendu, ni rendu public, ni utilisé pour vous proposer des mails commerciaux. Il n'est utilisé que pour vous contacter en cas de souci avec le contenu du commentaire, ou pour vous prévenir d'un nouveau commentaire si vous avez coché la case prévue à cet effet.