path: root/vim/vimrc
blob: 8673d51b909c0cc11ff01cec60e9212ffbb1203c (plain) (tree)














































































" Tom Ryder (tejr)'s vimrc: <https://sanctum.geek.nz/cgit/dotfiles.git>
" This file is designed to load without unsuppressed errors on Vim 6.0 and up.
" Anything older than that will probably break. There are also some odd bits
" of logic to cope with loading as much config as possible on vim-tiny.

" Undo anything the operating system's vimrc may have broken
runtime system.vim

" Load filetype settings, plugins, and maps
if has('autocmd')
  let g:maplocalleader = ','
  filetype plugin indent on

" Options dependent on the syntax feature
if has('syntax')

  " Use syntax highlighting
  if !exists('g:syntax_on')
    syntax enable

  " Use my colorscheme if using the GUI or if we have 256 colors
  if has('gui_running') || &t_Co >= 256
    silent! colorscheme sahara

  " If not sahara, then default with dark background
  if !exists('g:colors_name')
    set background=dark


" The all-important default indent settings; filetypes to tweak
set autoindent     " Use indent of previous line on new lines
set expandtab      " Use spaces instead of tabs
set shiftwidth=4   " Indent with four spaces
set softtabstop=4  " Insert four spaces with tab key

" Let me backspace over pretty much anything
set backspace+=eol     " Line breaks
set backspace+=indent  " Spaces from 'autoindent'
set backspace+=start   " The start of current insertion

" Try to keep backups in one system-appropriate dir
set backup
set backupdir^=~/.vim/cache/backup
if has('win32') || has('win64')
  set backupdir-=~/.vim/cache/backup
  set backupdir^=~/vimfiles/cache/backup

