/software
✏️ Go in 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.
First, you need to get Vim 8+. If you are Ubuntu you can simply add an ppa to install Vim:
$ sudo add-apt-repository ppa:jonathonf/vim
$ sudo apt-get update && sudo apt-get install -y vim
Then, you’ll need to get vim-plug
. There are a lot of plugin managers, but I like the minimalism of this one.
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
Then, just create a ~/.vimrc
with the following:
call plug#begin('~/.vim/plugged')
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
call plug#end()
let g:go_fmt_command = "goimports"
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
:PlugInstall
and press enter. When that is finished you can press esc
again and type
:GoInstallBinaries
That’s it!
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