summaryrefslogtreecommitdiffstats
path: root/nvim/init.vim
blob: 6de99ffe6446d1c8dc0a054ffecb7847ca104b35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
"LEMON'S VIMRC - 16 NOVEMBER 2014
" Preamble 
set t_Co=256
set background=dark
set path+=**
"

"
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>


" vertical diff please
set diffopt=vertical


" Remap the leader key
let mapleader = ","
 
" map ESCAPE to exit to Normal mode in neovim terminal 
tnoremap <Esc> <C-\><C-N>
  
 
" Signify mapping & config
nnoremap <leader>R :SignifyRefresh<CR>
let g:signify_realtime = 1

 
" Basic options 

set cmdheight=1
set termguicolors
set inccommand=split
set foldmethod=manual
set encoding=utf-8
set modelines=0
set relativenumber
set autoindent
set showmode
set cursorline
set ttyfast
set ttimeout
set notimeout
set nottimeout
set backspace=indent,eol,start
set ruler
set laststatus=2
set history=1000         " remember more commands and search history
set undolevels=1000      " use many muchos levels of undo
set wildignore=*.swp,*.bak,*.pyc,*.class
set title                " change the terminal's title
set visualbell           " don't beep
set noerrorbells         " don't beep
set number
set fillchars=diff:⣿,vert:\|
set noswapfile
set ruler               " show the cursor position all the time
set showcmd             " display incomplete commands
set vb
"set list
set listchars=tab:▸\ ,eol:¬,extends:❯,precedes:❮
set lazyredraw
set matchtime=3
set showbreak=↪
set shell=/usr/bin/fish
set splitbelow
set splitright
set autowrite
set autoread
set linebreak
set hidden
"set colorcolumn=80
"
" Wildmenu completion 
set wildmenu
" set wildmode=list:longest " don't like this
set wildignore+=.hg,.git,.svn                    " Version control
set wildignore+=*.aux,*.out,*.toc                " LaTeX intermediate files
set wildignore+=*.jpg,*.bmp,*.gif,*.png,*.jpeg   " binary images
set wildignore+=*.o,*.obj,*.exe,*.dll,*.manifest " compiled object files
set wildignore+=*.spl                            " compiled spelling word lists
set wildignore+=*.sw?                            " Vim swap files
set wildignore+=*.DS_Store                       " OSX bullshit
set wildignore+=*.luac                           " Lua byte code
set wildignore+=migrations                       " Django migrations
set wildignore+=*.pyc                            " Python byte code
set wildignore+=*.orig                           " Merge resolution files
" Clojure/Leiningen
set wildignore+=classes
set wildignore+=lib

