Zack Scholl

zack.scholl@gmail.com

Using vim with Golang

 / #tutorial #golang #vim 

Vim is a console-based text editor that has been around for 27 years (actually its a clone from vi that is 43 years old). Today, people still use and enjoy Vim.

Vim is not so bad when it comes to writing, but when it comes to coding there are arguably much sleeker GUI-based tools like Sublime, Visual Code, Atom, IntelliJ, etc.

And yet, as of 2018 there were still 17% of people using vim with Go, although that number as been in decline. This is particularly surprising to me because vim lacks a lot of useful GUI features (minimap, easy navigation between files). Also surprising Golang is becoming more popular even though it is not free (and quite expensive).

Still, I tried doing some coding with Vim. And, I like it! I like it enough to do it a lot. The features that I thought I would miss - like autocompletion, and easy building/testing from command-line - are really easy to do in Vim. Its definitely now part of my toolset, especially when I just need to edit a single file.

If you want to try using Go with Vim, here’s how you get started.

Instructions for setting up Vim with Go

There are multiple ways to do this, but I’m going to just write about a simple, easy method I found here.

$ git clone https://github.com/fatih/vim-go.git ~/.vim/pack/plugins/start/vim-go
$ mkdir -p ~/.vim/colors
$ curl https://raw.githubusercontent.com/tomasr/molokai/master/colors/molokai.vim > ~/.vim/colors/molokai.vim

Then, just create a ~/.vimrc with the following:

 1set nocompatible                " Enables us Vim specific features
 2filetype off                    " Reset filetype detection first ...
 3filetype plugin indent on       " ... and enable filetype detection
 4set ttyfast                     " Indicate fast terminal conn for faster redraw
 5set ttymouse=xterm2             " Indicate terminal type for mouse codes
 6set ttyscroll=3                 " Speedup scrolling
 7set laststatus=2                " Show status line always
 8set encoding=utf-8              " Set default encoding to UTF-8
 9set autoread                    " Automatically read changed files
10set autoindent                  " Enabile Autoindent
11set backspace=indent,eol,start  " Makes backspace key more powerful.
12set incsearch                   " Shows the match while typing
13set hlsearch                    " Highlight found searches
14set noerrorbells                " No beeps
15set number                      " Show line numbers
16set showcmd                     " Show me what I'm typing
17set noswapfile                  " Don't use swapfile
18set nobackup                    " Don't create annoying backup files
19set splitright                  " Vertical windows should be split to right
20set splitbelow                  " Horizontal windows should split to bottom
21set autowrite                   " Automatically save before :next, :make etc.
22set hidden                      " Buffer should still exist if window is closed
23set fileformats=unix,dos,mac    " Prefer Unix over Windows over OS 9 formats
24set noshowmatch                 " Do not show matching brackets by flickering
25set noshowmode                  " We show the mode with airline or lightline
26set ignorecase                  " Search case insensitive...
27set smartcase                   " ... but not it begins with upper case
28set completeopt=menu,menuone    " Show popup menu, even if there is one entry
29set pumheight=10                " Completion window max size
30set nocursorcolumn              " Do not highlight column (speeds up highlighting)
31set nocursorline                " Do not highlight cursor (speeds up highlighting)
32set lazyredraw                  " Wait to redraw
33
34" Enable to copy to clipboard for operations like yank, delete, change and put
35" http://stackoverflow.com/questions/20186975/vim-mac-how-to-copy-to-clipboard-without-pbcopy
36if has('unnamedplus')
37  set clipboard^=unnamed
38  set clipboard^=unnamedplus
39endif
40
41" This enables us to undo files even if you exit Vim.
42if has('persistent_undo')
43  set undofile
44  set undodir=~/.config/vim/tmp/undo//
45endif
46
47" Colorscheme
48syntax enable
49set t_Co=256
50let g:rehash256 = 1
51let g:molokai_original = 1
52colorscheme molokai
53
54
55let g:go_fmt_command = "goimports"
56let g:go_autodetect_gopath = 1
57let g:go_list_type = "quickfix"
58
59let g:go_highlight_types = 1
60let g:go_highlight_fields = 1
61let g:go_highlight_functions = 1
62let g:go_highlight_function_calls = 1
63let g:go_highlight_extra_types = 1
64let g:go_highlight_generate_tags = 1

Then open up a .go file. Note: if you are not in a GOPATH or using a module, then some things like autocompletion will not work!

Hit esc and then type

:GoInstallBinaries

and press enter.

Using Go in Vim

A lot of stuff is done automatically. For instance, whenever you save you will automatically be formatting with goimports.

To build a file you can save (:w!) and then you can run a program directly while editing using:

:! go build -v; ./program --debug

You can also use pre-built commands, say

:GoTest

You can pull up hints for functions by hitting Ctl+X+O.