My latest Vim setup

9 Apr 2016

The last time I updated my vim config in any serious way, vim 7.1 was all the rage. As of this writing, we are up to vim 7.4, and I've started loving go, so it's time for a serious update!

Step 1: Blow away .exrc and .gvimrc and consolidate everything in .vimrc.

.exrc is for really old versions of vi that don't need to exist anymore, and vim is smart enough to ignore the gvim stuff inside .vimrc, so there's no need for me to keep a separate .gvimrc anymore.

Also, because of Pathogen, vim now has a standard way of doing plugins! I will actually use vim-plug, but the format carries across other vim plugin managers.

Step 2: Make sure you tell Bash your terminal is capable of 256 colors by putting this somewhere in your .bashrc:

###############################################
# Tell terminal it can support 256 colors
export TERM=xterm-256color
# Tell the world (git in particular) that you want to use vim
# (instead of vi, which seems to pretend it can't understand all of .vimrc)
export VISUAL=vim
export EDITOR=vim
# Make vi a function that calls vim
vi() {
    vim "$@"
}

Step 3: Download vim-plug.

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Step 4: Put my color scheme in its own file. Gone are the days of putting my color preferences in .vimrc and .gvimrc! Put this in ~/.vim/colors/

highlight clear
if exists("syntax_on")
  syntax reset
endif
let g:colors_name = "manni"

highlight Normal        guibg=grey91  guifg=Black      ctermfg=0
highlight Cursor        guibg=Black   guifg=White      ctermfg=15
highlight NonText       guibg=bg      guifg=#bcbcbc    ctermfg=250
highlight SpecialKey    guibg=bg      guifg=#bcbcbc    ctermfg=250

" programming...
highlight Comment       guibg=bg      guifg=grey50     ctermfg=244
highlight Constant      guibg=bg      guifg=Brown      ctermfg=124
highlight Label         guibg=bg      guifg=Brown      ctermfg=124
highlight Number        guibg=bg      guifg=Brown      ctermfg=124
highlight Special       guibg=bg      guifg=DarkRed    ctermfg=9
highlight Function      guibg=bg      guifg=DarkBlue   ctermfg=18
" NONE resets Type; in particular, turn off bold from default setting
highlight Type          NONE
highlight Type          guibg=bg      guifg=Purple     ctermfg=91
highlight Statement     NONE
highlight Statement     guibg=bg      guifg=DarkGreen  ctermfg=22
highlight StorageClass  NONE
highlight StorageClass  guibg=bg      guifg=DarkBlue   ctermfg=18

Step 5: Put this in ~/.vimrc

" enable vim features instead of strict vi compatibility
set nocompatible
" tell vim it's OK to use 256 colors at the terminal
set t_Co=256
" try to detect the filetype
filetype on
" enable loading the plugin file for specific file types
filetype plugin on
" enable loading the indent file for specific file types
filetype indent on
" enable syntax highlighting
syntax on

" vim-plug configuration
call plug#begin('~/.vim/plugged')
Plug 'fatih/vim-go'
call plug#end()

" show matching brackets and parens
set showmatch
" sync syntax highlighting in a 5000-line window
" syntax sync minlines=5000
" sync syntax highlighting from the start of the file
syntax sync fromstart
" do not make backup~ files
set nobackup
" do not make backup~ files
set nowritebackup
" set character encoding to UTF-8
set encoding=utf-8
" show 3 lines above or below cursor when scrolling
set scrolloff=3
" show insert, replace, or visual mode in last line
set showmode
" show command in last line
set showcmd
" flash screen on bell
set visualbell
" assumes fast connection
set ttyfast
" show line and column number
set ruler
" every window gets a status line
set laststatus=2
" show spaces and tabs; to turn off for copying, use `:set nolist`
set list
set listchars=tab:→\ ,space:·,trail:·,nbsp:·
" switch off search pattern highlighting
set nohlsearch

