Asheet Kumar

Setting Up Vim for Golang

| Comments

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 about setting up vim for golang from scratch. Vim config that i shall be posting here is available here. I play with my vim configuartion frequently to increase productivity and improve my experience with vim. Follow below sections to setup your vim.

Set up a plugin manager

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

  • Run following command to install pathogen.vim.
1
2
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
  • Create a .vimrc file in home directory.
1
2
cd ~
touch .vimrc
  • Add following lines in .vimrc file.
1
2
3
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.

1
git clone https://github.com/scrooloose/nerdtree.git ~/.vim/bundle/nerdtree
  • Add following lines in .vimrc to configure nerdtree.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
" 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 colorscheme.

Vim colors is a collection of nice colorschemes for vim. I like codeschool colorscheme. To install a colorscheme create a directory colors under .vim directory then add your colorscheme.vim file in colors directory and add following lines in .vimrc file.

1
2
3
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 feature listed here. Run following command to install vim-go plugin.

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

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

1
: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.

Comments