Setting up vim for golang

Asheet Bhaskar
2 min readOct 4, 2018

--

I have been using vim for more than three months. Now I do most of my editing on vim. In this post, I shall be describing setting up vim for golang from scratch. Vim config that I shall be posting here is available here. I play with my vim configuration frequently to increase productivity and improve my experience with vim. Follow below sections to set up your vim.

Set up a plugin manager

There are many available plugin managers like pathogen, vim-plug, vim packages etc. I use pathogen.

  • Run the following command to install pathogen.vim.
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
  • Create a .vimrc file in the home directory.
cd ~
touch .vimrc
  • Add following lines in the .vimrc file.
execute pathogen#infect()
syntax on
filetype plugin indent on

Set up a file explorer

NerdTree gives ability to browse and open files and directories. It allows us to perform simple file operations like create, delete, rename a file or directory. Run following command to install nerdtree.

git clone https://github.com/scrooloose/nerdtree.git ~/.vim/bundle/nerdtree
  • Add following lines in .vimrc to configure nerdtree
" open a NERDTree automatically when vim starts up
autocmd vimenter * NERDTree

" open a NERDTree automatically when vim starts up if no files were specified
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif

" open NERDTree automatically when vim starts up on opening a directory
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif

" map a specific key or shortcut to open NERDTree
map <F2> :NERDTreeToggle<CR>

" close vim if the only window left open is a NERDTree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

" change default arrows
let g:NERDTreeDirArrowExpandable = '▸'
let g:NERDTreeDirArrowCollapsible = '▾'

Install a color scheme

Vim colors is a collection of nice color schemes for vim. I like codeschool color scheme. To install a color scheme create directory colors under .vim directory then add your color scheme.vimfile in colors directory and add following lines in the .vimrc file.

syntax enable
set background=dark
colorscheme _name-of-colorscheme_

Set up support for Go language. Install vim-go plugin

This plugin adds Go language support for Vim. It has many useful features listed here. Run the following command to install vim-go plugin.

git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go

After installing vim-go plugin, run following command from vim to install necessary binaries.

:GoInstallBinaries

After following above sections successfully you should have your vim configured for Golang. Vim is highly configurable. You can configure vim as per your preferences. If you have any suggestions about plugins to use, please let me know.

--

--

Asheet Bhaskar
Asheet Bhaskar

No responses yet