Vim 8 Setup, September 2021

13 Sep 2021 (was 25 July 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

" try to be in the directory of the currently-active buffer
set autochdir

" 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 0 lines above or below cursor when scrolling
set scrolloff=0

" 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

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

" Turn wrap on and off
nnoremap w :set wrap!

" Disable modifyOtherKeys mode (which was putting >4;m and >4;2m on status
" line after saving Go files
let &t_TI = ""
let &t_TE = ""

" Searching
" ------------
" switch off search pattern highlighting
set nohlsearch

" Toggle hlsearch
nnoremap  :set hlsearch! hlsearch?

" 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  :set list!

" 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 

" disable bringing up a man page for the word under the cursor;
" Shift-k is too similar to the Ctrl-k binding I use for tmux,
" and I hit Shift-k accidentally too often.
nnoremap  


" 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 j 
nnoremap k 
nnoremap l 
nnoremap h 

" horizontal split
nnoremap - :split
nnoremap _ :split

" vertical split
nnoremap  :vsplit
nnoremap \ :vsplit

" close
nnoremap c :close

" remap window equal resizing to leader equals
nnoremap = 

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

" Bring up my Buf extension
nnoremap b :Buf

" 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

" Bring up explorer quickly
nnoremap e :Explore

" 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
  nnoremap i :GoInfo
  nnoremap , :GoSameIdsToggle
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_function_calls = 1
let g:go_highlight_function_arguments = 1
let g:go_highlight_variable_declarations = 1
let g:go_highlight_variable_assignments = 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_fmt_command = "gopls"
let g:go_fmt_autosave = 1
let g:go_imports_mode = "gopls"
let g:go_imports_autosave = 1
"let g:go_rename_command = "gorename"
let g:go_rename_command = "gopls"
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
let g:go_def_mode = "gopls"
"let g:go_autodetect_gopath = 1
let g:go_bin_path = $HOME."/go/bin"

" YAML
" ----
augroup filetype_yaml
  autocmd!
  autocmd FileType yaml setlocal shiftwidth=2
  autocmd FileType yaml setlocal tabstop=2
  autocmd FileType yaml setlocal softtabstop=0
  autocmd FileType yaml setlocal expandtab
augroup END

" JSON
" ----------
augroup filetype_json
  autocmd!
  autocmd FileType javascript setlocal shiftwidth=2
  autocmd FileType javascript setlocal tabstop=2
  autocmd FileType javascript setlocal softtabstop=0
  autocmd FileType javascript setlocal expandtab
augroup END
let g:jfmt_autofmt = 1

" JavaScript
" ----------
augroup filetype_javascript
  autocmd!
  autocmd FileType javascript setlocal shiftwidth=2
  autocmd FileType javascript setlocal tabstop=2
  autocmd FileType javascript setlocal softtabstop=0
  autocmd FileType javascript setlocal expandtab
augroup END

" SQL
" ----------
augroup filetype_sql
  autocmd!
  autocmd FileType sql setlocal shiftwidth=2
  autocmd FileType sql setlocal tabstop=2
  autocmd FileType sql setlocal softtabstop=0
  autocmd FileType sql setlocal expandtab
augroup END


" HTML
" ----
augroup filetype_html

  " surround the current paragraph with paragraph tags
  function HtmlPSurround()
    execute "normal! {jI<p>\<esc>}kA</p>"
  endfunction
  
  " Make :HtmlP call HtmlPSurround()
  command HtmlP call HtmlPSurround()

  " Make leader-p wrap the current para with para tags
  nnoremap <Leader>p :HtmlP<CR>

  " Surround a visual selection with <pre></pre>
  :vmap p c<pre><CR></pre><ESC>P

augroup END

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