Can VIM Ever Be a Full-time Go Editor?

Can VIM Ever Be a Full-time Go Editor?

Can VIM really be a full-time editor? Is it genuinely possible to develop as productively with it as you would

Can VIM really be a full-time editor? Is it genuinely possible to develop as productively with it as you would using an IDE, such as PhpStorm, Eclipse, or Netbeans?

This is a question which I’ve become determined to answer, ideally in the affirmative.

Last week I started by posing the question Can VIM Ever Replace PHPStorm?. This wasn’t meant to be provocative, but an exploration of possibilities. And it’s a question which is important to me, the more I learn just how productive you can be with VIM.

If you’re not familiar with VIM, I strongly encourage you to become so. Whilst 30 years old, it’s a very capable, I’d even go so far as to say, amazing, application.

There isn’t any mouse interaction (well, mostly) available, at least by default. But despite that, you’d be amazed how much you can do just with the keyboard.

So I Started with PHP

As I’ve been developing with PHP for the last 15 years, it made sense to start using PHP as my point of reference.

Whilst I’ve been able to implement a lot of functionality, such as code inspections, linting, auto-indentation, brace and bracket completion, unfortunately, at this stage for me at least, the answer is no.

For PHP, VIM can’t replace my editor of choice, PhpStorm. I’ve not given up, not at all; but for two reasons, I’m not sure it’s going to happen.

Firstly, despite a lot of effort, I’ve always come up short getting even the most basic code completion to work. All I see is just a list of every symbol, function, variable, class – you name it.

Really, this isn’t helpful.

Secondly, PhpStorm is an exceptionally well put together piece of software. It covers so many aspects of PHP development, including code completion, code inspections, code refactoring, duplicate detection, and more.

In addition to this, it auto-indents, matches braces and brackets, and provides a number of related features and integrations, such as composer, Vagrant, and VCS integration. It’s hard to go past it. But PHP isn’t my only language.

And Then There Was Go

As with spoken, human, languages, I’m a polyglot, adept at amongst others, JavaScript, Bash, and the subject of today’s article, Go.

Now you might think, being younger than PHP, that the likelihood of getting auto-completion working in VIM, for Go, might be even less possible.

Turns out, it’s not only not a pipe dream, it’s readily available. Thanks to the two plugins, you get all that you’d expect, plus a whole lot more. Here’s but a sampling of what you get by installing these two plugins:

  • Syntax Highlighting
  • Auto code formatting on save
  • Ability to go to symbols and declarations
  • Documentation lookup
  • Linting, Testing, Building, Installing, and Running code
  • Source Analysis
  • An auto-completion daemon for the Go programming language
Golang auto-completion in VIM

I say this, because I recently got them both installed and sufficiently configured to do just this. So for Go VIM definitely can be my editor of choice, replacing more expensive and feature-laden alternatives.

Today I’m going to show you how to get it setup. But first, let’s have a look at it in action. In the screenshot above, you can see an example of editing a reasonably simple bit of Go code. You can see a struct and a function, and that I have code completion in progress for another struct.

You can see that the code completion’s correctly identified the four properties on the struct, those being Age, FirstName, Languages, and LastName, along with the type of each one.

This isn’t a mockup, this is real and works very well. What’s more, thanks to the plugins, you may as well have an IDE, such as Eclipse because, via keyboard mappings, you can do most of what you need without leaving your code.

Getting Started

If you want to get this working, you’ll need four plugins:

I’ll assume that your VIM configuration isn’t split across multiple files, but that it’s all contained in a single .vimrc file. If it’s not, adjust these instructions to suit your setup.

I’m also assuming that you’re using Vundle to manage your plugins. If you’re not, that’s fine. Both of the plugins’ repositories have instructions for multiple installation approaches.

Assuming you’re using Vundle, add the above Bundles above to your Bundles list as follows:

