" Leader key as ,
let mapleader=","
let maplocalleader=","

" Plugins {{{

call plug#begin()

" File management
Plug 'ctrlpvim/ctrlp.vim'
Plug 'justinmk/vim-dirvish'

" Vim behaviour
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-surround'
Plug 'wellle/targets.vim'
Plug 'justinmk/vim-sneak'
Plug 'tommcdo/vim-exchange'
Plug 'tommcdo/vim-lion'

" Colorscheme
Plug 'w0ng/vim-hybrid'

" Extras
Plug 'vimwiki/vimwiki'
Plug 'junegunn/goyo.vim'

runtime plugins.local
call plug#end()

runtime macros/matchit.vim

" }}}

" Settings {{{

filetype plugin indent on

set encoding=utf-8
set fileencoding=utf-8

" i - [noeol] instead of [Incomplete last line]
" x - [unix] instead of [unix format]
" t - Truncate file message at start if it is too long to fit on the command line
" I - Don't show the intro message when starting Vim
set shortmess=ixtI

" Appearance
if !exists("g:syntax_on")
    syntax enable
endif
colorscheme hybrid
set background=dark

set hidden

set number
set ruler
set showcmd
set cursorline
set nowrap
set laststatus=2
set scrolloff=3
set sidescrolloff=3
set synmaxcol=200 " Don't attempt to highlight lines longer than 200 chars
set listchars=tab:\|\ ,trail:∙,extends:>,precedes:<,nbsp:‡
set breakindent
set showbreak=>\ 
set breakindentopt=shift:2

set wildmenu
set wildmode=list:longest,full

" Indentation
set autoindent
set expandtab
set tabstop=4
set softtabstop=4
set shiftwidth=4
set shiftround
set backspace=indent,eol,start

" Search
set incsearch
set ignorecase
set smartcase
set infercase

" Redrawing
set lazyredraw
set ttyfast

" Disable all bells (audio, flash)
set belloff=all

" Allow the cursor to move anywhere in visual block mode
set virtualedit=block

" Automatically read the file if it changed outside vim
set autoread
set modeline
set modelines=1

" Open splits to the right and to the bottom
set splitbelow
set splitright

" Resize splits when vim itself is resized
augroup ResizeSplits
    au!
    au VimResized * :wincmd =
augroup END

" Use a nice vertical separator for splits
set fillchars+=vert:│

" Flash matching braces for 200ms
set showmatch
set matchtime=2

" Don't search through includes for completion as this can be slow
set complete-=i

" When joining lines, delete comment characters if appropriate
set formatoptions+=j
set nojoinspaces


" Backups/Swapfiles/Undofiles {{{

" Keep backups, swapfiles and undofiles if we're not root
if exists('$SUDO_USER')
    set viminfo=
    set nobackup
    set nowritebackup
    set noswapfile
    set noundofile
else
    set viminfo+=n~/.vim/tmp/viminfo
    set backupdir=~/.vim/tmp/backup
    set directory=~/.vim/tmp/swap
    set undodir=~/.vim/tmp/undo
    set undofile
endif

" }}}

" }}}

" Mappings {{{

" Sanity {{{
" These mappings improve consistency in Vim's commands

" Saner line handling
nnoremap j gj
nnoremap k gk
nnoremap gj j
nnoremap gk k
xnoremap j gj
xnoremap k gk
xnoremap gj j
xnoremap gk k

" :W and :Q still work
command! W write
command! Q quit

" Delete and paste without overwriting the paste register
nnoremap x "_x
xnoremap p "_c<Esc>p

" Make Y behave like other capitals
nnoremap Y y$

" When joining lines, keep the cursor in place
nnoremap J mzJ`z

" }}}

" Edit/source configuration
nnoremap <leader>ec :edit $MYVIMRC<cr>
nnoremap <leader>el :edit $MYVIMRC.local<cr>
nnoremap <leader>sc :source $MYVIMRC<cr>

" Remap H and L to start and end of the line
nnoremap H ^
nnoremap L $

" Easy split navigation
nnoremap <c-j> <c-w>j
nnoremap <c-k> <c-w>k
nnoremap <c-h> <c-w>h
nnoremap <c-l> <c-w>l

" <leader>a selects the whole buffer
nnoremap <leader>a ggVG

" gp selects the last pasted text
nnoremap gp `[v`]

" <leader> shortcuts for copying/pasting to/from system clipboard
nnoremap <leader>y "+y
xnoremap <leader>y "+y
nnoremap <leader>p "+p
xnoremap <leader>p "+p

" Shift-Q repeats the q macro
nnoremap Q @q
xnoremap Q :normal! @q<CR>

" Quickly edit the Q macro
nnoremap <leader>eq :<c-u><c-r><c-r>='let @q = '. string(getreg('q'))<cr><c-f><left>

" Close the current buffer with <leader>q
nnoremap <silent> <leader>q :bd<CR>

" Go back to the last buffer
nnoremap <silent> <backspace> <C-^>

" Ignore K keypresses, which do not often yield useful results
nnoremap K <nop>

" }}}

" GUI {{{
if has('gui_running')
    " Remove scrollbars and menus from the GUI
    set guioptions=c
endif
" }}}

" Plugin Settings {{{

" Dirvish {{{
let g:dirvish_mode = ':sort r /[^\/]$/'
" }}}
" CtrlP {{{
" Ignore files in .gitignore
let g:ctrlp_user_command = ['.git', 'cd %s && git ls-files -co --exclude-standard']

" Ctrl-B is Ctrl-P buffer list
" Ctrl-T is Ctrl-P tag list
nnoremap <C-B> :CtrlPBuffer<cr>
nnoremap <C-T> :CtrlPTag<cr>
"}}}
" VimWiki {{{
let g:vimwiki_dir_link = 'index'
let g:vimwiki_list = [{'path': '~/.vim/wiki/', 'syntax': 'markdown', 'ext': '.md'}]
" }}}

" }}}

runtime vimrc.local

" vim: set fdm=marker: