" Map leader
map <Space> <leader>
map <Space><Space> <leader><leader>
let mapleader = " "

map <leader><leader><leader> :hi Normal ctermbg=NONE<CR>:hi EndOfBuffer ctermbg=NONE<CR>

" Quickfix navigation
noremap <Up> :cprev<CR>
noremap <Down> :cnext<CR>
" Play default macro
noremap <leader>q @q

" Bepo mappings
noremap t h
noremap r l
noremap T H
noremap R L
noremap L D
noremap h r
noremap H R
noremap j t
noremap l d

" Scrolling
noremap <C-D> <C-Y>
noremap <C-S> <C-E>

" Movements (by displayed line)
noremap d gk
noremap s gj

" Swap display line behavior
noremap gd k
noremap gs j
noremap 0 g0
noremap g0 0
noremap $ g$
noremap g$ $
noremap ^ g^
noremap g^ ^

" Page up/down
noremap D <C-u>
noremap S <C-d>

" Next and previous in 'f' search to match standard scheme
noremap , ;
noremap ; ,

" Windows
nmap é <C-w>
noremap <C-W>h <C-W>s<C-W>j
noremap <C-W>v <C-W>v
noremap <C-W>é <C-w><C-w>

noremap <C-W>/ 4<C-W><
noremap <C-W>* 4<C-W>>
noremap <C-W>+ 4<C-W>+
noremap <C-W>- 4<C-W>-

noremap <C-W>t <C-W>h
noremap <C-W>r <C-W>l
noremap <C-W>d <C-W>k
noremap <C-W>s <C-W>j

noremap <C-W>T <C-W>H
noremap <C-W>R <C-W>L
noremap <C-W>D <C-W>K
noremap <C-W>S <C-W>J

noremap <C-W>n :vnew<CR>
noremap <C-W>f :vsplit<CR>gf

" Tabs
nnoremap ggt gt
nnoremap ggl :tabclose<CR>
nnoremap ggn :tabnew<CR>

" buffers as tabs
nnoremap gt :bnext<CR>
nnoremap gT :bprevious<CR>
nnoremap gn :enew<CR>
nnoremap gl :bprevious<CR>:bdelete #<CR>
nnoremap gL :bprevious<CR>:bdelete! #<CR>

" fold navigation
noremap zs zj
noremap zd zk
noremap zt zm
noremap zT zM

" Spell errors navigation
noremap zv [s
noremap zl ]s

" Insert lines
noremap <leader>o o<Esc>
noremap <leader>O O<Esc>

" follow links, C-] is impossible to type
noremap _ <C-]>

" disable Ex mode
noremap Q <nop>

" have Y work like D and C (ie: yank the line right of the cursor)
nnoremap Y y$

" remap ii to Esc in insert mode, easier !
inoremap ii <Esc>

" Write with root privileges
command! WW execute "w suda://%"
command! W execute "w"

" Edit vimrc
nnoremap <leader>$ :e $MYVIMRC<CR>

" Exit terminal
tnoremap ii <C-\><C-n>