" Make Vim able to edit crontab files ... 
set backupskip=/tmp/*,/private/tmp/*"
"
" Remaps ...
"NERDTree!
nmap \e :NERDTreeToggle<CR>
let NERDTreeIgnore=['\.pyc$', '\~$'] "ignore files in NERDTree

" targbar handler
nmap <F8> :TagbarToggle<CR>

" Don't use Ex mode, use Q for formatting
"map Q gq

" easier formatting of paragraphs
" from https://github.com/thesheff17/youtube/blob/master/vim/vimrc
vmap Q gq
nmap Q gqap

" set trailing whitespace to be visible with using special chars with ,s
"set listchars=tab:>-,trail:*,eol:$
"nmap <silent> <leader>s :set nolist!<CR>
" some misc settings
:nmap \l :setlocal number!<CR>
:nmap \o :set paste!<CR>

" map sort function to a key
" from https://github.com/thesheff17/youtube/blob/master/vim/vimrc
vnoremap <Leader>s :sort<CR>

" Plug Plugs...
call plug#begin('~/.config/nvim/plugged')
if has('nvim')
  Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
else
  Plug 'Shougo/deoplete.nvim'
  Plug 'roxma/nvim-yarp'
  Plug 'roxma/vim-hug-neovim-rpc'
endif
Plug 'google/yapf', { 'rtp': 'plugins/vim', 'for': 'python' }
Plug 'deoplete-plugins/deoplete-go', {'do': 'make'}
Plug 'rust-lang/rust.vim'
Plug 'tpope/vim-commentary'
Plug 'junegunn/fzf'
Plug 'junegunn/fzf.vim'
Plug 'zchee/deoplete-jedi'
Plug 'davidhalter/jedi'
Plug 'mhinz/vim-signify'  " marking changes in repo
Plug 'anekos/hledger-vim'
Plug 'fatih/vim-go', {'do': ':GoUpdateBinaries'}
Plug 'buoto/gotests-vim'
Plug 'zchee/deoplete-clang'
Plug 'w0rp/ale'
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-unimpaired'
Plug 'tpope/vim-surround'
Plug 'scrooloose/nerdtree'
Plug 'majutsushi/tagbar'
Plug 'SirVer/ultisnips'
Plug 'honza/vim-snippets'
Plug 'jlanzarotta/bufexplorer'
Plug 'itchyny/lightline.vim'
Plug 'janko-m/vim-test'
Plug 'tpope/vim-dispatch'
Plug 'radenling/vim-dispatch-neovim'
Plug 'Shougo/neoinclude.vim'
Plug 'psf/black', { 'branch': 'stable' }
Plug 'sebdah/vim-delve'
Plug 'tweekmonster/django-plus.vim'
Plug 'junegunn/limelight.vim'
Plug 'junegunn/vim-peekaboo'
call plug#end()

" Lightline config
let g:lightline = {
      \ 'colorscheme': 'default',
      \ 'active': {
      \   'left': [ [ 'mode', 'paste' ],
      \             [ 'gitbranch', 'readonly', 'filename', 'modified' ] ]
      \ },
      \ 'component_function': {
      \   'gitbranch': 'FugitiveHead'
      \ },
      \ }

" point neovim to virtualenv specific to neovim (using 3.7)
let g:python3_host_prog = "/home/lemon/.virtualenvs/pynvim/bin/python3"
let g:deoplete#enable_at_startup = 1

"" Some servers have issues with backup files, see #649
set nobackup
set nowritebackup
"
"" Better display for messages
"set cmdheight=2
"
"" You will have bad experience for diagnostic messages when it's default 4000.
"set updatetime=300
"
"" don't give |ins-completion-menu| messages.
"set shortmess+=c
"
"" always show signcolumns
"set signcolumn=yes
"

" .. Rust
let g:autofmt_autosave = 1
" 

" vim-test config ..
" use vim-dispatch to run tests in the quickfix window
" from Modern Vim Ch.4
" the mappings below are from vim-test
" https://github.com/janko/vim-test
" dispatch opens send test output to quickfix window
"let test#strategy = "dispatch" 
" neovim strategy runs the test in the neovim terminal
let test#strategy = "neovim"
let test#python#runner = "pytest"
let test#neovim#term_position = "topleft"
let test#python#pytest#options = '--tb=short --color=no'
let test#go#runner = 'gotest'
"let test#go#gotest#options = '-v'

" To run mypy using vim-dispath -with Dispatch
autocmd FileType python let b:dispatch = 'mypy --ignore-missing-imports'

nmap <silent> t<C-n> :TestNearest<CR>
nmap <silent> t<C-f> :TestFile<CR>
nmap <silent> t<C-s> :TestSuite<CR>
"nmap <silent> t<C-l> :TestLast<CR>
nmap <silent> <S-F10> :TestLast<CR>
nmap <silent> t<C-g> :TestVisit<CR>
"
"
"
" clang support ... for Debian Stretch only!
"let g:deoplete#sources#clang#libclang_path = '/usr/lib/llvm-3.8/lib/libclang.so.1' 
"let g:deoplete#sources#clang#clang_header = '/usr/lib/llvm-3.8/lib/clang/3.8.1/include'
" works on Debian Buster anyway
"let g:deoplete#sources#clang#clang_header = '/usr/lib/llvm-6.0/lib/clang/6.0.1/include/'
let g:deoplete#sources#clang#clang_header = '/usr/include/clang/10/include'
let g:deoplete#sources#clang#libclang_path = '/usr/lib/llvm-10/lib/libclang-10.so.1'
"
" deoplete omnifuc 
let g:deoplete#enable_at_startup = 1
let g:deoplete#disable_auto_complete = 0
"call deoplete#custom#option('auto_complete', 0)
"call deoplete#custom#source('jedi', 'debug_enabled', 1)
"call deoplete#enable_logging('DEBUG', '/tmp/deoplete.log')
"inoremap <silent><expr><C-Space> deoplete#mappings#manual_complete()
"
" jedi-specific stuff
" WHICH DOES NOT APPEAR TO WORK
" USE CTAGS TO GENERATE FILE FOR NAVIGATION
" e.g ctags ~/.virtualenvs/ctrack/lib64/python3.8/site-packages/**/*.py
" To pull in a virtualenv - file kept in root of project will automatically
" be picked up
" disable autocompletion, cause we use deoplete for completion
let g:jedi#completions_enabled = 0

