forked from drmikehenry/vimfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvimrc
More file actions
4493 lines (3777 loc) · 146 KB
/
Copy pathvimrc
File metadata and controls
4493 lines (3777 loc) · 146 KB
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
" vim:tw=80:ts=4:sts=4:sw=4:et:ai
" =============================================================
" Early Setup
" =============================================================
" Enable vi-incompatible Vim extensions (redundant since .vimrc exists).
set nocompatible
" Use utf-8 encoding for all content.
set encoding=utf-8
" 'fileencodings' contains a list of possible encodings to try when reading
" a file. When 'encoding' is a unicode value (such as utf-8), the
" value of fileencodings defaults to ucs-bom,utf-8,default,latin1.
" ucs-bom Treat as unicode-encoded file if and only if BOM is present
" utf-8 Use utf-8 encoding
" default Value from environment LANG
" latin1 8-bit encoding typical of DOS
" Setting this value explicitly, though to the default value.
set fileencodings=ucs-bom,utf-8,default,latin1
" Leaving 'fileencoding' unset, as it defaults to the value of 'encoding'.
" May set 'fileencoding' before writing a file to force a new encoding.
" May also set 'bomb' to force use of a BOM (Byte Order Mark).
" set fileencoding=
" Set environment variable to directory containing this vimrc. Expect absolute
" directory $HOME/.vim on Unix or %USERPROFILE%\vimfiles on Windows.
let $VIMFILES = expand("<sfile>:p:h")
" -------------------------------------------------------------
" List manipulation
" -------------------------------------------------------------
function! Flatten(nestedLists)
let flattened = []
let i = 0
while i < len(a:nestedLists)
if type(a:nestedLists[i]) == type([])
let flattened += Flatten(a:nestedLists[i])
else
let flattened += [a:nestedLists[i]]
endif
let i += 1
endwhile
return flattened
endfunction
" -------------------------------------------------------------
" runtimepath manipulation
" -------------------------------------------------------------
" Escape commas and backslashes in path.
function! PathEscape(path)
return substitute(a:path, '[\\,]', '\\&', "g")
endfunction
" Un-escape commas and backslashes in path.
function! PathUnescape(path)
" Very magic.
let rex = '\v'
" Find escaping backslash, capture following backslash or a comma.
let rex .= '\\([\\,])'
return substitute(a:path, rex, '\1', "g")
endfunction
" Join paths into comma-separated path string.
" Backslashes and commas will be escaped in final string.
" Arguments are individual strings or lists of strings.
function! PathJoin(...)
let paths = Flatten(a:000)
let paths = map(paths, 'PathEscape(v:val)')
let path = join(paths, ",")
return path
endfunction
" Split comma-separated path string into a list.
" Honors escaped backslashes and commas.
function! PathSplit(path)
" Split regex: Very magic.
let rex = '\v'
" Begin match after a non-backslash.
let rex .= '\\@<!'
" Match an zero or more pairs of backslashes; each pair
" represents a single escaped backslash.
let rex .= '%(\\\\)*'
" Now set start-of-match and require a comma (which is necessarily not
" escaped).
let rex .= '\zs,'
let paths = split(a:path, rex)
" Unescape individual paths.
let paths = map(paths, 'PathUnescape(v:val)')
return paths
endfunction
function! RtpPrepend(path)
if isdirectory(a:path)
let &runtimepath = PathEscape(a:path) . "," . &runtimepath
let after = a:path . "/after"
if isdirectory(after)
let &runtimepath = &runtimepath . "," . PathEscape(after)
endif
endif
endfunction
" Append path later in &runtimepath than refPath (similarly for path/after).
" - Non-existing paths are not added to &runtimepath.
" - If refPath does not exist, path is inserted at start of &runtimepath.
" - If refPath/after does not exist, path/after is added to end of &runtimepath.
function! RtpAppend(path, refPath)
if isdirectory(a:path)
let rtpParts = PathSplit(&runtimepath)
let i = index(rtpParts, a:refPath)
if i >= 0
call insert(rtpParts, a:path, i + 1)
else
call insert(rtpParts, a:path, 0)
endif
let after = a:path . "/after"
if isdirectory(after)
let i = index(rtpParts, a:refPath . "/after")
if i >= 0
call insert(rtpParts, after, i)
else
call add(rtpParts, after)
endif
endif
let &runtimepath = PathJoin(rtpParts)
endif
endfunction
" Inserts directory "path" right after $VIMUSERFILES in &runtimepath. If
" "path/after" exists, it will be inserted just before $VIMUSERFILES/after (or
" appended to the end of &runtimepath if $VIMUSEFILES/after is not in
" &runtimepath).
"
" The goal is to allow inheriting another user's configuration. This will get
" &runtimepath fixed up correctly, but you still need to source the before and
" after scripts within your before and after scripts, respectively.
function! RtpInherit(path)
call RtpAppend(a:path, $VIMUSERFILES)
endfunction
" -------------------------------------------------------------
" Pathogen plugin management (part one)
" -------------------------------------------------------------
" A couple of bundles must be initialized early for use in |VIMRC_BEFORE|
" scripts.
runtime bundle/pathogen/autoload/pathogen.vim
" The calls to expand() below launder the slashes to backslashes
" on Windows as well as expand environment variables. This is
" helpful to ensure that pathogen does not re-infect these plugins
" (since pathogen doesn't normalize the slashes during comparisons).
" We'd like to detect fonts in |VIMRC_BEFORE|.
call pathogen#surround(expand("$VIMFILES/bundle/fontdetect"))
" We'd like to set colorschemes in |VIMRC_BEFORE|.
call pathogen#surround(expand("$VIMFILES/bundle/colorsamplerpack"))
" Now restore $VIMFILES area to surround the other bundles, giving it higher
" priority than the added bundles.
call pathogen#surround($VIMFILES)
" -------------------------------------------------------------
" Customizing environment variables
" -------------------------------------------------------------
" NOTE: Several environment variables follow that may be customized.
" See doc/notes.txt in the |notes_customizations| section for details about
" these variables.
"
" Environment variables are used instead of Vim variables to allow
" configuration at the operating-system level outside of Vim.
" If local customizations directory exists, it takes precedence.
call RtpPrepend($VIMFILES . "/local")
" Setup an environment variable for cache-related bits. This follows
" XDG_CACHE_HOME by default, but can be overridden by the user.
if $VIM_CACHE_DIR == ""
if $XDG_CACHE_HOME != ""
let $VIM_CACHE_DIR = expand("$XDG_CACHE_HOME/vim")
else
if has("win32")
let $VIM_CACHE_DIR = expand("$USERPROFILE/.cache/vim")
else
let $VIM_CACHE_DIR = expand("$HOME/.cache/vim")
endif
endif
endif
" VIMUSER defaults to the logged-in user, but may be overridden to allow
" multiple user to share the same overrides (e.g., to let "root" share settings
" with another user).
if $VIMUSER == ""
let $VIMUSER = expand("$USER")
endif
" By default, don't permit old-style $VIMUSER-before.vim and $VIMUSER-after.vim.
let s:allowOldVimuserScripts = 0
" Probe for per-user directories. To allow them to be separately
" source-controlled, check outside $VIMFILES tree first, then inside.
if $VIMUSERFILES == ""
let $VIMUSERFILES = fnamemodify($VIMFILES, ":h")
if has("win32")
let $VIMUSERFILES .= "/_vimuser"
else
let $VIMUSERFILES .= "/.vimuser"
endif
if !isdirectory($VIMUSERFILES)
let $VIMUSERFILES = expand("$VIMFILES/user/$VIMUSER")
" For backward compatibility, allow old-style $VIMUSER-before.vim and
" $VIMUSER-after.vim scripts.
let s:allowOldVimuserScripts = 1
endif
endif
" VIMRC_BEFORE points directly to the script to execute.
if $VIMRC_BEFORE == ""
let $VIMRC_BEFORE = expand("$VIMUSERFILES/vimrc-before.vim")
" For backward compatibility:
if !filereadable($VIMRC_BEFORE) && s:allowOldVimuserScripts
let $VIMRC_BEFORE = expand("$VIMFILES/user/$VIMUSER-before.vim")
endif
endif
" VIMRC_AFTER points directly to the script to execute.
if $VIMRC_AFTER == ""
let $VIMRC_AFTER = expand("$VIMUSERFILES/vimrc-after.vim")
" For backward compatibility:
if !filereadable($VIMRC_AFTER) && s:allowOldVimuserScripts
let $VIMRC_AFTER = expand("$VIMFILES/user/$VIMUSER-after.vim")
endif
endif
" Prepend per-user directory to runtimepath (provides the highest priority).
call RtpPrepend($VIMUSERFILES)
" If it exists, source the specified |VIMRC_BEFORE| hook.
if filereadable($VIMRC_BEFORE)
source $VIMRC_BEFORE
endif
" -------------------------------------------------------------
" Pathogen plugin management (part two)
" -------------------------------------------------------------
" Infect all remaining bundles.
call pathogen#infect()
" Bundles in the "pre-bundle" directories will come earlier in the path
" than those in "bundle" directories.
call pathogen#infect('pre-bundle/{}')
" -------------------------------------------------------------
" Python path management
" -------------------------------------------------------------
" Setup Python's sys.path to include any "pylib" directories found
" as immediate children of paths in Vim's 'runtimepath'. This allows
" for more easily sharing Python modules.
if has('python')
function! AugmentPythonPath()
python << endpython
import vim
import os
for p in vim.eval("PathSplit(&runtimepath)"):
libPath = os.path.join(p, "pylib")
if os.path.isdir(libPath) and libPath not in sys.path:
sys.path.append(libPath)
endpython
endfunction
call AugmentPythonPath()
endif
" =============================================================
" Color schemes
" =============================================================
" Setup a color scheme early for better visibility of errors and to nail down
" 'background' (which changes when a new colorscheme is selected).
" If a user sets up a colorscheme in his |VIMRC_BEFORE| script (or early in his
" ~/.vimrc file), the varible g:colors_name will be set and no default
" colorscheme selection will take place. To use no colorscheme at all, set
" g:colors_name to the empty string.
" If the user hadn't chosen a colorscheme, we setup a default. Because in
" general there is no good way to reliably detect console background color, we
" default to assuming a dark background. Vim does try to use the environment
" variable COLORFGBG to guess the background color, but that variable is not
" always set properly (especially after ``sudo`` or ``ssh``).
" Provide a default colorscheme.
if !exists("g:colors_name")
if !has("gui_running") && &t_Co <= 8
" With only a few colors, it's better to use a simple colorscheme.
colorscheme elflord
else
" Dark scheme maintained by John Szakmeister.
colorscheme szakdark
endif
endif
" Provide a default Powerline colorscheme.
if !exists("g:Powerline_colorscheme")
if &background == "dark"
let g:Powerline_colorscheme = 'szakdark'
else
" At present, we don't have complete support for light backgrounds
" (though 'nuvola' is a start at it).
endif
endif
" =============================================================
" GUI Setup
" =============================================================
if !exists("g:DefaultFontFamilies")
let g:DefaultFontFamilies = []
endif
let g:DefaultFontFamilies += [
\ "PragmataPro for Powerline",
\ "PragmataPro",
\ "DejaVu Sans Mono for Powerline",
\ "Droid Sans Mono for Powerline",
\ "Consolas for Powerline",
\ "DejaVu Sans Mono",
\ "Droid Sans Mono",
\ "Consolas",
\]
" Font Families matching the regex patterns below have known-good Unicode
" symbols for use with Powerline.
if !exists("g:GoodUnicodeSymbolFontFamilyPatterns")
let g:GoodUnicodeSymbolFontFamilyPatterns = []
endif
let g:GoodUnicodeSymbolFontFamilyPatterns += [
\ '^PragmataPro\>',
\ '^DejaVu Sans Mono\>',
\ '^Droid Sans Mono\>',
\]
" Return type of Powerline symbols to use for given font family.
" Value will be one of "fancy", "unicode", or "compatible".
function! PowerlineSymbolsForFontFamily(family)
if a:family =~# ' Powerline$'
return "fancy"
endif
for pattern in g:GoodUnicodeSymbolFontFamilyPatterns
if a:family =~# pattern
return "unicode"
endif
endfor
return "compatible"
endfunction
function! SetFont()
if !has("gui_running")
return
endif
if !exists("g:FontFamily")
let g:FontFamily = fontdetect#firstFontFamily(g:DefaultFontFamilies)
endif
if !exists("g:FontSize")
let g:FontSize = 14
endif
if g:FontFamily != "" && g:FontSize > 0
if has("gui_gtk2")
let font = g:FontFamily . " " . g:FontSize
else
let font = g:FontFamily . ":h" . g:FontSize
endif
let &guifont = font
let g:Powerline_symbols = PowerlineSymbolsForFontFamily(g:FontFamily)
endif
endfunction
command! -bar SetFont call SetFont()
if has("gui_running")
" 'T' flag controls the toolbar (we don't need it).
set guioptions-=T
" 'a' is for Autoselect mode, in which selections will automatically be
" added to the clipboard (on Windows) or the primary selection (on Unix).
set guioptions-=a
" 'L' causes a left-side scrollbar to automatically appear when a
" vertical split is created. Unfortunately, there is a timing bug of
" some kind in Vim that sometimes prevents 'columns' from being
" properly maintained when the comings and goings of the scrollbar
" change the width of the GUI frame. The right-side scrollbar still
" functions properly for the active window, so there's no need for the
" left-side scrollbar anyway.
set guioptions-=L
SetFont
" Number of lines of text overall.
set lines=45
endif
" MacVim-specific setup. MacVim has a gvimrc setup that alters some bindings.
" We want to keep our M-Left, M-Right, M-Up, and M-Down bindings, so let's
" disable the MacVim setup, and only map the ones that don't collide with other
" mappings we make.
if has("gui_running") && has("gui_macvim")
if !exists("macvim_skip_cmd_opt_movement")
let macvim_skip_cmd_opt_movement = 1
noremap <D-Left> <Home>
noremap! <D-Left> <Home>
noremap <D-Right> <End>
noremap! <D-Right> <End>
noremap <D-Up> <C-Home>
inoremap <D-Up> <C-Home>
noremap <D-Down> <C-End>
inoremap <D-Down> <C-End>
imap <M-BS> <C-w>
imap <D-BS> <C-u>
endif
endif
" =============================================================
" General setup
" =============================================================
" Number of lines of VIM history to remember.
set history=500
" Automatically re-read files that have changed as long as there
" are no outstanding edits in the buffer.
set autoread
" Setup print options for hardcopy command.
set printoptions=paper:letter,duplex:off
" Configure mapping timeout in milliseconds (default 1000).
" Controls how long Vim waits for partially complete mapping
" before timing out and using prefix directly.
set timeout timeoutlen=3000
" Configure keycode timeout in milliseconds (default -1).
" Controls how long Vim waits for partially complete
" keycodes (such as <ESC>OH which is the <Home> key).
" If negative, uses 'timeoutlen'.
" Note that in insert mode, there is a special-case hack in the Vim
" source that checks for <Esc> and if there are no additional characters
" immediately waiting, Vim pretends to leave insert mode immediately.
" But Vim is still waiting for 'ttimeoutlen' milliseconds for keycodes,
" so if in insert mode you press <Esc>OH in console Vim (on Linux) within
" 'ttimeoutlen' milliseconds, you'll get <Home> instead of opening a new
" line above and inserting "H".
" Note: The previous value of 50 ms proved to be much too long once
" support for Alt+letter mappings were added by the fixkey plugin.
" Problems cropped up when pressing <Esc> to leave insert mode followed
" too quickly by j or k as cursor movements. With a long ttimeoutlen,
" these were being interpreted as Alt-j and Alt-k. Experimentally,
" it seems that ttimeoutlen=5 is short enough to avoid this error
" without causing other problems.
set ttimeout ttimeoutlen=5
" Disallow octal numbers for increment/decrement (CTRL-A/CTRL-X).
set nrformats-=octal
" -------------------------------------------------------------
" File settings
" -------------------------------------------------------------
" Where file browser's directory should begin:
" last - same directory as last file browser
" buffer - directory of the related buffer
" current - current directory (pwd)
" {path} - specified directory
set browsedir=buffer
" -------------------------------------------------------------
" Display settings
" -------------------------------------------------------------
" Show "ruler" at bottom (cursor position et al.).
set ruler
" Show initial characters of pending incomplete command.
set showcmd
" Use a taller command line to reduce need for pressing ENTER.
set cmdheight=2
" Show a visual bell instead of beeping.
set visualbell
" What to do when opening a new buffer. May be empty or may contain
" comma-separated list of the following words:
" useopen - use existing windows if possible.
" usetab - like useopen but also checks other tabs
" split - split current window before loading a buffer
" 'useopen' may be useful for re-using QuickFix window.
set switchbuf=
" -------------------------------------------------------------
" Setup wrapping for long lines
" -------------------------------------------------------------
" Enable wrapping of long lines.
set wrap
" Use the prompt "> " for wrapped lines.
let &showbreak=" "
" Break lines at reasonable places instead of mid-word.
set linebreak
" The 'breakat' variable determines good places to break.
" Defaults to line below:
" set breakat=\ \^I!@*-+;:,./?
" How far to scroll sideways when wrapping is off (:set nowrap).
" When zero (the default), will scroll to the middle of the screen.
" May use a small non-zero number for fast terminals.
set sidescroll=0
" Enable 'list' mode (:set list) to see non-visibles a la "reveal codes"
" in the old Word Perfect. In list mode, 'listchars' indicates
" what to show. Defaults to "eol:$", but has lots of features
" (see :help 'listchars).
" The "trail" setting means trailing whitespace.
" The feature is too disconcerting to leave on, but pre-configure
" listchars so :set list will do the right thing.
" set list
set listchars=trail:·,nbsp:·,extends:>,precedes:<,eol:$
" -------------------------------------------------------------
" Menu settings
" -------------------------------------------------------------
anoremenu 10.332 &File.Close\ All<Tab>:%bdelete :%bdelete<CR>
anoremenu 10.355 &File.Save\ A&ll<Tab>:wall :wall<CR>
" Configure the use of the Alt key to access menus.
" no - never use Alt key for menus; all Alt-key combinations are mappable.
" yes - always use Alt key for menus; cannot map Alt-key combinations.
" menu - Alt-key combinations not used by menus are mappable.
set winaltkeys=no
" -------------------------------------------------------------
" Key settings
" -------------------------------------------------------------
" Avoid the following key settings for maximum portability across terminal
" types. "No codes" means the terminal generates nothing for the given keys.
" "Aliased code" means the key generates the same code as another key, making
" the two keys indistinguishable (and the aliased key useless).
"
" gnome-terminal (TERM=xterm, COLORTERM=gnome-terminal):
" - No codes:
" <F10> (reserved for menu)
" <S-F10> (reserved for context menu)
" <S-Home> (reserved for scrollback)
" <S-End> (reserved for scrollback)
"
" Linux console (TERM=linux):
" - No codes:
" <S-F9>
" <S-F10>
" <S-F11>
" <S-F12>
" - Aliased codes:
" <S-Home> (same as <Home>)
" <S-End> (same as <End>)
"
" PuTTY (all except "SCO" mode) (TERM=putty, TERM=putty-256color):
" - Aliased codes:
" <S-F1> (same as <F11>)
" <S-F2> (same as <F12>)
" <S-F11> (same as <F11>)
" <S-F12> (same as <F12>)
" <S-Home> (same as <Home>)
" <S-End> (same as <End>)
"
" PuTTY "SCO" mode (TERM=putty-sco):
" - Aliased codes:
" <Delete> (same as <Backspace>)
" <S-Home> (same as <Home>)
" <S-End> (same as <End>)
"
" rxvt (TERM=rxvt, TERM=rxvt-unicode):
" - No codes:
" <S-Home> (reserved for scrollback for rxvt-unicode only)
" <S-End> (reserved for scrollback for rxvt-unicode only)
" - Aliased codes:
" <S-F1> (same as <F11>)
" <S-F2> (same as <F12>)
" Undo compiled-in mappings
silent! unmap <C-x>
silent! unmap <C-Del>
silent! unmap <S-Del>
silent! unmap <C-Insert>
silent! unmap <S-Insert>
silent! unmap! <S-Insert>
" Execute "make" in current directory.
nnoremap <F9> :wall<bar>make<CR>
inoremap <F9> <ESC>:wall<bar>make<CR>
" Execute current buffer.
nnoremap <F5> :wall<bar>! %:p<CR>
inoremap <F5> <ESC>:wall<bar>! %:p<CR>
" Return escaped path of directory containing current file.
function! EscapedFileDir()
return shellescape(expand("%:p:h"))
endfunction
" Signal fifo using fifosignal script.
nnoremap <F12> :wall<bar>call system("fifosignal " . EscapedFileDir())<CR>
inoremap <F12> <ESC>:wall<bar>call system("fifosignal " . EscapedFileDir())<CR>
" -------------------------------------------------------------
" Support routines
" -------------------------------------------------------------
" Return last selected text (as defined by `< and `>).
function! SelectedText()
let regA = getreg("a")
let regTypeA = getregtype("a")
silent execute 'normal! `<v`>"ay'
let text = @a
call setreg("a", regA, regTypeA)
return text
endfunction
" Return true if line is blank (i.e., contains only whitespace).
function! IsBlank(line)
return a:line =~# '^\s*$'
endfunction
" Indent function that leaves things unchanged.
" Existing (i.e., non-blank) lines keep their current indentation.
" New (blank) lines keep the indentation level of the previous non-blank line.
" Use via:
" set indentexpr=StatusQuoIndent()
function! StatusQuoIndent()
" Leave non-blank lines alone at their current indentation.
let thisLine = getline(v:lnum)
if !IsBlank(thisLine)
return indent(thisLine)
endif
let lnum = prevnonblank(v:lnum - 1)
if lnum == 0
return -1
endif
return indent(getline(lnum))
endfunction
" -------------------------------------------------------------
" QuickFix/Location List support
" -------------------------------------------------------------
" Open QuickFix window using standard position and height.
command! -bar Copen execute "botright copen " . g:QuickFixWinHeight
" Open Location List window using standard height.
command! -bar Lopen execute "lopen " . g:LocListWinHeight
" Return 1 if current window is the QuickFix window.
function! IsQuickFixWin()
if &buftype == "quickfix"
" This is either a QuickFix window or a Location List window.
" Try to open a location list; if this window *is* a location list,
" then this will succeed and the focus will stay on this window.
" If this is a QuickFix window, there will be an exception and the
" focus will stay on this window.
try
lopen
catch /E776:/
" This was a QuickFix window.
return 1
endtry
endif
return 0
endfunction
" Return 1 if current window is a Location List window.
function! IsLocListWin()
return (&buftype == "quickfix" && !IsQuickFixWin())
endfunction
" Return window number of quickfix buffer (or zero if not found).
function! GetQuickFixWinNum()
let qfWinNum = 0
let curWinNum = winnr()
for winNum in range(1, winnr("$"))
execute "noautocmd " . winNum . "wincmd w"
if IsQuickFixWin()
let qfWinNum = winNum
break
endif
endfor
execute "noautocmd " . curWinNum . "wincmd w"
return qfWinNum
endfunction
" Return 1 if current window's location list window is open.
function! LocListWinIsOpen()
let curWinNum = winnr()
let numOpenWindows = winnr("$")
" Assume location list window is already open.
let isOpen = 1
try
noautocmd lopen
catch /E776:/
" No location list available; nothing was changed.
let isOpen = 0
endtry
if numOpenWindows != winnr("$")
" We just opened a new location list window. Revert to original
" window and close the newly opened window.
noautocmd wincmd p
noautocmd lclose
let isOpen = 0
endif
return isOpen
endfunction
" Goto previous or next QuickFix or Location List message.
" messageType = "c" (for QuickFix) or "l" (for Location List).
" whichMessage = "previous" or "next".
" Return 1 on successful move.
function! GotoMessage(messageType, whichMessage)
try
execute a:messageType . a:whichMessage
catch /:E42:\|:E553:/
echo "No " . a:whichMessage . " message"
return 0
endtry
" Echo empty line to clear possible previous message.
echo ""
normal zz
return 1
endfunction
" Goto previous "thing" (diff, Location List message, QuickFix message).
function! GotoPrev()
if &diff
normal [czz
elseif LocListWinIsOpen()
call GotoMessage("l", "previous")
else
Copen
wincmd p
call GotoMessage("c", "previous")
endif
endfunction
" Goto next "thing" (diff, Location List message, QuickFix message).
function! GotoNext()
if &diff
normal ]czz
elseif LocListWinIsOpen()
call GotoMessage("l", "next")
else
Copen
wincmd p
call GotoMessage("c", "next")
endif
endfunction
" Setup previous/next browsing using F4/Shift-F4.
inoremap <silent> <F4> <C-O>:call GotoNext()<CR>
nnoremap <silent> <F4> :call GotoNext()<CR>
inoremap <silent> <S-F4> <C-O>:call GotoPrev()<CR>
nnoremap <silent> <S-F4> :call GotoPrev()<CR>
function! s:Qf2Args()
let l:files={}
argdo argdelete %
for l:lineDict in getqflist()
if l:lineDict.bufnr > 0
let l:files[bufname(l:lineDict.bufnr)]=1
endif
endfor
for l:file in keys(l:files)
execute "silent argadd " . l:file
endfor
endfunction
command! -bar Qf2Args call s:Qf2Args()
" Setup n and N for browsing to next or previous search match with automatic
" scrolling to the center of the window.
nnoremap n nzz
nnoremap N Nzz
" Move current line up one line (called from normal mode)
function! NMoveUp()
if line(".") > 1
let curCol = virtcol('.')
move .-2
exe ':silent normal ' . curCol . '|'
endif
endfunction
" Move current line down one line (called from normal mode)
function! NMoveDown()
if line(".") < line("$")
let curCol = virtcol('.')
move .+1
exe ':silent normal ' . curCol . '|'
endif
endfunction
" Move visual range up one line (called from normal mode)
function! VMoveUp()
if line("'<") > 1
silent '<,'>move '<-2
endif
" restore visual selection
silent normal! gv
endfunction
" Move visual range down one line (called from normal mode)
function! VMoveDown()
if line("'>") < line("$")
silent '<,'>move '>+1
endif
" restore visual selection
silent normal! gv
endfunction
nnoremap <silent> <M-Up> :call NMoveUp()<CR>
nnoremap <silent> <M-Down> :call NMoveDown()<CR>
nnoremap <silent> <M-k> :call NMoveUp()<CR>
nnoremap <silent> <M-j> :call NMoveDown()<CR>
inoremap <silent> <M-Up> <C-\><C-O>:call NMoveUp()<CR>
inoremap <silent> <M-Down> <C-\><C-O>:call NMoveDown()<CR>
inoremap <silent> <M-k> <C-\><C-O>:call NMoveUp()<CR>
inoremap <silent> <M-j> <C-\><C-O>:call NMoveDown()<CR>
xnoremap <silent> <M-Up> <C-C>:call VMoveUp()<CR>
xnoremap <silent> <M-Down> <C-C>:call VMoveDown()<CR>
xnoremap <silent> <M-k> <C-C>:call VMoveUp()<CR>
xnoremap <silent> <M-j> <C-C>:call VMoveDown()<CR>
xnoremap <silent> - <C-C>:call VMoveUp()<CR>
xnoremap <silent> + <C-C>:call VMoveDown()<CR>
" Invoke as:
" WithShiftWidth(1, "normal gv<gv")
" WithShiftWidth(1, ":'<,'>>")
function! WithShiftWidth(shiftWidth, toExec)
let save_sw = &sw
let &sw = a:shiftWidth
execute a:toExec
let &sw = save_sw
endfunction
" Derived from John Little's Vbs() function, posted in vim_use
" 9/8/2010 with Subject "Re: formating".
function! VMoveLeft()
if visualmode() == "\<c-v>"
let s = getpos("'<")
let e = getpos("'>")
let fl = min([s[1], e[1]])
let fc = min([s[2], e[2]])
let ll = max([s[1], e[1]])
let lc = max([s[2] + s[3], e[2] + e[3]])
let save_virtualedit = &virtualedit
let &virtualedit = "all"
call setpos(".", [0, fl, lc - (lc == 1 ? 0 : 1), 0])
execute "normal \<c-v>"
call setpos(".", [0, ll, lc - (lc == 1 ? 0 : 1), 0])
normal x
call setpos(".", [0, fl, fc - (fc == lc && fc != 1 ? 1 : 0), 0])
execute "normal \<c-v>"
call setpos(".", [0, ll, lc - (fc == lc && lc == 1 ? 0 : 1), 0])
let &virtualedit = save_virtualedit
else
call WithShiftWidth(1, ":'<,'><")
normal gv
endif
endfunction
function! VMoveRight()
if visualmode() == "\<c-v>"
execute "normal gvI\<Space>\<esc>"
normal gvl
else
call WithShiftWidth(1, ":'<,'>>")
normal gv
endif
endfunction
nnoremap <silent> <M-Left> :call WithShiftWidth(1, ":<")<CR>
nnoremap <silent> <M-Right> :call WithShiftWidth(1, ":>")<CR>
nnoremap <silent> <M-h> :call WithShiftWidth(1, ":<")<CR>
nnoremap <silent> <M-l> :call WithShiftWidth(1, ":>")<CR>
inoremap <silent> <M-Left> <C-\><C-O>:call WithShiftWidth(1, ":<")<CR>
inoremap <silent> <M-Right> <C-\><C-O>:call WithShiftWidth(1, ":>")<CR>
inoremap <silent> <M-h> <C-\><C-O>:call WithShiftWidth(1, ":<")<CR>
inoremap <silent> <M-l> <C-\><C-O>:call WithShiftWidth(1, ":>")<CR>
xnoremap <silent> <M-Left> <C-C>:call VMoveLeft()<CR>
xnoremap <silent> <M-Right> <C-C>:call VMoveRight()<CR>
xnoremap <silent> <M-h> <C-C>:call VMoveLeft()<CR>
xnoremap <silent> <M-l> <C-C>:call VMoveRight()<CR>
xnoremap <silent> <Backspace> <C-C>:call VMoveLeft()<CR>
xnoremap <silent> <Space> <C-C>:call VMoveRight()<CR>
" Strip whitespace from the left.
function! Lstrip(s)
return substitute(a:s, '^\s\+', '', "")
endfunction
" Strip whitespace from the right.
function! Rstrip(s)
return substitute(a:s, '\s\+$', '', "")
endfunction
" Remove "rubbish" whitespace (from Andy Wokula posting).
nnoremap <silent> drw :<C-U>call DeleteRubbishWhitespace()<CR>
function! DeleteRubbishWhitespace()
" Reduce many spaces or blank lines to one.
let saveVirtualEdit = [&virtualedit]
set virtualedit=
let line = getline(".")
if line =~ '^\s*$'
let savePos = winsaveview()
let saveFoldEnable = &foldenable
setlocal nofoldenable
normal! dvip0D
let savePos.lnum = line(".")
let &l:foldenable = saveFoldEnable
call winrestview(savePos)
elseif line[col(".")-1] =~ '\s'
normal! zvyiw
if @@ != " "
normal! dviwr m[
" m[ is just to avoid a trailing space
endif
endif
let [&ve] = saveVirtualEdit
silent! call repeat#set("drw")
endfunction
function! StripTrailingWhitespace()
let savePos = winsaveview()
let saveFoldEnable = &foldenable
setlocal nofoldenable
%substitute /\s\+$//ge
let &l:foldenable = saveFoldEnable
call winrestview(savePos)
endfunction
command! -bar StripTrailingWhitespace call StripTrailingWhitespace()
nnoremap <Leader><Leader>$ :StripTrailingWhitespace<CR>
" Remap Q from useless "Ex" mode to "gq" re-formatting command.
nnoremap Q gq
xnoremap Q gq
onoremap Q gq
" Paragraph re-wrapping, similar to Emacs's Meta-Q and TextMate's Ctrl-Q.
function! RewrapParagraphExpr()
return (&tw > 0 ? "gqip" : "vip:join\<CR>") . "$"
endfunction
function! RewrapParagraphExprVisual()
return (&tw > 0 ? "gq" : ":join\<CR>") . "$"
endfunction
function! RewrapParagraphExprInsert()
" Include undo point via CTRL-G u.
return "\<C-G>u\<Esc>" . RewrapParagraphExpr() . "A"
endfunction