[ Home ]

Vim 8 Setup, November 2017

Time for another vimrc refresh for Vim 8!

Step 1: Blow away .exrc and .gvimrc if they exist from previous versions of Vim.

Step 2: Set up your .bashrc for Vim:

# 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: Put my color scheme in ~/.vim/colors/manni.vim:

set background=light

if version > 580
    highlight clear
    if exists("syntax_on")
        syntax reset
    endif
endif

let colors_name = "manni"

" For explanation of highlight group names, see:
" http://vimdoc.sourceforge.net/htmldoc/syntax.html#highlight-groups
" For what the colors look like, see:
" https://jonasjacek.github.io/colors/

highlight TabLine           NONE
highlight TabLinefill       NONE
highlight Error             term=none      cterm=none   ctermfg=Black   ctermbg=Red
highlight StatusLine        term=none      cterm=none   ctermfg=White   ctermbg=Black
highlight StatusLineNC      term=none      cterm=none   ctermfg=Grey    ctermbg=Black

highlight DiffFile                         cterm=bold
highlight DiffLine                                      ctermfg=DarkCyan
highlight DiffAdded                                     ctermfg=DarkGreen
highlight DiffRemoved                                   ctermfg=DarkRed

highlight Normal            term=none      cterm=none   ctermfg=Black   ctermbg=255 "Grey93
highlight Comment           term=bold      cterm=none   ctermfg=244                 "Grey50
highlight Constant          term=underline cterm=none   ctermfg=124                 "Red3
highlight Label             term=underline cterm=none   ctermfg=124                 "Red3
highlight Special           term=bold      cterm=none   ctermfg=Black
highlight Identifier        term=underline cterm=none   ctermfg=Black
highlight Statement         term=bold      cterm=none   ctermfg=22                  "DarkGreen
highlight PreProc           term=underline cterm=none   ctermfg=94                  "Orange4
highlight Type              term=underline cterm=none   ctermfg=89                  "DeepPink4
highlight Visual            term=bold      cterm=none   ctermbg=251                 "Grey78
highlight Search            term=bold      cterm=none   ctermbg=224                 "MistyRose1
highlight CursorLine        term=none      cterm=none   ctermbg=254                 "Grey89
highlight Title             term=none      cterm=none   ctermfg=4                   "Navy
highlight Function          term=none      cterm=none   ctermfg=18                  "DarkBlue
highlight NonText           term=none      cterm=none   ctermfg=250                 "Grey74
highlight SpecialKey        term=none      cterm=none   ctermfg=250                 "Grey74
highlight LineNr            term=none      cterm=none   ctermfg=4                   "Navy

" Go
" ==

" Make things like %s the same color as string literals, but bold them
highlight goSpecialString   term=none      cterm=bold   ctermfg=124                 "Red3

" Make function and method calls the same color as function definitions
highlight goFunctionCall    term=none      cterm=none   ctermfg=18                  "DarkBlue
highlight goMethodCall      term=none      cterm=none   ctermfg=18                  "DarkBlue

Step 4: Put this in ~/.vimrc

" enable vim features instead of strict vi compatibility
set nocompatible

" do not wrap long lines; have them scroll off the side
set nowrap

" try to detect the filetype
filetype on

" enable syntax highlighting
syntax 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

" tell vim it's OK to use 256 colors at the terminal
set t_Co=256

" show matching brackets and parens
set showmatch

" don't bother redrawing during macros and such
"set lazyredraw

" use pop-up menu for tab completion of filenames
set wildmenu

" 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
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

" highlight the line the cursor is on
" set cursorline " leave off for now; ugly when listing hidden chars

" turn on line numbers
"set number  " leave off for now

" 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

" switch off search pattern highlighting
set nohlsearch

" make command line one line high
set ch=1

" turn off mouse pointer when typing begins
set mousehide

" put mouse in command-line mode, so mouse clicks don't move cursor
set mouse=c

" set manni color scheme
colorscheme manni

" set leader to comma
let mapleader = ","

" local leader to backslash. (local leader is used for only specific file
" types)
let maplocalleader = "\\"

" Searching
" ------------
" Highlight matches in search
set hlsearch

" Turn off search highlight when done
nnoremap <Leader><Space> :nohlsearch<CR>

" Listing unprintable chars
" ------------
" Change vertical split character from a bar to a space
" (so note that trailing space here is significant)
set fillchars+=vert:\ 

" Show spaces and tabs; to turn off for copying, use `:set nolist`
set listchars=tab:→\ ,space:·,trail:·,nbsp:·

" Default to listing hidden chars
set list

" Toggle listing hidden chars
nnoremap <Leader><CR> :set list!<CR>

" Status Line
" ------------
" %< 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

" disable recording macros: I hit this key accidentally too often
nnoremap q <Nop>

" Window Manipulation
" -------------------
" Note to self:
" :resize will resize a horizontal split, *and* you can give it
" relative lines, like :resize +5 or :resize -5
" :vertical resize can be used for vertical splits.

" remap window switching to leader then navigation letter
nnoremap <Leader>j <C-W><C-J>
nnoremap <Leader>k <C-W><C-K>
nnoremap <Leader>l <C-W><C-L>
nnoremap <Leader>h <C-W><C-H>

" remap window equal resizing to leader equals
nnoremap <Leader>= <C-W><C-=>

" when opening horizontal splits, place cursor in new split
set splitbelow

" when opening vertical splits, place cursor in new split
set splitright

function DestroyButKeepWindow()
  " get the number of the buffer we will close
  let l:current_bufnum = bufnr('%')
  " open a new empty buffer in this window
  enew
  " destroy the original buffer
  execute "bdelete " . l:current_bufnum
endfunction

" Make :Bd call the window closer
command Bd call DestroyButKeepWindow()

" Explore
" -------
" Note to self:
" :Explore brings up explorer
" :Sexplore brings up explorer in a horizontal split
" :Vexplore brings up explorer in a vertical split

" Remove explorer banner
let g:netrw_banner = 0

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

" Golang
" ------
augroup filetype_golang
  " when we use :make, autosave file; used by vim-go
  set autowrite

  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_array_whitespace_error = 1
let g:go_highlight_chan_whitespace_error = 1
let g:go_highlight_space_tab_error = 1
let g:go_highlight_trailing_whitespace_error = 1
let g:go_highlight_format_strings = 1
let g:go_highlight_types = 1
let g:go_highlight_fields = 1
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_extra_types = 1
let g:go_highlight_build_constraints = 1
let g:go_highlight_generate_tags = 1
let g:go_fmt_command = "goimports"
let g:go_metalinter_enabled = ['vet', 'golint', 'errcheck']
"These next two were too invasive, so I disabled them.
"let g:go_metalinter_autosave = 1
"let g:go_metalinter_autosave_enabled = ['vet', 'golint', 'errcheck']
"This next one was too distracting (but cool!); just use :GoInfo
"let g:go_auto_type_info = 1

" Use j and k to navigate up and down autocomplete,
" at the expense of not being able to type j and k
inoremap <expr> j ((pumvisible())?("\<C-n>"):("j"))
inoremap <expr> k ((pumvisible())?("\<C-p>"):("k"))

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

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

Step 6: 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 7: Install the vim-go plugin. cd to ~/.vim/pack/plugins/start and run

git clone https://github.com/fatih/vim-go.git ~/.vim/pack/plugins/start/vim-go

Step 8: Install my own Bug plugin:

git clone https://github.com/manniwood/vim-buf.git \
    ~/.vim/pack/plugins/start/vim-buf