golang

Vim for Golang Developers (Vim Golang)

Vim is one of the most powerful and popular text editors for developers and sys admins. It is highly configurable with extensive features and key-bindings that make the development a breeze.

In this tutorial, we will learn how to configure the Vim text editor for Go development including all the plugins and other QOL improvements.

Install Vim

If you do not have Vim installed on your system, you can do so using your system’s package manager. For example, on Debian, run the “apt” command as follows:

$ sudo apt-get install vim

 

You can use Homebrew on macOS:

$ brew install vim

 

Basic Vim Configuration

Before we dive into enabling and adding external plugins and features, let us configure the basics of the editor.

In Vim, we need to create a file called “.vimrc” file in the home directory. This is where we add the configuration for the editor.

$ touch ~/.vimrc

 

Edit the file and add the following configurations:

Use the following command to enable the line numbers in the editor:

set number

 

Use the following command to enable the syntax highlighting:

syntax enable

 

You can also enable the line wrapping using config.

set wrap

 

If you prefer to use four spaces for tabs, add the “tabstop” and shift the “width” configs as follows:

set tabstop=4
set shiftwidth=4
set expandtab

 

To enable the search highlighting as we type, use the following command:

set incsearch

 

Next, enable the case-insensitive searching using the following commands:

set ignorecase
set smartcase

 

We can also enable the editor to show the line and column number in status line.

set ruler

 

If you prefer to interact with the mouse in the editor, you can add the mouse support using config:

set mouse=a

 

Lastly, we can enable the clipboard support. This requires Vim with +clipboard.

set clipboard=unnamedplus

 

Once we have the desired configuration enabled, we can proceed and setup the external packages and plugins.

Install and Manage the Plugins

To make the process of installing and configuring the plugins in Go, we can use the vim-plug which is a powerful and easy-to-use plugin manager for Go.

To install it, add the following to your “.vimrc”:

if empty(glob('~/.vim/autoload/plug.vim'))
  silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif

" Initialize Vim-Plug
call plug#begin('~/.vim/plugged')

 

Once installed, we can add the new plugins by adding the following entry in the “.vimrc” file:

call plug#begin().

 

Install Vim-Go

Vim-Go is a plugin that adds the Go language support for Vim with extensive features and support such as quick compile and execution of binaries, improved syntax highlighting, and more.

We can then add the vim-go plugin as follows:

Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }

 

Save the changes to the “.vimrc” file.

Conclusion

In this tutorial, we learned how to quickly configure the Vim text editor for Go development. Keep in mind that there is more you can add such as key bindings, custom commands, and more.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list