" statusline
"
" %< means truncate on the left if too long
" %F is full path to the file we are editing
set statusline=%<%F
" %m shows [+] if the file is modified but not saved
set statusline+=%m
" %r shows [RO] if a file is read-only
set statusline+=%r
" %h shows [Help] if we are in a help buffer
set statusline+=%h
" %w shows [Preview] if we are in a preview window
set statusline+=%w
" separation point between the left and right items
set statusline+=%=
" prints the fileformat; that is, the kind of newline (one of unix, dos or mac)
" (If you type `:set fileformat?`, vim will tell you the current file format)
set statusline+=%{&fileformat}
" a literal forward slash
set statusline+=/
" %Y shows the filetype, such as VIM or HTML or GO
set statusline+=%Y
" %l shows the line number, and %8l uses 8 left-padded spaces to do so
set statusline+=%8l
" a literal comma
set statusline+=,
" %v shows the virtual column number;
" instead of counting a tab as one char, it counts it as the number
" of spaces it uses in the display
" %-8v leaves 8 spaces to the right to do so
set statusline+=%-8v

" These next two commands inspired by
" http://stackoverflow.com/questions/9160570/append-or-prepend-selected-text-to-a-file-in-vim
"
"command to save selected lines to the named file
com! -nargs=1 -range Save call SaveSelectedLinesToFile()
fun! SaveSelectedLinesToFile(filename)
  exec "'<,'>w! >>" . a:filename
endfunc

"command to move selected lines to the named file
com! -nargs=1 -range DelSave call MoveSelectedLinesToFile()
fun! MoveSelectedLinesToFile(filename)
  exec "'>,'>w! >>" . a:filename
  norm gvd
endfunc

colorscheme manni

" for gvim
set guifont=Monospace\ 9
set ch=2  " make command line two lines high
set mousehide  " turn off mouse pointer when typing begins
set mouse=c  " put mouse in command-line mode, so mouse clicks don't move cursor
set guioptions-=T  " turn off toolbar
set guioptions-=m  " turn off menus
set guioptions+=b  " add horizontal scrollbar
set nowrap  " do not wrap long lines; have them scroll off the side
" Make shift-insert work like in Xterm
map <S-Insert> <MiddleMouse>
map! <S-Insert> <MiddleMouse>

" Set startup window size, which only makes sense for gvim, not vim in
" terminal
if has("gui_running")
  set lines=60 columns=120
endif

let mapleader = ","
let maplocalleader = "\\"

" Ruby overrides
augroup filetype_ruby
  autocmd!
  autocmd FileType ruby setlocal shiftwidth=2
  autocmd FileType ruby setlocal softtabstop=2
  autocmd FileType ruby setlocal expandtab
augroup END

" golang overrides
augroup filetype_golang
  autocmd!
  autocmd FileType go setlocal shiftwidth=2
  autocmd FileType go setlocal tabstop=2
  autocmd FileType go setlocal softtabstop=0
  autocmd FileType go setlocal noexpandtab
  autocmd FileType go nmap <Leader>d <Plug>(go-def-vertical)
  autocmd FileType go nmap <Leader>D <Plug>(go-doc-vertical)
  autocmd FileType go nmap <Leader>b <Plug>(go-doc-browser)
  autocmd FileType go nmap <Leader>i <Plug>(go-implements)
  autocmd FileType go nmap <Leader>n <Plug>(go-info)
  autocmd FileType go nmap <Leader>r <Plug>(go-rename)
augroup END
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
let g:go_fmt_command = "goimports"

Step 6: Install gorename so that you can use it from within vim:

go get golang.org/x/tools/cmd/gorename

Step 7: Install gocode so that you can use it from within vim:

go get github.com/nsf/gocode
$GOPATH/bin/gocode set propose-builtins true

Step 8: Install the vim-go plugin. Launch vim, and type :PlugInstall, and the vim-go plugin will get installe. Restart vim.