Bundle 'fatih/vim-go'
Bundle 'nsf/gocode'
Bundle 'Shougo/vimproc.vim'
Bundle 'Shougo/vimproc.vim', {
   'build' : {
       'windows' : 'toolsupdate-dll-mingw',
       'cygwin' : 'make -f make_cygwin.mak',
       'mac' : 'make -f make_mac.mak',
       'linux' : 'make',
       'unix' : 'gmake',

Some Plugin Configuration

Once you have these added, you then need to add some plugin configuration options. This will make the experience that much smoother. To save time, I’ve included them below.

What they do is to setup a series of keyboard mappings, so that it’s easy to work with Go directly in VIM. The last 5 lines turns on syntax highlighting for various symbols, which is disabled by default.

" Go keyboard mappings
au FileType go nmap <Leader>s <Plug>(go-implements)
au FileType go nmap <Leader>i <Plug>(go-info)
au FileType go nmap <Leader>gd <Plug>(go-doc)
au FileType go nmap <Leader>gv <Plug>(go-doc-vertical)
au FileType go nmap <Leader>gb <Plug>(go-doc-browser)
au FileType go nmap <leader>r <Plug>(go-run)
au FileType go nmap <leader>b <Plug>(go-build)
au FileType go nmap <leader>t <Plug>(go-test)
au FileType go nmap <leader>c <Plug>(go-coverage)
au FileType go nmap <Leader>ds <Plug>(go-def-split)
au FileType go nmap <Leader>dv <Plug>(go-def-vertical)
au FileType go nmap <Leader>dt <Plug>(go-def-tab)

" Enable syntax highting on everything
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1

With all that added, run BundleInstall, and then restart VIM. Once done, you’re ready to code, in Go, like a pro.

But Wait – There’s More!

No, you don’t get a set of steak, or Ginsu, knives thrown in for free. But naturally there’s more available than I’ve covered here; because an exhaustive discussion of the four plugins is beyond the remit of this post.

So I strongly encourage you to check out the respective plugin repositories to see exactly what’s on offer and to go further than I have here.

One Last Thing

I have to mention this, more as a just in case. But I’m using VIM in the terminal, on a Mac. I’m not sure why yet, but when I attempt to use it in MacVim, it doesn’t work. I’m trying to get it worked out. When I have, I’ll update this post.

The Setup In Action

Now, to finish up, I want to show you it in action. In the screencast below, you can see how it works, using the simple file which was used in the screenshot above. It isn’t the most elegant of code, but it’s enough to show you what to expect.

Wrapping Up

And that just goes to show that Vim can be your daily code editor. The key features which the more expensive IDEs come with, out of the box, are all available for VIM, for Go, as well. To be fair, Vim isn’t an IDE, and it was never intended to be one. It’s

A highly configurable text editor built to enable efficient text editing

So when you compare it with IDEs, whilst you can configure VIM to do what a lot of them do, out of the box, there can be quite a lot of work involved to configure and tweak settings to be as you would normally expect them to operate.

Perhaps this puts a lot of people off from using it; as there’s so much onus on the user to get their setup working as they want it. Perhaps some developers just want to be able to start developing straight-away, and not have to be concerned of what’s going on internally.

This is a debate which can rage for a long time, so I’ll not get in to it further, as there’s no clear answer either in the affirmative, or negative to this question. It all comes down to the individual.

If you like the flexibility to configure your setup exactly as you want it, and don’t mind investing a bit of time and effort to get it there, then VIM will be a good fit for you. If, however, you want to get going straight-away, and want things to, largely, “just work“, then stick with an IDE.

Are you a Go developer, who uses Vim? What’s your setup like? Mind sharing your configuration?

You might also be interested in these tutorials too...

Why I Created the Free the Geek Website By Hand
Sun, Nov 8, 2015

Why I Created the Free the Geek Website By Hand

With so many existing solutions for creating websites, why would you create one by hand? This post looks at the reasons why I chose to create the site by hand, versus using an established platform, such as Sculpin or Wordpress

First Experiences with Symfony 4 & the Symfony Community
Sat, Dec 30, 2017

First Experiences with Symfony 4 & the Symfony Community

Recently, I decided to learn the basics of the Symfony (4) framework, so that I could better understand one of my client’s applications, and provide better support to it. I never expected to use such a well-rounded framework. Nor did I expect to encounter such an engaged and supportive community. Here’s the story.

Becoming a Polyglot Developer (again)
Mon, Oct 10, 2016

Becoming a Polyglot Developer (again)

I’ve been developing software in PHP for a long time. But I recently decided to return to my polyglot roots, developing in multiple languages on a regular basis. The question is, which language, or languages, are the right ones to learn and use.

Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus