set nocompatible              " be iMproved

" ========================================================================
" vim-plug
" ========================================================================
if has('nvim')
  call plug#begin('~/.config/nvim/plugged')
else
  call plug#begin('~/.vim/plugged')
endif

" Language specific bundles
Plug 'pangloss/vim-javascript'
Plug 'othree/html5.vim'
Plug 'docunext/closetag.vim'
Plug 'mattn/emmet-vim'
Plug 'kchmck/vim-coffee-script'
Plug 'vim-ruby/vim-ruby'
Plug 'tpope/vim-rails'
Plug 'tpope/vim-bundler'
Plug 'elixir-lang/vim-elixir'
Plug 'jtratner/vim-flavored-markdown'
Plug 'mustache/vim-mustache-handlebars'
Plug 'hashivim/vim-terraform'
Plug 'pearofducks/ansible-vim'
Plug 'fatih/vim-go'

" Vim enhancements
Plug 'junegunn/vim-easy-align'
Plug 'tpope/vim-endwise'
Plug 'tpope/vim-obsession'
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-unimpaired'
Plug 'vim-scripts/argtextobj.vim'
Plug 'airblade/vim-gitgutter'

" Easier navigation
Plug 'ctrlpvim/ctrlp.vim'
Plug 'tacahiroy/ctrlp-funky'

" Integration with other systems
Plug 'christoomey/vim-tmux-navigator'
Plug 'scrooloose/syntastic'
Plug 'rking/ag.vim'
Plug 'rizzatti/dash.vim'

" Themes
Plug 'chriskempson/base16-vim'
Plug 'bling/vim-airline'

" All of your Plug must be added before the following line
call plug#end()

if has('nvim')
  set termguicolors
endif

" Themes
set laststatus=2

" Set tabs to 2 spaces
set tabstop=2 shiftwidth=2 expandtab
set smarttab

" Highlight searches
set hlsearch
" Do incremental searching
set incsearch
nnoremap <Leader>fr :call VisualFindAndReplace()<CR>
xnoremap <Leader>fr :call VisualFindAndReplaceWithSelection()<CR>

function! VisualFindAndReplace()
    :OverCommandLine%s/
    :w
endfunction
function! VisualFindAndReplaceWithSelection() range
    :'<,'>OverCommandLine s/
    :w
endfunction

" Highlight unwanted whitespace
highlight ExtraWhitespace ctermbg=red guibg=red
au InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
au InsertLeave * match ExtraWhitespace /\s\+$/

" Set column line at 80 chars
"set colorcolumn=80
"highlight ColorColumn ctermbg=7

" ========================================================================
" Ruby config
" ========================================================================
augroup myfiletypes
  " Clear old autocmds in group
  autocmd!
  " autoindent with two spaces, always expand tabs
  autocmd FileType ruby,eruby,yaml setlocal ai sw=2 sts=2 et
  autocmd FileType ruby,eruby,yaml setlocal path+=lib
augroup END

filetype plugin on
runtime macros/matchit.vim

autocmd BufRead *_spec.rb syn keyword rubyRspec describe context it specify it_should_behave_like before after setup subject its shared_examples_for shared_context let
highlight def link rubyRspec Function

" ========================================================================
" Spelling and file specific settings
" ========================================================================

set spelllang=en

" Mark *.md files as markdown
au BufRead,BufNewFile *.md set filetype=markdown

" Enable spellchecking for Markdown and git commits
autocmd FileType markdown setlocal spell
autocmd FileType gitcommit setlocal spell
autocmd FileType gitcommit match ExtraWhitespace /\s\s+$/
set complete+=kspell

" Automatically wrap at 80 characters for Markdown, JS and Ruby
autocmd BufRead,BufNewFile *.md,*.markdown,*.js,*.rb setlocal textwidth=80

" Set spelling error to underline
hi clear SpellBad
hi SpellBad cterm=underline

" ========================================================================
" Python config
" ========================================================================
let python_highlight_all = 1
au BufNewFile,BufRead *.py
    \ set tabstop=4 |
    \ set softtabstop=4 |
    \ set shiftwidth=4 |
    \ set textwidth=79 |
    \ set expandtab |
    \ set autoindent |
    \ set fileformat=unix

" ========================================================================
" Plugin configuration
" ========================================================================
set wildignore+=*/tmp/*,*.so,*.swp,*.zip     " MacOSX/Linux
let g:ctrlp_user_command = ['.git/', 'git --git-dir=%s/.git ls-files -oc --exclude-standard']
let g:ctrlp_extensions = ['funky']
nnoremap <Leader>fu :CtrlPFunky<Cr>
nnoremap <Leader>fU :execute 'CtrlPFunky ' . expand('<cword>')<Cr>

" Syntastic should use ChefDK Ruby executable
let g:syntastic_ruby_mri_exec = '/opt/chefdk/embedded/bin/ruby'

" Easy align
" Start interactive EasyAlign for a motion/text object (e.g. gaip)
nmap ga <Plug>(EasyAlign)
" Start interactive EasyAlign in visual mode (e.g. vipga)
xmap ga <Plug>(EasyAlign)

" ========================================================================
" Base config
" ========================================================================
set autoindent
set number

" Make backspace key work in insert mode
set backspace=indent,eol,start

map <C-s> <esc>:w<CR>
imap <C-s> <esc>:w<CR>

" Emacs-like beginning and end of line.
imap <C-e> <C-o>$
imap <C-a> <C-o>^

" Vim splits open in a more natural location, i.e. to
" the right or below instead of the defaults.
" http://robots.thoughtbot.com/post/48275867281/vim-splits-move-faster-and-more-naturally
set splitbelow
set splitright

" Tab for autocomplete
inoremap <Tab> <C-P>

" Let's be reasonable, shall we?
nmap k gk
nmap j gj

" Press Space to turn off highlighting and clear any message already displayed.
:nnoremap <silent> <Space> :nohlsearch<Bar>:echo<CR>

nnoremap <silent> <BS> :TmuxNavigateLeft<cr>
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l

" ========================================================================
" Styling config
" ========================================================================
" Color schemes
colorscheme base16-default-dark
set background=dark