" open the go-to function in split, not another buffer
let g:jedi#use_splits_not_buffers = "right"

let g:jedi#goto_command = "<leader>d"
let g:jedi#goto_assignments_command = "<leader>g"
let g:jedi#goto_stubs_command = "<leader>s"
let g:jedi#goto_definitions_command = ""
let g:jedi#documentation_command = "K"
let g:jedi#usages_command = "<leader>n"
let g:jedi#completions_command = "<C-Space>"
let g:jedi#rename_command = "<leader>r"
"
" golang stuff 
let g:go_term_mode = "split"
let g:go_term_height = 10
"autocmd FileType go nmap <leader>r :w<CR>:split <bar> terminal go run %<CR>
autocmd FileType go nmap <leader>r :GoRun<CR>
" highlights the variable in the file for you..
"let g:go_auto_sameids = 1
" auto import...
let g:go_fmt_command = "goimports"
" automatic type info on cursor
let g:go_auto_type_info = 1
au Filetype go nmap <leader>ga <Plug>(go-alternate-edit)
au Filetype go nmap <leader>gah <Plug>(go-alternate-split)
au Filetype go nmap <leader>gav <Plug>(go-alternate-vertical)
au FileType go nmap <F10> :GoTest -short<cr>
au FileType go nmap <F9> :DlvToggleBreakpoint<CR>
au FileType go nmap <S-F9> :DlvTest<CR>
au FileType go nmap <F11> :DlvDebug<CR>
au FileType go nmap <S-F5> :GoRename<CR>
 
 
" ultisnips... 
"inoremap <silent><expr><TAB> pumvisible() ? "\<C-n>" : "\<TAB>"
"let g:UltiSnipsExpandTrigger="<tab>"
"let g:UltiSnipsSnippetsDir = "/home/lemon/dotfiles/Ultisnips"
"let g:UltiSnipsSnippetDirectories=["/home/lemon/dotfiles/Ultisnips"]
"" let g:UltiSnipsJumpForwardTrigger="<c-n>"
"" let g:UltiSnipsJumpBackwardTrigger="<c-p>"
"let g:UltiSnipsJumpForwardTrigger="<c-j>"
"let g:UltiSnipsJumpBackwardTrigger="<c-k>"
 
" yapf - format python code (https://github.com/google/yapf)...
map <leader>y :call yapf#YAPF()<cr>
imap <leader>y :call yapf#YAPF()<cr>
"autocmd FileType python nnoremap <leader>yy :0,$!yapf<Cr><C-o>
  
 
" isort - sort python imports (https://github.com/timothycrosley/isort) ...
let maplocalleader = "\\"
autocmd FileType python nnoremap <LocalLeader>j :!isort %<CR><CR>
  
 
" Tabs, spaces, wrapping ...
set tabstop=4
set shiftwidth=4
set shiftround
set softtabstop=4
set expandtab
set tw=79 " width of document(used by gd)
set nowrap "don't automatically wrap on load
set formatoptions=qrn1j
set fo-=t " don't automatically wrap text when typing
" 
"
" Enable folding ...
set foldmethod=manual
set foldlevel=99
"
 
 
" Backups ...
set undodir=~/.nvim-tmp/tmp/undo//			"undo files
set backupdir=~/.nvim-tmp/tmp/backup//		" backups
set directory=~/.nvim-tmp/tmp/swap//			" swap files
set backup								" enable backups
"
" Highlight VCS conflict markers ...
match ErrorMsg '^\(<\|=\|>\)\{7\}\([^=].\+\)\?$'
 
