Setting up vim for Golang programming + beginner cheat sheet

  • Install Pathogen, The Vim package manager
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
  • Add the following content to ~/.vimrc
  • vi ~/.vimrc
execute pathogen#infect()
syntax on
filetype plugin indent on
  • Install vim-go
$ git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go 

Here are some tips/vim-commands that could help you get going

:GoInstallBinaries 

This installs golint, gofmt and other static tools …

:split <filename> 

opens up the file in the split window

:hide 

closes the split window

More multiple window tricks are mentioned here

https://www.cs.oberlin.edu/~kuperman/help/vim/windows.html

5gg 

go to 5th line

12gg 

go to 12 line

11yy

copy 11 lines from current position

11, 22y 

copy from 11th to 22nd line

:set number 

to show line numbers on vim

You can run, build , test the Go package from vim

You need a map leader set in vim. To check the mapleader value run the following command in vim

:echo mapleader

If it shows undefined then you need to set the mapleader

add the following line to your ~/.vimrc

let mapleader=","

and then add the following to the ~/.vimrc

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)

this is to set Mapleader + r will run the go file and Mapleader + b will build the file

in this case the mapleader is comma(,) , so the command comma + r will run the file and comma + t will run the test

d$ 

deletes from current cursor to the end of the line

Shift + a 

go to end of the line and switch to insert mode

$

Go to the end of the line

5,10d 

delete from 5th to 10th line

0 

go to the beginning of the line

yw 

Copy the word under cursor

:%s/foo/bar/gc 

Change each ‘foo’ to ‘bar’, but ask for confirmation first.