" Normalization {{{
if !has('nvim')
" The following settings are the defaults in nvim. Set them also in vim for
" a consistent experience between the two
set autoindent
set autoread
set backspace=indent,eol,start
silent! set belloff=all
set complete-=i
set display=lastline
set nofsync
set formatoptions+=j
set history=10000
set hlsearch
set incsearch
set langnoremap
silent! set nolangremap
set laststatus=2
set nrformats-=octal
set ruler
set sessionoptions-=options
silent! set shortmess+=F
set showcmd
set sidescroll=1
set smarttab
set tabpagemax=50
set tags=./tags;,tags
set ttimeout
set ttimeoutlen=50
set ttyfast
set viminfo^=!
set wildmenu
if has('multi_byte')
set encoding=utf-8
scriptencoding utf-8
endif
" Set cursor shape based on mode (:h termcap-cursor-shape)
" Vertical bar in insert mode
let &t_SI = "\e[6 q"
" Underline in replace mode
let &t_SR = "\e[4 q"
" Block in normal mode
let &t_EI = "\e[2 q"
let s:datadir = vim#datadir()
let &backupdir = s:datadir . '/backup//,' . &backupdir
let &directory = s:datadir . '/swap//,' . &directory
if has('persistent_undo')
let &undodir = s:datadir . '/undo//,' . &undodir
endif
" Create directories if they don't exist
if exists('*mkdir')
let s:dirs = [&backupdir, &directory]
if has('persistent_undo')
let s:dirs += [&undodir]
endif
for s:opt in s:dirs
let s:dir = simplify(split(s:opt, ',')[0])
if !isdirectory(s:dir)
call mkdir(s:dir, 'p')
endif
endfor
unlet s:dirs s:opt s:dir
endif
endif
" }}}
" Clear all vimrc autocmds at the beginning
augroup vimrc | execute 'autocmd!' | augroup END
" Setup {{{
if has('autocmd')
filetype plugin indent on
endif
if has('syntax') && !exists('g:syntax_on')
syntax enable
endif
" Set $VIMHOME
let $VIMHOME = split(&runtimepath, ',')[0]
" Use vim-pathogen if native packages are not supported
if !has('packages')
" Use vim-pathogen to emulate native packaging behavior
if empty(glob($VIMHOME . '/autoload/pathogen.vim'))
silent execute '!curl -LSso ' . $VIMHOME . '/autoload/pathogen.vim https://tpo.pe/pathogen.vim'
endif
silent! execute pathogen#infect('pack/{}/start/{}', 'pack/{}/opt/{}')
endif
" }}}
" Colors {{{
" Try to make comments italic
autocmd vimrc ColorScheme * highlight Comment gui=italic cterm=italic
" Load colorscheme
silent! colorscheme base16-eighties
" Highlight trailing whitespace
highlight link TrailingWhitespace Error
" }}}
" Settings {{{
set backup
set backupdir-=.
set confirm
set cursorline
set expandtab
set hidden
set ignorecase
set lazyredraw
set listchars=tab:>\ ,trail:-,extends:>,precedes:<,nbsp:+
set matchpairs+=<:>
set pumheight=10
set scrolloff=2
set shell=/bin/sh
set shiftwidth=4
set showmatch
set sidescrolloff=5
set smartcase
set softtabstop=-1
set noswapfile
silent! set tagcase=match
set virtualedit+=block
set wildcharm=<Tab>
set wildignorecase
set wildmode=longest:full,full
if has('wildignore')
set wildignore+=*.pyc,__pycache__,.DS_Store,*~,#*#
endif
if has('mouse')
set mouse=a
endif
if has('folding')
set foldlevelstart=99
endif
if has('persistent_undo')
set undofile
endif
" Color the whole area outside of 'textwidth'
if has('syntax')
let &colorcolumn = '+' . join(range(1, 256), ',+')
endif
if has('linebreak')
set linebreak
set breakindent
endif
if has('textprop')
set completeopt+=popup
set completepopup=highlight:Pmenu,align:menu,border:off
endif
" Set default path variables to blank defaults
setglobal path=.,,
setglobal include=
setglobal includeexpr=
setglobal define=
setglobal isfname+=@-@
" Use a better grepprg if available
set grepformat^=%f:%l:%c:%m
if executable('rg')
set grepprg=rg\ --vimgrep\ --smart-case
elseif executable('ag')
set grepprg=ag\ --vimgrep\ --smart-case
elseif executable('grep')
set grepprg=grep\ --line-number\ --recursive\ -I\ $*\ *
else
set grepprg=internal
endif
" Enable histogram diff
if has('nvim-0.3.2') || has('patch-8.1.0360')
set diffopt=filler,internal,algorithm:histogram,indent-heuristic
endif
" }}}
" Mappings {{{
" Insert {{{
" jk in Insert mode escapes to Normal mode
inoremap jk <Esc>
" Set undo points when deleting
inoremap <C-U> <C-G>u<C-U>
inoremap <C-W> <C-G>u<C-W>
" Auto close braces in insert mode
inoremap {<CR> {<CR>}<Esc>O
" Copy line above/below (similar to <C-Y>/<C-E> but works on the whole line)
inoremap <expr> <C-G><C-Y> repeat('<C-Y>', len(getline(line('.')-1))-col('.')+1)
inoremap <expr> <C-G><C-E> repeat('<C-E>', len(getline(line('.')-1))-col('.')+1)
" Navigate through popup menu with Tab and S-Tab and use Enter to make a
" selection. If popup menu is not visible, Enter should set an undo point
" before inserting a newline
inoremap <expr> <Tab> pumvisible() ? "\<C-N>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-P>" : "\<S-Tab>"
inoremap <expr> <CR> pumvisible() ? "\<C-Y>" : "\<C-]><C-G>u\<CR>"
inoremap <C-R>! <C-O>:r !
" }}}
" Visual {{{
xnoremap * y/\V<C-R>"<CR>
xnoremap # y?\V<C-R>"<CR>
" }}}
" Normal {{{
nnoremap <expr> j (v:count == 0 ? 'gj' : 'j')
nnoremap <expr> k (v:count == 0 ? 'gk' : 'k')
nnoremap ^ g^
nnoremap $ g$
nnoremap 0 g0
nnoremap g^ ^
nnoremap g$ $
nnoremap g0 0
" Set Q and gQ to Nop to prevent accidentally entering command mode
nnoremap Q <Nop>
nnoremap gQ <Nop>
" Write file with <Space>w
nnoremap <Space>w :w<CR>
" List buffers and put :b on the command line
nnoremap <Space>b :ls<CR>:b<Space>
nnoremap <C-W><Space>b :ls<CR>:sb<Space>
" Search for a tag by regex
nnoremap <Space>] :tjump /
nnoremap <C-W><Space>] :stjump /
nnoremap <Space>} :ptjump /
" Find files in current buffer's directory
nnoremap <Space>e :e %:p:h/<Tab>
nnoremap <C-W><Space>e :sp %:p:h/<Tab>
" Clear search buffer with <C-L>
nnoremap <silent> <C-L> :<C-U>nohlsearch<Bar>diffupdate<Bar>syn sync fromstart<CR><C-L>
vnoremap <silent> <C-L> :<C-U>nohlsearch<Bar>diffupdate<Bar>syn sync fromstart<CR>gv<C-L>
" Format whole buffer with formatprg without changing cursor position
" See :h restore-position
nnoremap gq<CR> mzHmygggqG`yzt`z
" Allow [[ and friends to work when opening brace is not in the first column
noremap <silent> [[ ?{<CR>w99[{:let @/ = ''<CR>
noremap <silent> ][ /}<CR>b99]}:let @/ = ''<CR>
noremap <silent> ]] j0?{<CR>w99[{%/{<CR>:let @/ = ''<CR>
noremap <silent> [] k$/}<CR>b99]}%?}<CR>:let @/ = ''<CR>
" Unimpaired style mappings
nnoremap <expr> [a ":\<C-U>" . v:count1 . "prev\<CR>"
nnoremap <expr> ]a ":\<C-U>" . v:count1 . "next\<CR>"
nnoremap <expr> [b ":\<C-U>" . v:count1 . "bprev\<CR>"
nnoremap <expr> ]b ":\<C-U>" . v:count1 . "bnext\<CR>"
nnoremap <expr> [l ":\<C-U>" . v:count1 . "lprev\<CR>"
nnoremap <expr> ]l ":\<C-U>" . v:count1 . "lnext\<CR>"
nnoremap <expr> [q ":\<C-U>" . v:count1 . "cprev\<CR>"
nnoremap <expr> ]q ":\<C-U>" . v:count1 . "cnext\<CR>"
nnoremap <expr> [t ":\<C-U>" . v:count1 . "tprev\<CR>"
nnoremap <expr> ]t ":\<C-U>" . v:count1 . "tnext\<CR>"
nnoremap [A :<C-U>first<CR>
nnoremap ]A :<C-U>last<CR>
nnoremap [B :<C-U>bfirst<CR>
nnoremap ]B :<C-U>blast<CR>
nnoremap [L :<C-U>lfirst<CR>
nnoremap ]L :<C-U>llast<CR>
nnoremap [Q :<C-U>cfirst<CR>
nnoremap ]Q :<C-U>clast<CR>
nnoremap [T :<C-U>tfirst<CR>
nnoremap ]T :<C-U>tlast<CR>
nnoremap <expr> [e ":\<C-U>.move --" . v:count1 . "\<CR>"
nnoremap <expr> ]e ":\<C-U>.move +" . v:count1 . "\<CR>"
xnoremap <expr> [e ":move --" . v:count1 . "\<CR>gv"
xnoremap <expr> ]e ":move +" . (v:count1 + line("'>") - line("'<")) . "\<CR>gv"
nnoremap [<Space> :<C-U>put! =repeat(nr2char(10), v:count1)<CR><CR>:']+1<CR>
nnoremap ]<Space> :<C-U>put =repeat(nr2char(10), v:count1)<CR><CR>:'[-1<CR>
nnoremap <expr> yon ':setlocal ' . (&number ? 'no' : '') . "number\<CR>"
nnoremap <expr> yor ':setlocal ' . (&relativenumber ? 'no' : '') . "relativenumber\<CR>"
nnoremap <expr> yol ':setlocal ' . (&list ? 'no' : '') . "list\<CR>"
nnoremap <expr> yoc ':setlocal ' . (&cursorline ? 'no' : '') . "cursorline\<CR>"
nnoremap <expr> yo<Bar> ':setlocal ' . (&cursorcolumn ? 'no' : '') . "cursorcolumn\<CR>"
nnoremap <expr> yod ':' . (&diff ? 'diffoff' : 'diffthis') . "\<CR>"
nnoremap <expr> yos ':setlocal ' . (&spell ? 'no' : '') . "spell\<CR>"
function! Substitute(type, ...) abort
call feedkeys(":'[,']s/" . expand('<cword>') . "//g\<Left>\<Left>", 'n')
endfunction
nnoremap <silent> gS :set operatorfunc=Substitute<CR>g@
function! Sort(type, ...) abort
'[,']sort
call setpos('.', getpos("''"))
endfunction
nnoremap <silent> gs m':set operatorfunc=Sort<CR>g@
xnoremap <silent> gs :sort<CR>
nnoremap g/ :<C-U>g//#<Left><Left>
" }}}
" Terminal {{{
if exists(':tmap')
tnoremap <Esc> <C-\><C-N>
tnoremap <C-\><C-W> <C-\><C-N><C-W>
if has('nvim')
tnoremap <expr> <C-\><C-R> '<C-\><C-N>"' . nr2char(getchar()) . 'pi'
else
tnoremap <C-\><C-R> <C-W>"
tnoremap <C-W> <C-W>.
endif
endif
" }}}
" Command {{{
cnoremap <expr> <C-P> wildmenumode() ? "\<C-P>" : "\<Up>"
cnoremap <expr> <C-N> wildmenumode() ? "\<C-N>" : "\<Down>"
if has('nvim') && &wildoptions =~# 'pum'
cnoremap <expr> <C-J> pumvisible() ? "\<Down>\<Tab>" : "\<C-J>"
cnoremap <expr> <C-K> pumvisible() ? "\<Up>\<Tab>" : "\<C-K>"
else
cnoremap <expr> <C-J> wildmenumode() ? "\<Down>\<Tab>" : "\<C-J>"
cnoremap <expr> <C-K> wildmenumode() ? "\<Up>\<Tab>" : "\<C-K>"
endif
" }}}
" }}}
" Autocommands {{{
augroup vimrc
" Set marks by filetype for quick navigation
autocmd BufLeave *.h mark H
autocmd BufLeave *.{c,cc,cpp} mark C
autocmd BufLeave *.py mark P
" Close preview window with q
autocmd BufWinEnter * if &previewwindow | nnoremap <buffer> q <C-W>q | endif
" Start insert mode when entering terminal
if exists('##TermOpen')
autocmd TermOpen * startinsert
autocmd BufEnter term://* startinsert
endif
" Enable omnicompletion from syntax if no other option available
" See :h ft-syntax-omni
if exists('+omnifunc')
autocmd FileType * if &omnifunc == '' | setlocal omnifunc=syntaxcomplete#Complete | endif
endif
if argc() == 0 && filereadable('Session.vim')
if v:vim_did_enter
source Session.vim
else
autocmd VimEnter * nested source Session.vim
endif
endif
augroup END
" }}}