" Searching and movement ...
nnoremap / /\v
vnoremap / /\v
set ignorecase
set smartcase
set incsearch
set showmatch
set hlsearch
set gdefault
set scrolloff=3
set sidescroll=1
set sidescrolloff=10
set virtualedit+=block
nnoremap <leader><space> :noh<cr>:call clearmatches()<cr>

nmap \x :cclose<CR>

" Don't move on *
nnoremap * *<c-o>

" Easier to type move extreme left and right
noremap H ^
noremap L g_

" Use ctrl-e and ctrl-a
inoremap <c-a> <esc>I
inoremap <c-e> <esc>A

" open a Quickfix window for the last search
nnoremap <silent> <leader>/ :execute 'vimgrep /'.@/.'/g %'<CR>:copen<CR>

" ##################################
" Ag!
"
" How to do search (May 2020)
"
" lgrep -R "search term" .venv 
"
" (ALE currently hijacks the quicklist which is why we need l there)
"  - to search current directory including
" virtualenv. Results will open in :lopen (loclist)

" :Ag
"
" Will do nice FZF search, but not into gitignored or hidden files.
"
" !ag -Q "breakpoint()" .venv
"
" will do an exact match search but not populate the loclist.
" ##################################
"
"
let g:ackprg = 'ag --smart-case --nogroup --nocolor --column'
" WARNING - IF REPLACE GREP WITH AG, IT DOESN'T POPULATE THE QUICKFIX
" WHICH IS SOMETHING I WANT FOR BIG SEARCHES
"if executable('ag')
"    set grepprg=ag\ --nogroup\ --smart-case\ --nocolor\ --column
"    set grepformat=%f:%l:%c:%m
"endif
" bind \ (backward slash) to grep shortcut
" THIS ALLOWS AN ag SEARCH AND PUTS OUTPUT IN Quick Fix window
" this is great for passing ag flags, such as .venv or Q or whatever
"command -nargs=+ -complete=file -bar Agg silent! grep! <args>|cwindow|redraw!


" (example use :14 to select from menu)
nnoremap <leader>S :g//#<left><left>


" Folding ... 
" Enable folding with the spacebar
nnoremap <space> za
nnoremap <leader>fm :set foldmethod=manual<CR>
" Make zO recursively open whatever fold we're in, even if it's partially open.
nnoremap zO zczO
" Use ,z to "focus" the current fold
nnoremap <leader>z zMzvzz

"" Quick Editing ...
nnoremap <leader>ev <C-w>s<C-w>j<C-w>L:e $MYVIMRC<cr>

" Convenience mappings ... 
" No more help key.
noremap  <F1> :checktime<cr>
inoremap <F1> <esc>:checktime<cr>

" Kill window
"nnoremap <leader>K :q<cr>

" Wrap
nnoremap <leader>W :set wrap!<cr>

" Change case
"noremap <C-S-u> gUiw
"inoremap <C-S-u> <esc> gUiwea

" Substitute
nnoremap <leader>su :%s//<left>
nnoremap <Leader>s :%s/<C-r><C-w>//g<Left><Left>
"
" Emacs bindings in command line mode
cnoremap <c-a> <home>
cnoremap <c-e> <end>

" Turn off vimdiff
nnoremap <leader>D :diffoff!<cr>

" Formatting, like Textmate
nnoremap Q gqip