" Add some paths not to back up
set backupskip^=/dev/shm/*  " Shared memory RAM disk
set backupskip^=/var/tmp/*  " Debian's $TMPDIR for sudoedit(8)
if !has('unix')
  set backupskip-=/dev/shm/*
  set backupskip-=/var/tmp/*

" Indent wrapped lines
silent! set breakindent

" Clear default 'comments' value, let the filetype handle it
set comments=

" Add completion options
if exists('+completeopt')
  set completeopt+=longest  " Insert longest common substring
  set completeopt+=menuone  " Show the menu even if only one match

" Give me a prompt instead of just rejecting risky :write, :saveas
set confirm

" Only turn on 'cursorline' if my colorscheme loaded
if exists('+cursorline')
  if exists('g:colors_name') && g:colors_name ==# 'sahara'
    set cursorline

" Try to keep swapfiles in one system-appropriate dir
set directory^=~/.vim/cache/swap//
if has('win32') || has('win64')
  set directory-=~/.vim/cache/swap//
  set directory^=~/vimfiles/cache/swap//

" Use UTF-8 if we can and env LANG didn't tell us not to
if has('multi_byte') && !exists('$LANG') && &encoding ==# 'latin1'
  set encoding=utf-8

" Don't wait for a key after Escape in insert mode
" In vim-tiny but not NeoVim, so just suppress errors
silent! set noesckeys

" Fold based on indent, but only when I ask
if has('folding')
  set foldlevelstart=99
  set foldmethod=indent

" Delete comment leaders when joining lines, if supported
silent! set formatoptions+=j

" If available, use GNU grep niceties for searching
if system('grep --version') =~# '^grep (GNU grep)'
  set grepprg=grep\ -HnRs\ --exclude='.git*'

" Don't load GUI menus; set here before GUI starts
if has('gui_running')
  set guioptions+=M

" Allow buffers to have changes without being displayed
set hidden

" Keep much more command and search history
set history=2000

" Highlight completed searches; clear on reload
set hlsearch
if 1

" Don't assume I'm editing C; let the filetype set this
set include=

" Show search matches as I type my pattern
set incsearch

" Don't join lines with two spaces at the end of sentences
set nojoinspaces

" Don't show a statusline if there's only one window
" This is the Vim default, but NeoVim changed it
if &laststatus != 1
  set laststatus=1

" Don't redraw the screen during batch execution
set lazyredraw

" Break lines at word boundaries
set linebreak

" Define extra 'list' display characters
set listchars+=extends:>       " Unwrapped text to screen right
set listchars+=precedes:<      " Unwrapped text to screen left
set listchars+=tab:>-          " Tab characters, preserve width
set listchars+=trail:_         " Trailing spaces
silent! set listchars+=nbsp:+  " Non-breaking spaces

" Don't allow setting options via buffer content
set nomodeline

" Treat numbers with a leading zero as decimal, not octal
set nrformats-=octal

" Options for file search with gf/:find
set path-=/usr/include  " Let the C/C++ filetypes set that
set path+=**            " Search current directory's whole tree

" Disable command line display of file position
" This is the Vim default, but NeoVim changed it
if &ruler
  set noruler

" Make sessions usable
if exists('+sessionoptions')
  set sessionoptions-=localoptions  " No buffer options or mappings
  set sessionoptions-=options       " No global options or mappings

" Don't show startup splash screen (I donated)
set shortmess+=I

" Prefix wrapped rows with three dots
set showbreak=...

" New window positioning
set splitbelow  " Below the current window, not above
set splitright  " Right of the current window, not left

" Give me a bit longer to complete mappings
set timeoutlen=3000

" No terminal mouse, even if we could
silent! set ttymouse=

" Keep undo files, hopefully in a dedicated directory
if has('persistent_undo')
  set undofile
  if has('win32') || has('win64')
    set undodir^=~/vimfiles/cache/undo//
    set undodir^=~/.vim/cache/undo//

" Wildmenu settings; see also plugin/wildignore.vim
set wildmenu                " Use wildmenu
set wildmode=list:longest   " Tab press completes and lists
silent! set wildignorecase  " Case insensitive, if supported

" Let me move beyond buffer text in visual block mode
if exists('+virtualedit')
  set virtualedit+=block

" Never beep at me
set visualbell t_vb=

" Stack normal/visual/select Ctrl-L to clear search highlight
nnoremap <silent> <C-L> :<C-U>nohlsearch<CR><C-L>
vnoremap <silent> <C-L> :<C-U>nohlsearch<CR>gv<C-L>

" Remap insert Ctrl-C to undo the escaped insert operation
if &loadplugins
  imap <C-C> <Plug>(InsertCancel)

" Map double Ctrl-K in insert mode to search digraph names
imap <C-K><C-K> <Plug>(DigraphSearch)

" Remap normal space to scroll down a page
nnoremap <Space> <PageDown>
" If we have plugins, do a :next after hitting the last line
if &loadplugins
  nmap <Space> <Plug>(ScrollNext)

" Remap normal/visual & to preserve substitution flags
nnoremap <silent> & :&&<CR>
if exists(':xnoremap')
  xnoremap <silent> & :&&<CR>

" Map g: as a 'colon operator'
nmap g: <Plug>(ColonOperator)

" Cycle through argument list
nnoremap [a :previous<CR>
nnoremap ]a :next<CR>
" Cycle through buffers
nnoremap [b :bprevious<CR>
nnoremap ]b :bnext<CR>
" Cycle through quicklist/:helpgrep items
nnoremap [c :cprevious<CR>
nnoremap ]c :cnext<CR>
" Cycle through location list items
nnoremap [l :lprevious<CR>
nnoremap ]l :lnext<CR>

" Insert blank lines around current line
nmap [<Space> <Plug>(PutBlankLinesAbove)
nmap ]<Space> <Plug>(PutBlankLinesBelow)

" Normal leader maps; use <Bslash> not <Leader> for vim-tiny

" \a toggles 'formatoptions' 'a' flag using a plugin
nnoremap <Bslash>a :<C-U>ToggleFlagLocal formatoptions a<CR>

" \b toggles copy-pasteable linebreak settings
nmap <Bslash>b <Plug>(CopyLinebreakToggle)

" \c toggles 'cursorline'; no visual mode map as it doesn't work
nnoremap <Bslash>c :<C-U>setlocal cursorline! cursorline?<CR>
" \C toggles 'cursorcolumn'; works in visual mode
nnoremap <Bslash>C :<C-U>setlocal cursorcolumn! cursorcolumn?<CR>
if exists(':xnoremap')
  xnoremap <Bslash>C :<C-U>setlocal cursorcolumn! cursorcolumn?<CR>gv

" \d inserts the local date (POSIX date)
nnoremap <Bslash>d :read !date<CR>
" \D inserts the UTC date (POSIX date)
nnoremap <Bslash>D :read !date -u<CR>

" \e forces a buffer to be editable
nnoremap <Bslash>e :<C-U>setlocal modifiable noreadonly<CR>

" \f shows the current 'formatoptions' at a glance
nnoremap <Bslash>f :<C-U>setlocal formatoptions?<CR>

" \F reloads filetype plugins
nnoremap <Bslash>F :<C-U>doautocmd filetypedetect BufRead<CR>

" \g changes directory to the current file's location
nnoremap <Bslash>g :<C-U>cd %:h<CR>:pwd<CR>

" \h toggles highlighting search results
nnoremap <Bslash>h :<C-U>set hlsearch! hlsearch?<CR>

" \H shows command history
nnoremap <Bslash>H :<C-U>history :<CR>

" \i toggles showing matches as I enter my pattern
nnoremap <Bslash>i :<C-U>set incsearch! incsearch?<CR>

" \j jumps to buffers (jetpack)
nnoremap <Bslash>j :<C-U>buffers<CR>:buffer<Space>

" \k shows my marks
nnoremap <Bslash>k :<C-U>marks<CR>

" \l toggles showing tab, end-of-line, and trailing whitespace
nnoremap <Bslash>l :<C-U>setlocal list! list?<CR>
if exists(':xnoremap')
  xnoremap <Bslash>l :<C-U>setlocal list! list?<CR>gv

" \m shows normal maps
nnoremap <Bslash>m :<C-U>map<CR>
" \M shows buffer-local normal maps
nnoremap <Bslash>M :<C-U>map <buffer><CR>

" \n toggles line number display
nnoremap <Bslash>n :<C-U>setlocal number! number?<CR>
if exists(':xnoremap')
  xnoremap <Bslash>n :<C-U>setlocal number! number?<CR>gv
" \N toggles position display in bottom right
nnoremap <Bslash>N :<C-U>set ruler! ruler?<CR>
if exists(':xnoremap')
  xnoremap <Bslash>N :<C-U>set ruler! ruler?<CR>gv

" \o opens a line below in paste mode
nmap <Bslash>o <Plug>(PasteOpenBelow)
" \O opens a line above in paste mode
nmap <Bslash>O <Plug>(PasteOpenAbove)

" \p toggles paste mode
nnoremap <Bslash>p :<C-U>set paste! paste?<CR>

" \q formats the current paragraph
nnoremap <Bslash>q gqap

" \r acts as a replacement operator
nmap <Bslash>r <Plug>(ReplaceOperator)
if exists(':xmap')
  xmap <Bslash>r <Plug>(ReplaceOperator)

" \R reloads ~/.vimrc
nnoremap <Bslash>R :<C-U>source $MYVIMRC<CR>

" \s toggles spell checking
nnoremap <Bslash>s :<C-U>setlocal spell! spell?<CR>

" \t shows current filetype
nnoremap <Bslash>t :<C-U>setlocal filetype?<CR>
" \T clears filetype
nnoremap <Bslash>T :<C-U>setlocal filetype=<CR>

" \u sets US English spelling (compare \z)
nnoremap <Bslash>u :<C-U>setlocal spelllang=en_us<CR>

" \v shows all global variables
nnoremap <Bslash>v :<C-U>let g: v:<CR>
" \V shows all local variables
nnoremap <Bslash>V :<C-U>let b: t: w:<CR>

" \w toggles wrapping
nnoremap <Bslash>w :<C-U>setlocal wrap! wrap?<CR>
if exists(':xnoremap')
  xnoremap <Bslash>w :<C-U>setlocal wrap! wrap?<CR>gv

" \x strips trailing whitespace via a custom plugin
nmap <Bslash>x :StripTrailingWhitespace<CR>
if exists(':xmap')
  xmap <Bslash>x :StripTrailingWhitespace<CR>

" \X squeezes repeated blank lines via a custom plugin
nmap <Bslash>X :SqueezeRepeatBlanks<CR>
if exists(':xmap')
  xmap <Bslash>X :SqueezeRepeatBlanks<CR>

" \y shows all registers
nnoremap <Bslash>y :<C-U>registers<CR>

" \z sets NZ English spelling (compare \u)
nnoremap <Bslash>z :<C-U>setlocal spelllang=en_nz<CR>

" \= runs the whole buffer through =, preserving position
nnoremap <Bslash>= :<C-U>call vimrc#Anchor('1G=G')<CR>
" \+ runs the whole buffer through gq, preserving position
nnoremap <Bslash>+ :<C-U>call vimrc#Anchor('1GgqG')<CR>

" \. runs the configured make program into the location list
nnoremap <Bslash>. :<C-U>lmake!<CR>

" \< and \> adjust indent of last edit; good for pasting
nnoremap <Bslash><lt> :<C-U>'[,']<lt><CR>
nnoremap <Bslash>> :<C-U>'[,']><CR>

" \_ uses last changed or yanked text as a characterwise object
onoremap <Bslash>_ :<C-U>normal! `[v`]<CR>

" \% uses entire buffer as a linewise object
onoremap <Bslash>% :<C-U>normal! 1GVG<CR>

" \{ and \} move to lines with non-space chars before current column
nmap <Bslash>{ <Plug>(VerticalRegionUpNormal)
nmap <Bslash>} <Plug>(VerticalRegionDownNormal)
omap <Bslash>{ <Plug>(VerticalRegionUpOperator)
omap <Bslash>} <Plug>(VerticalRegionDownOperator)
if exists(':xmap')
  xmap <Bslash>{ <Plug>(VerticalRegionUpVisual)
  xmap <Bslash>} <Plug>(VerticalRegionDownVisual)

" \/ types :vimgrep for me ready to enter a search pattern
nnoremap <Bslash>/ :<C-U>vimgrep /\c/ **<S-Left><S-Left><Right>
" \? types :helpgrep for me ready to enter a search pattern
nnoremap <Bslash>? :<C-U>helpgrep \c<S-Left>

" \DEL deletes the current buffer
nnoremap <Bslash><Delete> :bdelete<CR>
" \INS edits a new buffer
nnoremap <Bslash><Insert> :<C-U>enew<CR>

" Execution mappings; each of these clobbers register z

" \@ executes line in normal mode
nnoremap <Bslash>@ ^"zyg_@z
" \: executes line in command mode
nnoremap <Bslash>: ^"zyg_:<C-R>z<CR>
" \! executes line with 'shell'
nnoremap <Bslash>! ^"zyg_:!<C-R>z<CR>

" Source any .vim files from ~/.vim/config
runtime! config/*.vim