-
Notifications
You must be signed in to change notification settings - Fork 0
/
vimrc
1775 lines (1731 loc) · 55.8 KB
/
vimrc
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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" somini.vimrc
" Legacy Stuff {{{
set nocompatible "Vim, not vi
" Force UTF-8 for insane systems
set encoding=utf-8
scriptencoding utf-8
set fileencodings=ucs-bom,utf-8,default,latin1
set backspace=indent,eol,start "Backspace works as intended
set showcmd "Show the command line as you type
set noshowmode " Redundant with Airline
set modeline "Enable modeline
if has('autocmd')
filetype plugin indent on "Enable filetype detection
endif
set updatetime=500 "ms
" Timeouts and stuff, Esc is instantaneous but can also be used in mappings
set notimeout
set ttimeout
set ttimeoutlen=100 "ms
if v:version > 703 || v:version == 703 && has("patch541")
set formatoptions+=j " Delete comment character when joining commented lines
endif
" i_C-u shouldn't delete so much
inoremap <C-u> <C-g>u<C-u>
" Baffling default
nnoremap Y y$
" GUI Options {{{
" Needs to be set here, to influence GUI loading
" set guioptions-=M "Don't load $VIMRUNTIME/menu.vim
" set guioptions-=f "Don't fork from the shell
" }}}
autocmd VimEnter * call s:SetupLegacy()
function! s:SetupLegacy()
" Matchit
sunmap a%
endfunction
set tagcase=match " https://github.com/vim/vim/issues/712
" }}}
" Pathogen {{{
runtime 3rd_party/vim-pathogen/autoload/pathogen.vim
if !exists('*pathogen#infect')
let s:error_marker = expand('<sfile>:p:h').'/.novimrc'
if !filereadable(s:error_marker) " Warn only the first time
echohl WarningMsg
echo "Can't load pathogen, won't change any options!"
echo "Try running `git submodule update` or clone the repo recursively"
echohl None
" Create the marker file
silent execute 'tabnew '.s:error_marker
silent write!
silent tabclose
endif
augroup vimrc_pathogen | autocmd!
autocmd VimEnter * echohl ErrorMsg | exec 'echo "'.'Fix the vim folder and `rm '.s:error_marker.'`"' | echohl None
augroup END
colorscheme blue "Ugly-ass colors to hammer the point home
finish
endif
" Plugin Blacklist {{{
" The big one, commited into git and the host-specific one
let s:plugin_blacklist = expand(expand('<sfile>:p:h').'/plugin_blacklist')
" The host-specific one is defined as a function of the big one
let s:plugin_blacklist_host = s:plugin_blacklist . '_host'
let s:blacklist = []
for f in [s:plugin_blacklist, s:plugin_blacklist_host]
if filereadable(f)
let s:blacklist += readfile(f)
endif
endfor
unlet f
if !empty(s:blacklist)
let g:pathogen_blacklist = s:blacklist
endif
"}}}
execute pathogen#infect('1st_party/{}','3rd_party/{}')
"}}}
" Utilities {{{
let g:user_dir=expand("<sfile>:p:h")
"SetToggle: Toggle complex options {{{
function! SetToggle(option, value)
" Check if the option contains value
if stridx(getwinvar(winnr(), '&'.a:option), a:value) == -1
let l:operator='+='
else "Exists, remove it
let l:operator='-='
endif
execute 'set '.a:option.l:operator.a:value
endfunction
" }}}
"IsEmptyBuffer: Check if the current buffer is empty {{{
function! IsEmptyBuffer()
if line('$') == 1 && getline(1) ==# '' && !&modified
return 1
else
return 0
endif
endfunction
"}}}
" Command line configuration
" Complete the longest common prefix, then use <Tab> to cycle
" through the various matches.
set wildmenu
set wildmode=longest:full,list:full
set wildignorecase "Ignore case
" Insert completion configuration
" Keep the same-ish configuration from the command line
" The SuperTab plugin helps with the rest
set complete=.,w,b,u,U,t,i
set completeopt=menu,preview,longest
set isfname-== " Remove = from filenames. Breaks files with = on the name
set lazyredraw "Don't update the screen in the middle of macros, etc
set autoread "Re-read files if they haven't been changed in vim
" More information with less clicks
nnoremap <C-g> 2<C-g>
" Keep the cursor still when joining lines
nnoremap J m`J``
" Edit a register on the command line
function! s:ChangeRegister()
let l:reg_regex = '[a-z]'
echohl Question
echo 'Choose register('.l:reg_regex.'): '
echohl None
let l:c = nr2char(getchar())
if l:c =~? l:reg_regex
let l:reg = tolower(l:c)
return ":let @".l:reg." = \<C-r>=string(getreg(".string(l:reg)."))\<CR>\<Left>"
else
echohl WarningMsg
echo 'Register invalid: "'.strtrans(l:c).'"'
echohl None
endif
endfunction
nnoremap <expr> <Leader>r <SID>ChangeRegister()
" Setup the leader keys
let g:mapleader = '\'
let g:maplocalleader = ','
" Nice unicode modified character
let s:modified_glyph = '☢'
" Move lines {{{
nnoremap <silent> + :move +1<CR>
nnoremap <silent> - :move -2<CR>
vnoremap <silent> + :call <SID>MoveCurrentBlock(1)<CR>gv
vnoremap <silent> - :call <SID>MoveCurrentBlock(-1)<CR>gv
function! s:MoveCurrentBlock(offset) range
" Only on V-Line visual mode
if !a:offset || a:offset == 0 || visualmode() !=# 'V'
return
endif
let l:range = a:lastline - a:firstline + 1
if a:offset > 0
let l:move = a:lastline + a:offset
else
let l:move = a:firstline + a:offset - 1
endif
if l:move > line('$')
return
endif
execute a:firstline.','.a:lastline.'move '.l:move
endfunction
"}}}
" Keep buffer locations {{{
if v:version >= 700
" Save current view settings on a per-window, per-buffer basis. {{{
function! s:AutoSaveWinView()
if !exists("w:vimrc_bufview")
let w:vimrc_bufview = {}
endif
let w:vimrc_bufview[bufnr("%")] = winsaveview()
endfunction
"}}}
" Restore current view settings. {{{
function! s:AutoRestoreWinView()
let l:buf = bufnr("%")
if exists("w:vimrc_bufview") && has_key(w:vimrc_bufview, l:buf)
let l:v = winsaveview()
let atStartOfFile = l:v.lnum == 1 && l:v.col == 0
if atStartOfFile && !&diff
call winrestview(w:vimrc_bufview[l:buf])
endif
unlet w:vimrc_bufview[l:buf]
endif
endfunction
"}}}
augroup vimrc_winview | autocmd!
" When switching buffers, preserve window view.
autocmd BufLeave * call <SID>AutoSaveWinView()
autocmd BufEnter * call <SID>AutoRestoreWinView()
augroup END
endif
"}}}
" Non-file Buffer {{{
let g:nonfile_filetypes_standalone = [
\ 'help',
\ 'man',
\ ]
let g:nonfile_filetypes_standalone_regex = '\V'.join(g:nonfile_filetypes_standalone,'\|')
let g:nonfile_filetypes_modal = [
\ 'nerdtree',
\ 'startify',
\ ]
let g:nonfile_filetypes_modal_regex = '\V'.join(g:nonfile_filetypes_modal,'\|')
let g:nonfile_filetypes = g:nonfile_filetypes_standalone + g:nonfile_filetypes_modal
let g:nonfile_filetypes_regex = '\V'.join(g:nonfile_filetypes,'\|')
"}}}
" Root Markers {{{
" Account for git submodules
let g:root_markers_vcs = [
\ '.git',
\ '.git/',
\ '.hg/',
\ '.svn/',
\ '.bzr/',
\ '_darcs/',
\]
let g:root_markers_other = [
\ '.RootMarker',
\ 'Makefile',
\ 'Rakefile',
\ 'setup.py', 'requirements.txt',
\]
let g:root_markers = g:root_markers_vcs + g:root_markers_other
"}}}
" Change Indentation {{{
nnoremap <silent> <Leader>ci :call <SID>stab()<CR>
function! s:stab()
let l:in = input('Indentation Value: ')
if !empty(l:in) && l:in =~# '\v[\+\-]?\d*'
" Do it!
call s:stab_change(l:in, 0)
else
echohl WarningMsg
echo 'Pattern not a match'
call s:stab_check()
echohl None
endif
endfunction
function! s:stab_change(val, check)
let l:tabs = a:val =~# '^-' "NoExpandTab, unless you say so
let l:value = abs(a:val)
let &l:expandtab = l:tabs
let &l:tabstop = l:value
let &l:shiftwidth = l:value
" Refresh the indent guides
IndentGuidesToggle
IndentGuidesToggle
if a:check
call s:stab_check()
endif
endfunction
function! s:stab_check()
setlocal expandtab? tabstop? shiftwidth?
endfunction
function! s:stab_command(...)
if a:0 == 1
call s:stab_change(a:000[0], 1)
else
call s:stab_check()
endif
endfunction
command! -nargs=? Stab call s:stab_command(<args>)
"}}}
" }}}
" Clipboard {{{
set clipboard=unnamedplus " Sync the unnamed register with the system clipboard
augroup vimrc_clipboard | autocmd!
autocmd VimEnter * call s:SetupClipboard()
augroup END
" Setup the clipboard using EasyClip helpers {{{
function! s:SetupClipboard()
" Insert mode {{{
" C-q does what C-v did, insert raw characters
inoremap <C-q> <C-v>
" C-v pastes
if exists('g:loaded_EasyClip') && g:loaded_EasyClip == 1
" Make sure EasyClip is loaded, otherwise use a fallback
imap <C-v> <Plug>EasyClipInsertModePaste
else
execute 'imap <script> <C-v> <C-g>u' . paste#paste_cmd['i']
endif
"}}}
" Command mode {{{
" C-y pastes
if exists('g:loaded_EasyClip') && g:loaded_EasyClip
" Make sure EasyClip is loaded, otherwise use a fallback
cmap <C-y> <Plug>EasyClipCommandModePaste
else
cnoremap <C-y> <C-r>+
endif
"}}}
endfunction
"}}}
" yY yanks the line minus the <CR> at the end
" just like dD from EasyClip
nnoremap yY m`0y$``
" Duplicate lines, upwards and downwards
" TODO: A count should create X copies
nnoremap <silent> yp :copy +0<CR>
nnoremap <silent> yP :copy -1<CR>
"}}}
" Command Line {{{
" EMACS/Readline compatible (mostly)
" <C-b>: Move <1 character {{{
cnoremap <C-b> <Left>
cnoremap <C-x><C-b> <C-b>
"}}}
" <C-f>: Move >1 character {{{
cnoremap <C-f> <Right>
cnoremap <C-x><C-f> <C-f>
"}}}
" <A-f>: Move >1 word {{{
cnoremap <A-f> <C-Right>
"}}}
" <A-b>: Move <1 word {{{
cnoremap <A-b> <C-Left>
"}}}
" <C-a>: Start of Line {{{
cnoremap <C-a> <Home>
cnoremap <C-x><C-a> <C-a>
"}}}
"<C-e>: End of line
" <C-d>: Delete >1 character {{{
cnoremap <C-d> <C-r>=CommandLine_Helper_DeleteForward()<CR><Del>
cnoremap <C-x><C-d> <C-d>
"}}}
" <C-k>: Delete line >cursor {{{
cnoremap <C-k> <C-\>eCommandLine_KillForward()<CR>
cnoremap <C-x><C-k> <C-k>
"}}}
"<C-u>: Delete line <cursor
" Helper Functions {{{
function! s:CommandLine_Is_End()
return getcmdpos() - 1 == strlen(getcmdline())
endfunction
function! CommandLine_Helper_DeleteForward()
" If the cursor is at the end of the command line,
" insert a character to be deleted by the <Del>
return s:CommandLine_Is_End() ? 'x' : ''
endfunction
function! CommandLine_KillForward()
let l:line = getcmdline()
let l:cur = getcmdpos()
let l:s_from = 0
let l:s_to = l:cur - 1
return strpart(l:line, l:s_from, l:s_to)
endfunction
"}}}
"}}}
" Data Safety and Managing {{{
nnoremap <silent> <Leader>w :call <SID>WriteCurrentBuffer()<CR>
function! s:WriteCurrentBuffer()
let l:dir = expand('%:h:p')
if !isdirectory(l:dir)
echom 'Making directory: '.l:dir
call mkdir(l:dir, 'p')
endif
if &readonly
SudoWrite
else
if exists('b:resolved_symlink')
write!
unlet! b:resolved_symlink
else
write
endif
endif
endfunction
set history=1000 " Disk space is REALLY cheap
function! CheckDir(dir) "{{{
if !isdirectory(a:dir)
call mkdir(a:dir,"p")
endif
endfunction "}}}
set fileformats=unix,dos,mac "ALL the formats, by this order
" Vim Info {{{
set viminfo='150 "Save the marks for this much files !must exist!
set viminfo+=f0 "Don't save '0 to '9
set viminfo+=! "Save upper-case variables
set viminfo+=h "Don't activate search highlight on start
set viminfo+=s50 "Don't save registers larger than this Kb
set viminfo+=<100 "nor larger than these lines
" set viminfo+=r$SKIP_PATH_PREFIX
" set viminfo+=n$VIMINFO_FILENAME !must be the last one!
augroup vimrc_viminfo | autocmd!
autocmd VimEnter * call s:SetupViminfo()
augroup END
function! s:SetupViminfo() "{{{
" Only if there was a setup
if !exists('g:viminfo_old') || !exists('g:viminfo_new')
return
endif
if filereadable(g:viminfo_old) "Make sure it's there
" Read the info in the old viminfo file
execute 'rviminfo!' g:viminfo_old
call delete(g:viminfo_old)
endif
if filereadable(g:viminfo_new) "Make sure it's there
" This runs after the viminfo is read, so just read it again
execute 'rviminfo!' g:viminfo_new
endif
wviminfo "Merge both files
rviminfo!
" Return to status quo
unlet g:viminfo_old g:viminfo_new
endfunction
"}}}
" }}}
" Undo {{{
set undofile
" set undodir=OS_specific
set undolevels=1500 " Undo forever
" }}}
" Views {{{
" set viewdir=OS_specific
set viewoptions+=slash " The One True Path Separator
set viewoptions+=unix " The One True Format
"}}}
" Swap Files {{{
" set directory=OS_specific
" }}}
" Backup Files {{{
set backup writebackup "Backup and delete old file
set backupcopy=yes " Keep the inodes on Linux
" set backupdir=OS_specific
" Don't backup this file patterns
let &backupskip=expand($VIMRUNTIME).'/*'
set backupext=.vim.bak
" }}}
" Sessions {{{
set sessionoptions-=help "Don't store the help windows
set sessionoptions-=options "Don't store options in sessions
set sessionoptions+=slash "The One True Path Separator
set sessionoptions+=unix "The One True Format
" }}}
" Follow symlinks {{{
" NOTE: this happens with directory symlinks anyway (due to Vim's chdir/getcwd
" magic when getting filenames).
" Sources:
" - https://github.com/blueyed/dotfiles
" - https://github.com/tpope/vim-fugitive/issues/147#issuecomment-7572351
" - http://www.reddit.com/r/vim/comments/yhsn6/is_it_possible_to_work_around_the_symlink_bug/c5w91qw
function! s:vimrc_follow_symlinks(...)
unlet! b:resolved_symlink
if exists('w:no_resolve_symlink') && w:no_resolve_symlink
return
endif
if &ft == 'help'
return
endif
let fname = a:0 ? a:1 : expand('%')
if fname =~ '^\w\+:/'
" Do not mess with 'fugitive://' etc.
return
endif
let fname = simplify(fname)
let resolvedfile = resolve(fname)
if resolvedfile == fname
return
endif
let resolvedfile = fnameescape(resolvedfile)
let sshm = &shm
set shortmess+=A " silence ATTENTION message about swap file (would get displayed twice)
redraw " Redraw now, to avoid hit-enter prompt.
exec 'file ' . resolvedfile
let &shm=sshm
unlet! b:git_dir
call fugitive#detect(resolvedfile)
if &modifiable
" Only display a note when editing a file, especially not for `:help`.
redraw " Redraw now, to avoid hit-enter prompt.
echomsg 'Resolved symlink: =>' resolvedfile
let b:resolved_symlink = 1
endif
endfunction
command! -bar FollowSymlink call s:vimrc_follow_symlinks()
command! ToggleFollowSymlink let w:no_resolve_symlink = !get(w:, 'no_resolve_symlink', 0) | echo "w:no_resolve_symlink =>" w:no_resolve_symlink
augroup vimrc_symlinks | autocmd!
autocmd BufReadPost * nested call s:vimrc_follow_symlinks()
augroup end
" }}}
" }}}
" Appearance {{{
set background=dark
if &t_Co >= 256 || has('gui_running') " Something nice
colorscheme harlequin
else "Sensible fallback
colorscheme evening
endif
set background=dark "Again, for some reason
if has("syntax")
syntax enable
endif
set laststatus=2 "Always show the statusline
set hlsearch "Highlight searches ...
nohlsearch "but not when starting up
set sidescroll=1 "Scroll 1 line at a time, horizontally
set scrolloff=5
set sidescrolloff=10
" Toggle line wrapping
nnoremap <silent> <Leader>tw :setlocal wrap!<CR>
"{{{ Non-Printable Characters
let &showbreak = ' ↪'
set list "Show non-printable characters
let &listchars = 'tab: '
"Alternate tab: ≈~
set listchars+=trail:•
set listchars+=precedes:⇇,extends:⇉
set listchars+=nbsp:·
" set listchars+=eol:¶
"}}}
" Numbers {{{
set numberwidth=1 " Minimal width of the number column
set nonumber norelativenumber " No line numbers by default ...
"... but toggle them with:
nnoremap <silent> <Leader>tn :call <SID>ToggleNumber()<CR>
nnoremap <silent> <Leader>tN :call <SID>ToggleRelativeNumber()<CR>
" Functions {{{
" Let the toggle number mapping toggle all numbers, and persist the relative
" number option with a script variable
function! s:ToggleNumber()
if &number
let s:vimrc_relativenumber = &relativenumber
setlocal nonumber norelativenumber
else
setlocal number
let &l:relativenumber = (exists('s:vimrc_relativenumber')) ? s:vimrc_relativenumber : 0 "OFF
endif
endfunction
function! s:ToggleRelativeNumber()
if &number
setlocal relativenumber!
else
let s:vimrc_relativenumber = exists('s:vimrc_relativenumber') ? !s:vimrc_relativenumber : 1 "ON
endif
endfunction
"}}}
"}}}
" Folds {{{
nnoremap <silent> <Leader>tf :call <SID>ToggleFolds()<CR>
function! s:ToggleFolds()
let l:lvl = foldlevel('.')
if l:lvl + 1 > &l:foldcolumn
" Extent the column to the new distance
let &l:foldcolumn = l:lvl + 1
else
if &l:foldcolumn > 0
set foldcolumn=0
else
let &l:foldcolumn = foldlevel('.') + 1
endif
endif
endfunction
"}}}
" Cursor Line and Column {{{
set nocursorline nocursorcolumn
nnoremap <silent> <Leader>tc :setlocal cursorline!<CR>
nnoremap <silent> <Leader>tC :setlocal cursorcolumn!<CR>
"}}}
"}}}
" Windowing Systems {{{
set noequalalways " Don't resize the windows on resize
function! s:vimrc_fullscreen()
echohl WarningMsg
echo 'Fullscreen: Needs OS specific mappings!'
echohl None
endfunction
" This should be remapped on OS specific places
nnoremap <Plug>ToggleFullscreen :call <SID>vimrc_fullscreen()<CR>
nmap <silent> <F11> <Plug>ToggleFullscreen
" Close the bottom windows on <C-w><C-q>. This aliases an alias for `ZQ`
nnoremap <silent> <Plug>(vimrc-close-bwin) :lclose<CR>:cclose<CR>:pclose<CR>:UndotreeHide<CR>
nmap <silent> <C-w><C-q> <Plug>(vimrc-close-bwin)
"}}}
" Navigation {{{
nnoremap <Space> <C-d>
nnoremap <S-Space> <C-u>
nmap g<Space> <S-Space>
nnoremap <silent> <C-Left> :bprevious<CR>
nnoremap <silent> <C-Right> :bnext<CR>
" Tabs {{{
nmap <A-h> <Plug>AirlineSelectPrevTab
nmap <A-l> <Plug>AirlineSelectNextTab
nmap <silent> <A-S-h> :tabmove -1<CR>
nmap <silent> <A-S-l> :tabmove +1<CR>
nmap <A-1> <Plug>AirlineSelectTab1
nmap <A-2> <Plug>AirlineSelectTab2
nmap <A-3> <Plug>AirlineSelectTab3
nmap <A-4> <Plug>AirlineSelectTab4
nmap <A-5> <Plug>AirlineSelectTab5
nmap <A-6> <Plug>AirlineSelectTab6
nmap <A-7> <Plug>AirlineSelectTab7
nmap <A-8> <Plug>AirlineSelectTab8
nmap <A-9> <Plug>AirlineSelectTab9
nnoremap <A-q> :tabclose<CR>
"}}}
set hidden "Don't prompt when changing buffers
" Alt-jk to move around {{{
if has('gui_running')
nnoremap <A-j> <C-e>
nnoremap <A-k> <C-y>
else
nnoremap <Esc>j <C-e>
nnoremap <Esc>k <C-y>
endif
"}}}
"On visual-block let the cursor go to "illegal" places (Half-tabs, past the end, etc)
set virtualedit=block
" Jumps {{{
" <C-o>: Go back to
" The opposite of <C-o>, aliased by SuperTab
nnoremap g. <C-i>
nnoremap <silent> g: :call <SID>goto_jump()<CR>
function! s:goto_jump()
" http://vim.wikia.com/wiki/Jumping_to_previously_visited_locations
jumps
let j = input('Jump to: ')
if j != ''
let pattern = '\v\c^\+'
let l:cmd = 'normal! '
if j =~ pattern
let j = substitute(j, pattern, '', 'g')
let l:cmd .= j . "\<C-i>"
else
let l:cmd .= j . "\<C-o>"
endif
let l:cmd .= 'zzzv'
execute l:cmd
endif
endfunction
"}}}
" "Smart" Apostrophe {{{
nnoremap ' `
nnoremap ` '
"}}}
" "Smart" Zero {{{
nnoremap 0 ^
nnoremap ^ 0
"}}}
" Smart Underscore {{{
function! SmartUnderscore()
let l:line = line('.')
let l:col = col('.')
execute 'normal! ^'
let l:soft = col('.')
execute 'normal! 0'
let l:hard = col('.')
if l:col != l:soft
call cursor(l:line, l:soft)
else
call cursor(l:line, l:hard)
endif
endfunction
nnoremap <silent> _ :<C-u>call SmartUnderscore()<CR>
"}}}
" Quickfix and Location List {{{
autocmd FileType qf call s:vimrc_listings()
function! s:vimrc_listings()
redir => buf
silent file
redir END
if match(buf, '\cQuickfix') > -1
" Quickfix
nnoremap <silent> <buffer> q :cclose<CR>
nnoremap <silent> <buffer> <CR> <CR>
nnoremap <silent> <buffer> l <CR>:cclose<CR>
nnoremap <silent> <buffer> L <CR>:wincmd w<CR>
nnoremap <silent> <buffer> gg :crewind<CR>:wincmd w<CR>
nnoremap <silent> <buffer> j :cnext<CR>
nnoremap <silent> <buffer> J :cnext<CR>:wincmd w<CR>
nnoremap <silent> <buffer> <A-j> j
nnoremap <silent> <buffer> k :cprevious<CR>
nnoremap <silent> <buffer> K :cprevious<CR>:wincmd w<CR>
nnoremap <silent> <buffer> <A-k> k
nnoremap <silent> <buffer> G :clast<CR>:wincmd w<CR>
else
" Location List
nnoremap <silent> <buffer> q :lclose<CR>
nnoremap <silent> <buffer> <CR> <CR>
nnoremap <silent> <buffer> l <CR>:lclose<CR>
nnoremap <silent> <buffer> L <CR>:wincmd w<CR>
nnoremap <silent> <buffer> gg :lrewind<CR>:wincmd w<CR>
nnoremap <silent> <buffer> j :lnext<CR>
nnoremap <silent> <buffer> J :lnext<CR>:wincmd w<CR>
nnoremap <silent> <buffer> <A-j> j
nnoremap <silent> <buffer> k :lprevious<CR>
nnoremap <silent> <buffer> K :lprevious<CR>:wincmd w<CR>
nnoremap <silent> <buffer> <A-k> k
nnoremap <silent> <buffer> G :llast<CR>:wincmd w<CR>
endif
endfunction
" Quickfix Manipulation
nnoremap <silent> [q :cprevious<CR>zzzv
nnoremap <silent> ]q :cnext<CR>zzzv
" Location List Manipulation
nnoremap <silent> [l :lprevious<CR>zzzv
nnoremap <silent> ]l :lnext<CR>zzzv
"}}}
"}}}
" Diff & Splits {{{
" Diffs {{{
set diffopt+=iwhite "Ignore whitespace on diffs
" Jump to the next hunk, but choose how
nnoremap <silent> ]c :call <SID>HunkMoveTo(1)<CR>
nnoremap <silent> [c :call <SID>HunkMoveTo(0)<CR>
function! s:HunkMoveTo(next)
let l:cmd = ''
if &diff
execute 'normal! '.(a:next ? ']c' : '[c')
return
endif
if exists('b:sy') && len(b:sy.hunks) > 0
" Use Signify
let l:cmd = "\<Plug>".'(signify-'. (a:next ? 'next' : 'prev') .'-hunk)'
elseif exists('b:gitgutter_tracked')
" Use GitGutter
let l:cmd = "\<Plug>".'GitGutter'. (a:next ? 'Next' : 'Prev') .'Hunk'
endif
" Center and open folds
let l:cmd .= 'zzzv'
execute 'normal ' . l:cmd
endfunction
nnoremap <silent> du :<C-r>diffupdate<CR>
" After changing something in a diff, move to the next diff
nmap do do]c
nmap dp dp]c
" Diff the current file
" TODO: Do something similar for signify
nnoremap <silent> <Leader>d :Gdiff<CR>
" One-shot diff of the current file
" This is plugin-specific
nnoremap <silent> <Leader>D :call DiffOneShot()<CR>
function! DiffOneShot()
" Git Gutter
if exists('g:gitgutter_enabled') && g:gitgutter_enabled == 0
call s:DiffOneShot_GitGutter()
elseif exists('b:sy') && b:sy.active == 0
call s:DiffOneShot_Signify()
endif
endfunction
"}}}
" Splits {{{
set splitright " New splits to the right
set nosplitbelow " New splits on top
"}}}
"}}}
" Search {{{
set ignorecase smartcase "Sane defaults
set incsearch "Start searching right away
" Use <C-l> to clear the highlighting of :set hlsearch.
" <C-l> Already clears the screen, it's just a bonus
" Refresh the statusline too
nnoremap <silent> <C-l> :nohlsearch<CR>:AirlineRefresh<CR><C-l>
" Center on search
" Make sure it works on folds
nnoremap * *zvzz
nnoremap n nzvzz
nnoremap N Nzvzz
" Don't mess with search directions, n is ALWAYS forward
augroup vimrc_setup_search | autocmd!
autocmd VimEnter * call s:vimrc_setup_search()
augroup end
function! s:vimrc_setup_search()
nmap # *NN
nmap g# g*NN
if exists('g:loaded_visualstar') && g:loaded_visualstar == 1
xmap # *NN
xmap g# g*NN
endif
endfunction
" 'g/' to start a full-file search and replace for the current pattern
" Also works on visual mode
nnoremap g/ :<C-u>%s@<C-r>/@@<Left>
xnoremap g/ :s@<C-r>/@@<Left>
" '&' to repeat last ':s', use flags too
nnoremap & :&&<CR>
xnoremap & :&&<CR>
"}}}
" Mouse {{{
set mouse=a "Enable the mouse in all modes
set selectmode= "But no select mode, ever
set mousemodel=extend "Just like xterm, everywhere
set nomousefocus "Don't move the mouse to switch windows
" But toggle this setting, sometimes it's cool
nnoremap <silent> <Leader>tm :set mousefocus!<CR>
set mousehide "Hide the pointer when writing
"}}}
" Help {{{
" <F1> for choosing help
nnoremap <F1> :help<Space>
" g<F1> to close the help, regardless of current buffer
if exists(':helpclose') "Newer vim
nnoremap <silent> <C-F1> :helpclose<CR>
else
nmap <silent> <C-F1> <F1><CR><F1>
endif
nmap g<F1> <C-F1>
" <F1> on Insert mode does the same
imap <F1> <C-o><F1>
imap <C-F1> <C-o><C-F1>
"}}}
" Spell {{{
let g:spelling_filetypes = [
\ 'text',
\ 'mkd', 'markdown',
\ 'rst',
\ 'asciidoc',
\ 'gitcommit',
\ 'help',
\ 'mail'
\]
let g:spelling_filetypes_comma = join(g:spelling_filetypes,',')
let g:spelling_filetypes_regex = '\V'.join(g:spelling_filetypes,'\|')
" Toggle spell checking
nnoremap <silent> <Leader>tS :call SpellLoop_ToggleSpell()<CR>
set nospell "Disable it by default
set spelllang=en "Just a sensible default
set spellfile= "Auto-discover
" set spellcapcheck="[.?!]\_[\])'" \t]\+" "Check capitals on start of sentences
set spellsuggest=fast "Works reasonably enough, but it's really fast, especially for English
set spellsuggest+=25 "Show at most this suggestions
let g:loaded_spellfile_plugin = 1 "Don't ask for downloading spellfiles
function! s:vimrc_text()
" Initialize the spelling plugins
call lexical#init({"spell": 1})
call SpellLoop_Init()
" FIXME: Integrate SpellCheck into syntastic
nnoremap <silent> <buffer> Q :SpellLCheck!<CR>
" `==` formats the entire buffer properly, instead of indenting
" Keep the current location and leverage the "textobj-entire" plugin
nmap <silent> <buffer> == m`gqa<CR>'`
runtime scripts/text_wrapping.vim
runtime scripts/text_mappings.vim
endfunction
augroup vimrc_spelling | autocmd!
" Mark this files as "text"
execute 'autocmd FileType' g:spelling_filetypes_comma 'call s:vimrc_text()'
" Disable indent guides
let g:indent_guides_exclude_filetypes = g:spelling_filetypes
augroup END
"}}}
" Whitespace Management {{{
set autoindent smartindent "Indent in smart, language-specific ways
set shiftround "Align to the nearest "shiftwidth"
" Enter {{{
" On normal mode, Enter put a new line after the current one
nnoremap <silent> <CR> :put =''<CR>
" And S-Enter puts it before
nnoremap <silent> <S-CR> :put! =''<CR>
" Synonym for terminals
nmap g<CR> <S-CR>
"}}}
" Tab {{{
set smarttab "Use 'shiftwidth' with tabs
" Tab to indent text by shiftwidth columns
" C-Tab to indent text by 1 column, using spaces
function! IndentLine(cols)
if a:cols > 0
let l:pat = '^'
let l:subs = repeat(' ',a:cols)
elseif a:cols < 0
" The last x spaces, where x < -a:cols
" If that doesn't match, just ^
" TODO: Remove tabs?
let l:pat = '\v^\s{-}\zs {1,'.-a:cols.'}\ze\S|^'
let l:subs = ''
else
return "Do nothing
endif
if exists(':keeppatterns')
let l:keeppatterns = 'keeppatterns '
else
let l:keeppatterns = ''
let l:old_search = @/
endif
execute l:keeppatterns 'substitute' '/'.l:pat.'/'.l:subs.'/'
if empty(l:keeppatterns)
let @/ = l:old_search
endif
endfunction
command! -nargs=? -range IndentLine <line1>,<line2>call IndentLine(<f-args>)
nnoremap <Tab> >>
nnoremap <S-Tab> <<
nnoremap <silent> <C-Tab> :IndentLine 1<CR>
nnoremap <silent> <C-S-Tab> :IndentLine -1<CR>
nmap g<Tab> <C-Tab>
nmap g<S-Tab> <C-S-Tab>
" On visual mode, keep the indented text select
vnoremap <Tab> >gv
vnoremap <S-Tab> <gv
vnoremap <C-Tab> :IndentLine 1<CR>gv
vnoremap <C-S-Tab> :IndentLine -1<CR>gv
vmap g<Tab> <C-Tab>
vmap g<S-Tab> <C-S-Tab>
"
"}}}
" Split the line at this point (replaces current char)
" gS reshuffles the resulting lines
nnoremap <silent> gs r<CR>
nmap <silent> gS gs-j
" Tabularize on Steroids {{{
function! TabularizeThisN(ask)
if exists('g:tabular_loaded')
let l:cmd = ':Tabularize'
if a:ask != 1 && exists('*TabularizeHasPattern') && TabularizeHasPattern()
" Reuse the last Tabularize command
let l:cmd .= "\<CR>"
else
let l:cmd .= "\<Space>"
endif
return l:cmd
endif
endfunction
" Double leader to always ask for a new pattern
nnoremap <expr> <Leader><Tab> TabularizeThisN(0)
nnoremap <expr> <Leader><Leader><Tab> TabularizeThisN(1)
"}}}
"}}}
" 2Leader Commands{{{
" cd: Change dir to the current file
nnoremap <silent> <Leader><Leader>cd :lcd %:h<CR>:pwd<CR>
" CD: Change dir to current file. Works globally
nnoremap <silent> <Leader><Leader>CD :cd %:h<CR>:pwd<CR>
" b : Show a list of buffers and use CtrlP functionalities
nnoremap <silent> <Leader><Leader>b :CtrlPBuffer<CR>
" m : Show a list of marks, and prompt for one
nnoremap <silent> <Leader><Leader>m :marks<CR>:mark<Space>
" Vim: Yo dawg, etc {{{
function! s:IsVimRcFile()
let l:cfull = fnamemodify(expand('%'),':p')
let l:cdir = fnamemodify(expand('%'),':p:h')
let l:cname = fnamemodify(expand('%'),':t')
if index([expand($MYVIMRC),expand($MYGVIMRC)], l:cfull) != -1
return 1
elseif l:cdir ==# g:user_dir && l:cname =~# '\v\Cg?vimrc(\.\w+)?'
return 1
else
return 0
endif
endfunction
function! s:vimrc_edit(vimrc)
if expand('%:p') ==# expand(a:vimrc)
return
endif
execute IsEmptyBuffer() ? 'edit' : 'tabnew' a:vimrc
tabmove 0 "Move the tab to the leftmost position
endfunction
" ve: Edit your vimrc
" vE: Edit your gvimrc
nnoremap <silent> <Leader><Leader>ve :call <SID>vimrc_edit($MYVIMRC)<CR>
nnoremap <silent> <Leader><Leader>vE :call <SID>vimrc_edit($MYGVIMRC)<CR>
function! s:vimrc_write()
if s:IsVimRcFile()
silent write
endif
endfunction
" vs: Source your vimrc right here
nnoremap <silent> <Leader><Leader>vs :call <SID>vimrc_write()<CR>:source $MYVIMRC<CR>:AirlineRefresh<CR>
"}}}
"}}}
" Plugin Configuration {{{