" Easier linewise reselection
nnoremap <leader>V V`]

" Split line (colleague of Join lines)
nnoremap S i<cr><esc><right>

" Source the current line
vnoremap <leader>S y:execute @@<cr>:echo 'Sourced selection.'<cr>
nnoremap <leader>S ^vg_y:execute @@<cr>:echo 'Sourced line.'<cr>

" Select (charwise) the contents of the current line, excluding indentation.
" Great for pasting Python lines into REPLs.
nnoremap vv ^vg_
 
" Mutt ... 
augroup ft_muttrc
    au!

    au BufRead,BufNewFile *.muttrc set ft=muttrc

    au FileType muttrc setlocal foldmethod=marker foldmarker=,}}}
augroup END
"

" vim-fugitive ... 
" prevent buffer build-up
autocmd BufReadPost fugitive://* set bufhidden=delete
"
" ALE statusline ...
"set statusline+=%{ALEGetStatusLine()}
"let g:ale_statusline_format = ['⨉ %d', '⚠ %d', '• ok']
"let g:ale_linters = {'python': ['flake8'],}
 
 
" ale setting..
"let g:ale_lint_on_text_changed = 'never'

" black config ...
"let g:black_virtualenv = "/home/lemon/.virtualenvs/black-for-vim/"
"nmap <leader>l :Black<CR>
" LET'S RUN VIRTUALENV BLACK ON SAVE!
"autocmd BufWritePre *.py execute ':Black'
"let g:black_virtualenv = $VIRTUAL_ENV


let g:ale_enabled = 1
let g:ale_sign_column_always = 1
let g:ale_open_list = 0
let g:ale_set_highlights = 1
let g:ale_set_signs = 1
let g:ale_set_loclist = 0
let g:ale_set_quickfix = 1
let g:ale_echo_cursor = 1
let g:ale_echo_msg_error_str = 'Error'
let g:ale_echo_msg_format = '%linter% - %code: %%s'
let g:ale_loclist_msg_format = '%linter% - %code: %%s'
let g:ale_echo_msg_warning_str = 'Warning'
"let g:ale_linters = {'python': ['flake8', 'mypy', 'pyls', 'pylint'],
"\                    'ocaml': ['merlin'],
" \}
let g:ale_linters = {'python': ['flake8', 'mypy'],
\                    'ocaml': ['merlin'],
\                    'cpp': ['clang'],
\                    'c': ['clang'],
\                    'go': ['gometalinter', 'gofmt'],
 \}
"let g:ale_linters_ignore = {'python': ['pyls']}
let g:ale_fixers = {'python': ['isort', 'black']}
let g:ale_python_mypy_ignore_invalid_syntax = 1
let g:ale_python_mypy_executable = 'mypy'
let g:ale_python_mypy_options = '--config-file mypy.ini'
" let g:ale_sign_error = '>>'
let g:ale_sign_warning = '--'
let g:ale_fix_on_save = 1
let g:ale_linters_explicit = 0


" Mappings in the style of unimpaired-next
nmap  [W <Plug>(ale_first)
nmap  [w <Plug>(ale_previous)
nmap  ]w <Plug>(ale_next)
nmap  ]W <Plug>(ale_last)

 
" Bufexplorer config ... 
let g:bufExplorerDefaultHelp=1       " Show default help.
let g:bufExplorerShowNoName=1        " Show "No Name" buffers.
let g:bufExplorerShowUnlisted=1      " Show unlisted buffers.
"
" Django ... 
augroup ft_django
    au!

    au BufNewFile,BufRead urls.py           setlocal nowrap
    au BufNewFile,BufRead urls.py           normal! zR
    au BufNewFile,BufRead dashboard.py      normal! zR
    au BufNewFile,BufRead local_settings.py normal! zR

    au BufNewFile,BufRead admin.py     setlocal filetype=python.django
    au BufNewFile,BufRead urls.py      setlocal filetype=python.django
    au BufNewFile,BufRead models.py    setlocal filetype=python.django
    au BufNewFile,BufRead views.py     setlocal filetype=python.django
    au BufNewFile,BufRead settings.py  setlocal filetype=python.django
    au BufNewFile,BufRead settings.py  setlocal foldmethod=marker
    au BufNewFile,BufRead forms.py     setlocal filetype=python.django
    au BufNewFile,BufRead common_settings.py  setlocal filetype=python.django
    au BufNewFile,BufRead common_settings.py  setlocal foldmethod=marker
augroup END

" vim-markdown ...
let g:vim_markdown_folding_disabled=1
set foldenable
let g:vim_markdown_initial_foldlevel=1
let g:vim_markdown_no_default_key_mappings=1
let g:vim_markdown_math=1
let g:vim_markdown_frontmatter=1
"
" get rid of annoying Press ENTER prompts etc ...
"set shortmess=atI
"
" Remap movement in windows ...
map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-l> <c-w>l
map <c-h> <c-w>h
 
" General python properness ...
autocmd FileType python set sw=4
autocmd FileType python set ts=4
autocmd FileType python set sts=4
 
" Escape sudo heck ...
cmap w!! %!sudo tee > /dev/null %
 
 
" my todo.todo... {{
autocmd BufNewFile,BufRead *.todo set syntax=markdown
"autocmd BufWritePost *.todo !todo-push
nmap <F2> a<C-R>=strftime("%c")<CR><Esc>
"let @d=':r! date _kb-I
ikb ($a)0"+dd:!xlcipkbkbkbkbclip -o >> /home/lemon/todo/done.todo
'
let @d='0$a(k2a)0"+dd:!xlcipkbkbkbkbclip -o >> /home/lemon/todo/done.todo
'
" ...}}

"
" FZF ...
" This is the default extra key bindings
let g:fzf_action = {
  \ 'ctrl-t': 'tab split',
  \ 'ctrl-x': 'split',
  \ 'ctrl-v': 'vsplit' }

" Default fzf layout
" - down / up / left / right
let g:fzf_layout = { 'down': '~40%' }

" Customize fzf colors to match your color scheme
let g:fzf_colors =
\ { 'fg':      ['fg', 'Normal'],
  \ 'bg':      ['bg', 'Normal'],
  \ 'hl':      ['fg', 'Comment'],
  \ 'fg+':     ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
  \ 'bg+':     ['bg', 'CursorLine', 'CursorColumn'],
  \ 'hl+':     ['fg', 'Statement'],
  \ 'info':    ['fg', 'PreProc'],
  \ 'prompt':  ['fg', 'Conditional'],
  \ 'pointer': ['fg', 'Exception'],
  \ 'marker':  ['fg', 'Keyword'],
  \ 'spinner': ['fg', 'Label'],
  \ 'header':  ['fg', 'Comment'] }
"  More fzf settings
" (https://github.com/zenbro/dotfiles/blob/master/.nvimrc#L151-L187)
 let g:fzf_nvim_statusline = 0 " disable statusline overwriting

  nnoremap <C-p> :<C-u>FZF<CR>
  nnoremap <leader><C-p> :<C-u>FZF!<CR>
  nnoremap ; :Buffers<CR>
  nnoremap <leader>t :Files<CR>
  nnoremap <leader>o :Tags<CR>
  nnoremap <leader>h :History<CR>
  nnoremap <silent> <leader>0 :Files<CR>
  nnoremap <silent> <leader>A :Windows<CR>
  nnoremap <silent> <leader>; :BLines<CR>
  nnoremap <silent> <leader>l :Lines<CR>
" nnoremap <silent> <leader># :Lines<CR>
  nnoremap <silent> <leader>o :BTags<CR>
" command history is :History:
  nnoremap <silent> <leader>? :History:<CR>
  nnoremap <silent> <leader>/ :execute 'Ag ' . input('Ag/')<CR>
" nnoremap <silent> <leader>. :AgIn #
" nnoremap <silent> <leader>a :Buffers<CR>
" nnoremap <silent> <leader>O :Tags<CR>
  nnoremap <silent> <leader>P :call SearchWordWithAg()<CR>
  vnoremap <silent> <leader>P :call SearchVisualSelectionWithAg()<CR>
" nnoremap <silent> <leader>gl :Commits<CR>
" nnoremap <silent> <leader>ga :BCommits<CR>
  nnoremap <silent> <leader>ft :Filetypes<CR>
  nnoremap <silent> <leader>CC :Commands<CR>

  imap <C-x><C-f> <plug>(fzf-complete-file-ag)
  imap <C-x><C-l> <plug>(fzf-complete-line)

  function! SearchWordWithAg()
    execute 'Ag' expand('<cword>')
  endfunction

  function! SearchVisualSelectionWithAg() range
    let old_reg = getreg('"')
    let old_regtype = getregtype('"')
    let old_clipboard = &clipboard
    set clipboard&
    normal! ""gvy
    let selection = getreg('"')
    call setreg('"', old_reg, old_regtype)
    let &clipboard = old_clipboard
    execute 'Ag' selection
  endfunction

"  function! SearchWithAgInDirectory(...)
"    call fzf#vim#ag(join(a:000[1:], ' '), extend({'dir': a:1}, g:fzf#vim#default_layout))
"  endfunction
"  command! -nargs=+ -complete=dir AgIn call SearchWithAgInDirectory(<f-args>)

" Enable per-command history.
" CTRL-N and CTRL-P will be automatically bound to next-history and
" previous-history instead of down and up. If you don't like the change,
" explicitly bind the keys to down and up in your $FZF_DEFAULT_OPTS.
let g:fzf_history_dir = '~/.local/share/fzf-history'
"
" Mapping selecting mappings
nmap <leader><tab> <plug>(fzf-maps-n)
xmap <leader><tab> <plug>(fzf-maps-x)
omap <leader><tab> <plug>(fzf-maps-o)

" Insert mode completion
imap <c-x><c-k> <plug>(fzf-complete-word)
imap <c-x><c-f> <plug>(fzf-complete-path)
imap <c-x><c-j> <plug>(fzf-complete-file-ag)
imap <c-x><c-l> <plug>(fzf-complete-line)

" Advanced customization using autoload functions
inoremap <expr> <c-x><c-k> fzf#vim#complete#word({'left': '15%'}
" 
"
" Ocaml/merlin 
"let g:opamshare = substitute(system('opam config var share'),'\n$','','''')
"execute "set rtp+=" . g:opamshare . "/merlin/vim"
"set rtp^="/home/lemon/.opam/4.06.1/share/ocp-indent/vim"
"

" Search from git root via :Rag (Root Ag)
" Thanks! https://github.com/fortes/dotfiles/blob/7ba6ea651edde8fbc24fdf9df52ba06f7e956efc/stowed-files/nvim/.vimrc
" :Rag  - hidden preview enabled with "?" key
" :Rag! - fullscreen and preview window above

" Change to git root of current file (if in a repo)
function! FindGitRootCD()
  let root = systemlist('git -C ' . expand('%:p:h') . ' rev-parse --show-toplevel')[0]
  if v:shell_error
    return ''
  else
    return {'dir': root}
  endif
endfunction


function! GitRootCD()
  let result = FindGitRootCD()
  if type(result) == type({})
    execute 'lcd' fnameescape(result['dir'])
    echo 'Now in '.result['dir']
  else
    echo 'Not in git repo!'
  endif
endfunction

command! GitRootCD :call GitRootCD()

command! -bang -nargs=* Rag
  \ call GitRootCD() | call fzf#vim#ag(<q-args>,
  \                 <bang>0 ? fzf#vim#with_preview('up:60%', '?')
  \                         : fzf#vim#with_preview('right:50%:hidden', '?'),
  \                 <bang>0)

" Use fuzzy searching for K & Q, select items to go into quickfix
nnoremap L :Rag! <C-R><C-W><cr>
vnoremap L :<C-u>norm! gv"sy<cr>:silent! Rag! <C-R>s<cr>
nnoremap Q :Rag!<SPACE>
"

"highlight ColorColumn ctermbg=0
highlight NonText cterm=NONE
"
syntax enable