
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:
set nocompatible " Enables us Vim specific features
filetype off " Reset filetype detection first ...
filetype plugin indent on " ... and enable filetype detection
set ttyfast " Indicate fast terminal conn for faster redraw
set ttymouse=xterm2 " Indicate terminal type for mouse codes
set ttyscroll=3 " Speedup scrolling
set laststatus=2 " Show status line always
set encoding=utf-8 " Set default encoding to UTF-8
set autoread " Automatically read changed files
set autoindent " Enabile Autoindent
set backspace=indent,eol,start " Makes backspace key more powerful.
set incsearch " Shows the match while typing
set hlsearch " Highlight found searches
set noerrorbells " No beeps
set number " Show line numbers
set showcmd " Show me what I'm typing
set noswapfile " Don't use swapfile
set nobackup " Don't create annoying backup files
set splitright " Vertical windows should be split to right
set splitbelow " Horizontal windows should split to bottom
set autowrite " Automatically save before :next, :make etc.
set hidden " Buffer should still exist if window is closed
set fileformats=unix,dos,mac " Prefer Unix over Windows over OS 9 formats
set noshowmatch " Do not show matching brackets by flickering
set noshowmode " We show the mode with airline or lightline
set ignorecase " Search case insensitive...
set smartcase " ... but not it begins with upper case
set completeopt=menu,menuone " Show popup menu, even if there is one entry
set pumheight=10 " Completion window max size
set nocursorcolumn " Do not highlight column (speeds up highlighting)
set nocursorline " Do not highlight cursor (speeds up highlighting)
set lazyredraw " Wait to redraw
" Enable to copy to clipboard for operations like yank, delete, change and put
" http://stackoverflow.com/questions/20186975/vim-mac-how-to-copy-to-clipboard-without-pbcopy
if has('unnamedplus')
set clipboard^=unnamed
set clipboard^=unnamedplus
endif
" This enables us to undo files even if you exit Vim.
if has('persistent_undo')
set undofile
set undodir=~/.config/vim/tmp/undo//
endif
" Colorscheme
syntax enable
set t_Co=256
let g:rehash256 = 1
let g:molokai_original = 1
colorscheme molokai
let g:go_fmt_command = "goimports"
let g:go_autodetect_gopath = 1
let g:go_list_type = "quickfix"
let g:go_highlight_types = 1
let g:go_highlight_fields = 1
let g:go_highlight_functions = 1
let g:go_highlight_function_calls = 1
let g:go_highlight_extra_types = 1
let 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
.