/
.vimrc
2040 lines (1683 loc) · 57 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
"=============================================================================
" 基本設定 : {{{
" vi互換をOFF
set nocompatible
" filetype別プラグイン/インデントを有効化
filetype plugin indent on
" augroup設定
augroup MyAutoCmd
autocmd!
augroup END
" windows環境用変数
let s:iswin = has('win32') || has('win64') || has('win32unix')
" mac環境用変数
let s:ismac = has('mac')
" 現在のファイルを基準として$MYVIMRC、$HOMEを設定
let $MYVIMRC = expand('<sfile>')
let $HOME = expand('<sfile>:h')
" .vimとvimfilesの違いを吸収する
if s:iswin
let $DOTVIM = $HOME."/vimfiles"
else
let $DOTVIM = $HOME."/.vim"
endif
" .vimrcを開く
nnoremap <silent>vv :<C-u>edit $MYVIMRC<CR>
" .vimrc保存時は自動で再読み込み
autocmd MyAutoCmd BufWritePost $MYVIMRC source $MYVIMRC
" 起動時に.vimrcを開く
autocmd MyAutoCmd VimEnter * nested if @% == '' && s:GetBufByte() == 0 | edit $MYVIMRC | endif
" 起動時は常に変更なしとする(標準入力対策)
autocmd MyAutoCmd VimEnter * set nomodified
" boolean値
let s:true = 1
let s:false = 0
" }}}
"=============================================================================
" 共通関数設定 : {{{
" SID取得
function! s:SID()
return matchstr(expand('<sfile>'), '<SNR>\d\+_\zeSID$')
endfunction
" 存在確認&ディレクトリ作成
function! s:Mkdir(dir)
if !isdirectory(a:dir)
call mkdir(a:dir, 'p')
endif
endfunction
" 確認メッセージ
function! s:Confirm(msg)
return input(printf('%s [y/N]: ', a:msg)) =~? '^y\%[es]$'
endfunction
" バッファ番号からbasenameを取得
function! s:GetBufBasename(...)
let bufnr = a:0 ? a:1 : ''
let bufname = bufname(bufnr)
if bufname == ''
let buftype = getbufvar(bufnr, '&buftype')
if buftype == ''
return '[No Name]'
elseif buftype ==# 'quickfix'
return '[Quickfix List]'
elseif buftype ==# 'nofile' || buftype ==# 'acwrite'
return '[Scratch]'
endif
endif
return fnamemodify(bufname, ':t')
endfunction
" カレントバッファのサイズを取得
function! s:GetBufByte()
let byte = line2byte(line('$') + 1)
if byte == -1
return 0
else
return byte - 1
endif
endfunction
" ハイライトグループを取得
function! s:GetHighlight(group_name)
redir => hl
execute 'highlight ' . a:group_name
redir END
return substitute(substitute(hl, '[\r\n]', '', 'g'), 'xxx', '', '')
endfunction
" コマンド実行後の表示状態を維持する
function! s:ExecuteKeepView(expr)
let wininfo = winsaveview()
execute a:expr
call winrestview(wininfo)
endfunction
" 現在のバッファを作業用領域に設定
function! s:SetScratch()
setlocal bufhidden=unload
setlocal nobuflisted
setlocal buftype=nofile
setlocal noswapfile
endfunction
" エラーメッセージ
function! s:ErrorMsg(msg)
echohl ErrorMsg
echo 'ERROR: ' . a:msg
echohl None
endfunction
" }}}
"=============================================================================
" 文字コード設定 : {{{
" 内部文字コード
set encoding=utf-8
" ターミナルエンコーディング
if has('gui_running') && s:iswin
set termencoding=cp932
endif
" 文字コード判別リスト
if has('iconv')
let s:enc_euc = 'euc-jp'
let s:enc_jis = 'iso-2022-jp'
if iconv("\x87\x64\x87\x6a", 'cp932', 'euc-jisx0213') ==# "\xad\xc5\xad\xcb"
let s:enc_euc = 'euc-jisx0213,euc-jp'
let s:enc_jis = 'iso-2022-jp-3'
endif
let &fileencodings = 'ucs-bom'
if &encoding !=# 'utf-8'
let &fileencodings = &fileencodings . ',' . 'ucs-2le'
let &fileencodings = &fileencodings . ',' . 'ucs-2'
endif
let &fileencodings = &fileencodings . ',' . s:enc_jis
if &encoding ==# 'utf-8'
let &fileencodings = &fileencodings . ',' . s:enc_euc
let &fileencodings = &fileencodings . ',' . 'cp932'
elseif &encoding =~# '^euc-\%(jp\|jisx0213\)$'
let &encoding = s:enc_euc
let &fileencodings = &fileencodings . ',' . 'utf-8'
let &fileencodings = &fileencodings . ',' . 'cp932'
else
let &fileencodings = &fileencodings . ',' . 'utf-8'
let &fileencodings = &fileencodings . ',' . s:enc_euc
endif
let &fileencodings = &fileencodings . ',' . &encoding
unlet s:enc_euc
unlet s:enc_jis
endif
" 改行コード判別リスト
set fileformats=unix,dos,mac
" 新規バッファ作成時
autocmd MyAutoCmd BufNewFile * setlocal fileencoding=utf8 fileformat=unix
" 各種文字コードで開き直す
command! -bang -bar -complete=file -nargs=? Utf8 edit<bang> ++enc=utf-8 <args>
command! -bang -bar -complete=file -nargs=? Jis edit<bang> ++enc=iso-2022-jp <args>
command! -bang -bar -complete=file -nargs=? Sjis edit<bang> ++enc=cp932 <args>
command! -bang -bar -complete=file -nargs=? Euc edit<bang> ++enc=euc-jp <args>
" 各種改行コードで開き直す
command! -bang -bar -complete=file -nargs=? Unix edit<bang> ++fileformat=unix <args>
command! -bang -bar -complete=file -nargs=? Dos edit<bang> ++fileformat=dos <args>
command! -bang -bar -complete=file -nargs=? Mac edit<bang> ++fileformat=mac <args>
" }}}
"=============================================================================
" 表示設定 : {{{
" 表示メッセージロケール
language message C
" シンタックスON
if !exists('syntax_on')
syntax enable
endif
" シンタックス有効列数
set synmaxcol=1000
" シンタックスON/OFF切り替え
nnoremap <expr><F3> exists('syntax_on') ? ":\<C-u>syntax off\<CR>" : ":\<C-u>syntax enable\<CR>"
" 全画面表示
if s:iswin
autocmd MyAutoCmd GUIEnter * FullScreen
else
autocmd MyAutoCmd GUIEnter * set columns=1000 lines=1000
endif
" GUIオプション
set guioptions&
set guioptions-=m
set guioptions-=T
set guioptions-=r
set guioptions-=L
set guioptions-=e
" GUIフォント
if s:iswin
autocmd MyAutoCmd GUIEnter * set guifont=MS_Gothic:h10:cSHIFTJIS
else
autocmd MyAutoCmd GUIEnter * set guifont=Osaka-Mono:h14
endif
" ターミナルの場合は256色指定
if !has('gui_running')
set t_Co=256
endif
" コマンドライン行数
if has('gui_running')
autocmd MyAutoCmd GUIEnter * set cmdheight=1
else
set cmdheight=2
endif
" 現在のモードを表示
set showmode
" 行番号/相対行番号を表示
set number
if v:version >= 703
set relativenumber
endif
" 行を折り返して表示
set wrap
" 折り返された行の先頭に表示する文字列
let &showbreak = '+++ '
" 現在行のハイライト
if has('gui_running')
set cursorline
autocmd MyAutoCmd WinLeave * set nocursorline
autocmd MyAutoCmd WinEnter,BufRead * set cursorline
endif
" 括弧の対応表示を行う
set showmatch
" <TAB>、行末スペース、スクロール中の行頭文字の可視化
set list
set listchars=tab:>\ ,trail:-,nbsp:%,extends:>,precedes:<
" 全角スペースを視覚化
autocmd MyAutoCmd ColorScheme * highlight IdeographicSpace ctermbg=Gray guibg=Gray50
autocmd MyAutoCmd VimEnter,WinEnter * call s:VisualizeIS()
function! s:VisualizeIS()
if !exists('w:is_match')
let w:is_match = matchadd('IdeographicSpace', ' ')
endif
endfunction
" カラースキーム
if !exists('g:colors_name')
if has('gui_running')
autocmd MyAutoCmd GUIEnter * colorscheme desert
else
colorscheme desert
endif
endif
" 半透明
if has('gui_running')
if s:iswin
autocmd MyAutoCmd GUIEnter * set transparency=220
else
set transparency=10
endif
endif
" 全角文字表示幅
if exists('+ambiwidth')
if has('gui_running')
autocmd MyAutoCmd GUIEnter * nested set ambiwidth=double
else
set ambiwidth=double
endif
endif
" 全角入力時のカーソルの色を変更
if has('gui_running')
autocmd MyAutoCmd ColorScheme * highlight CursorIM guifg=#000000 guibg=#cc9999
endif
" CUI環境でも挿入モード時のカーソル形状を変更
let &t_SI.="\eP\e[5 q\e\\"
let &t_EI.="\eP\e[1 q\e\\"
" }}}
"=============================================================================
" インデント設定 : {{{
" <TAB>は空白を使用
set expandtab
" <TAB>が対応する空白量
set tabstop=8
" <TAB>で挿入される空白量
set softtabstop=4
" インデントの移動幅
set shiftwidth=4
" 行頭の余白内で<TAB>を打ち込むと'shiftwidth'の数だけインデントする
set smarttab
" 新しい行のインデントを現在行と同じにする
set autoindent
" 新しい行を作成したときに高度な自動インデントを行う
set smartindent
" tabstopの段階調整
nnoremap <silent><F4> :let &tabstop = (&tabstop * 2 > 16) ? 2 : &tabstop * 2<CR>:echo 'tabstop:' &tabstop<CR>
" 各filetype設定
autocmd MyAutoCmd FileType * call s:SetIndentFromDict()
let s:indents = {
\ 'ruby': 2,
\ 'coffee': 2,
\ }
function! s:SetIndentFromDict()
for ft in keys(s:indents)
if &filetype == ft
let indent = s:indents[ft]
execute 'setlocal softtabstop=' . indent
execute 'setlocal shiftwidth=' . indent
break
endif
endfor
endfunction
" }}}
"=============================================================================
" 入力設定 : {{{
" タイムアウト
set timeout
" 入力待ち時間
set timeoutlen=10000
" キーコードタイムアウト
set ttimeout
" キーコード入力待ち時間
set ttimeoutlen=50
" CursorHold発生時間
set updatetime=4000
" ;と:を入れ替え
noremap ; :
noremap : ;
" 全てのキーマップを表示
command! -nargs=* -complete=mapping AllMaps map <args> | map! <args> | lmap <args>
" IME状態を保存しない
if has('multi_byte_ime') || has('xim')
set iminsert=0
set imsearch=0
inoremap <silent><ESC> <ESC>
endif
" ビープ音を消去
if has('gui_running')
autocmd MyAutoCmd GUIEnter * set visualbell t_vb=
else
set visualbell
set t_vb=
endif
" xを汎用キー化
noremap x <Nop>
" 日時の短縮入力
iabbrev *dt* <C-r>=strftime("%Y/%m/%d %H:%M:%S")<CR><C-r>=<SID>Eatchar('\s')<CR>
iabbrev *d* <C-r>=strftime("%Y/%m/%d")<CR><C-r>=<SID>Eatchar('\s')<CR>
iabbrev *t* <C-r>=strftime("%H:%M:%S")<CR><C-r>=<SID>Eatchar('\s')<CR>
function! s:Eatchar(pattern)
let c = nr2char(getchar(0))
return (c =~ a:pattern) ? '' : c
endfunction
" テキストオブジェクト簡易入力
onoremap aa a<
xnoremap aa a<
onoremap ia i<
xnoremap ia i<
onoremap ar a[
xnoremap ar a[
onoremap ir i[
xnoremap ir i[
" 番号付きリストを作成
nnoremap <silent>xl :<C-u>call <SID>MakeOrderedList()<CR>
function! s:MakeOrderedList()
let current = line('.')
for i in range(1, v:count)
put =i . '. '
endfor
execute 'normal!' current + 1 . 'G'
endfunction
" }}}
"=============================================================================
" 編集設定 : {{{
" バックスペース対象
set backspace=indent,eol,start
" Yの動作をDやCと同じにする
map Y y$
" 空行を挿入
nnoremap <silent>O :<C-u>call append('.', '')<CR>j
" ノーマルモードでの改行
nnoremap x<CR> i<CR><ESC>
" コメントを継続しない
autocmd MyAutoCmd FileType * setlocal formatoptions& formatoptions-=o formatoptions-=r
" 挿入モード中の履歴保存
inoremap <C-u> <C-g>u<C-u>
inoremap <C-w> <C-g>u<C-w>
" ^Mを取り除く
command! RemoveCr call s:ExecuteKeepView('silent! %substitute/\r$//g | nohlsearch')
" 行末のスペースを取り除く
command! RemoveEolSpace call s:ExecuteKeepView('silent! %substitute/ \+$//g | nohlsearch')
" 空行を取り除く
command! RemoveBlankLine silent! global/^$/delete | nohlsearch | normal! ``
" 手動コメントアウト
noremap [Comment] <Nop>
map gC [Comment]
noremap <silent>[Comment]c :call <SID>ToggleComment('// ', '')<CR>
noremap <silent>[Comment]h :call <SID>ToggleComment('<!-- ', ' -->')<CR>
noremap <silent>[Comment]i :call <SID>ToggleComment('; ', '')<CR>
noremap <silent>[Comment]p :call <SID>ToggleComment('# ', '')<CR>
noremap <silent>[Comment]s :call <SID>ToggleComment('-- ', '')<CR>
noremap <silent>[Comment]v :call <SID>ToggleComment('" ', '')<CR>
function! s:ToggleComment(cs, ce) range
let remove = s:true
let current = a:firstline
while (current <= a:lastline)
let line = getline(current)
if strlen(line) > 0
if strpart(line, match(line, "[^[:blank:]]"), strlen(a:cs)) !=# a:cs
let remove = s:false
break
endif
if !empty(a:ce) && strpart(line, strlen(line) - strlen(a:ce)) !=# a:ce
let remove = s:false
break
endif
endif
let current += 1
endwhile
let current = a:firstline
while (current <= a:lastline)
let line = getline(current)
if strlen(line) > 0
if remove
execute 'normal! ^d' . strlen(a:cs) . 'l'
else
execute 'normal! 0i' . a:cs
endif
if !empty(a:ce)
if remove
execute 'normal! $v' . (strlen(a:ce) - 1) . 'hd'
else
execute 'normal! A' . a:ce
endif
endif
endif
normal! j
let current += 1
endwhile
execute a:firstline
normal! ^
endfunction
" undo情報を破棄
nnoremap <silent>xU :<C-u>call <SID>DropUndoInfo()<CR>
function! s:DropUndoInfo()
if &modified
call s:ErrorMsg('This buffer has been modified!')
return
endif
let old_undolevels = &undolevels
set undolevels=-1
execute "normal! aa\<BS>\<ESC>"
let &modified = s:false
let &undolevels = old_undolevels
endfunction
" HTMLインデント
command! HtmlIndent call s:HtmlIndent()
function! s:HtmlIndent()
%substitute/>\zs\ze</\r/ge
let old = &filetype
set filetype=xml
normal! ggVG=
let &filetype = old
endfunction
" 数値を連番に置換
vnoremap <silent><C-a> :call <SID>Sequence()<CR>
function! s:Sequence() range
normal! jk
let c = col('.')
for n in range(1, a:lastline - a:firstline)
execute 'normal! j' . n . "\<C-a>"
call cursor('.', c)
endfor
endfunction
" 保存時に存在しないディレクトリを自動で作成
autocmd MyAutoCmd BufWritePre * call s:AutoMkdir(expand('<afile>:p:h'), v:cmdbang)
function! s:AutoMkdir(dir, force)
if !isdirectory(a:dir) && (a:force || s:Confirm('"' . a:dir . '" does not exist. Create?'))
call s:Mkdir(a:dir)
endif
endfunction
" }}}
"=============================================================================
" 移動設定 : {{{
" wの動作をeに変更
noremap w e
noremap W E
" カーソルを行頭、行末で止まらないようにする
set whichwrap=b,s,[,],<,>
noremap h <Left>
noremap l <Right>
" ビジュアルモード時の$は改行まで移動しない
set selection=old
" 対応移動ペア
set matchpairs=(:),{:},[:],<:>
" カーソルの上または下に表示する最低行数
set scrolloff=5
" スクロール時に列を維持する
set nostartofline
" 最後に編集した内容を選択
nnoremap gm `[v`]
vnoremap gm :<C-u>normal gm<CR>
onoremap gm :<C-u>normal gm<CR>
" phpにてlast_patternを変更しない関数移動
autocmd MyAutoCmd FileType php call s:RemapPHPSectionJump()
function! s:RemapPHPSectionJump()
let function = '\(abstract\s\+\|final\s\+\|private\s\+\|protected\s\+\|public\s\+\|static\s\+\)*function'
let class = '\(abstract\s\+\|final\s\+\)*class'
let interface = 'interface'
let section = '\(.*\%#\)\@!\_^\s*\zs\('.function.'\|'.class.'\|'.interface.'\)'
exe "nno <buffer> <silent> [[ :<C-u>call search('" . escape(section, '|') . "', 'b')<CR>"
exe "nno <buffer> <silent> ]] :<C-u>call search('" . escape(section, '|') . "')<CR>"
exe "ono <buffer> <silent> [[ :<C-u>call search('" . escape(section, '|') . "', 'b')<CR>"
exe "ono <buffer> <silent> ]] :<C-u>call search('" . escape(section, '|') . "')<CR>"
endfunction
" }}}
"=============================================================================
" 検索設定 : {{{
" 検索パターンにマッチした箇所を強調表示
set hlsearch
" インクリメンタル検索
set incsearch
" 検索文字列の大文字小文字を区別しない
set ignorecase
" 検索文字列に大文字が含まれている場合は区別して検索する
set smartcase
" 循環検索
set wrapscan
" *による検索時に初回は移動しない
nnoremap <silent>* viw:<C-u>call <SID>StarSearch()<CR>:<C-u>set hlsearch<CR>`<
vnoremap <silent>* :<C-u>call <SID>StarSearch()<CR>:<C-u>set hlsearch<CR>
function! s:StarSearch()
let orig = @"
normal! gvy
let text = @"
let @/ = '\V' . substitute(escape(text, '\/'), '\n', '\\n', 'g')
let @" = orig
endfunction
" '/'と'?'を自動エスケープ
cnoremap <expr>/ getcmdtype() == '/' ? '\/' : '/'
cnoremap <expr>? getcmdtype() == '?' ? '\?' : '?'
" 選択範囲内の検索
vnoremap <silent>/ :<C-u>call <SID>RangeSearch('/')<CR>
vnoremap <silent>? :<C-u>call <SID>RangeSearch('?')<CR>
function! s:RangeSearch(d)
let s = input(a:d)
if strlen(s) > 0
let s = a:d . '\%V' . s . "\<CR>"
call feedkeys(s, 'n')
endif
endfunction
" ハイライト消去
nnoremap <silent># :<C-u>nohlsearch<CR>
" }}}
"=============================================================================
" コマンド設定 : {{{
" コマンド補完候補を表示
set wildmenu
" 補完モード
set wildmode=longest,list,full
" 補完時に大文字小文字を区別しない
set wildignorecase
" 入力中コマンドを表示
set showcmd
" 履歴保存数
set history=1000
" Emacs風キーバインド
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <C-d> <Del>
cnoremap <C-h> <BS>
" zsh風履歴検索
cnoremap <C-p> <Up>
cnoremap <Up> <C-p>
cnoremap <C-n> <Down>
cnoremap <Down> <C-n>
" コマンドの出力を別ウィンドウで開く
command! -nargs=+ -complete=command Capture silent call s:CmdCapture(<q-args>)
function! s:CmdCapture(cmd)
redir => result
execute a:cmd
redir END
new
call s:SetScratch()
file `='Capture: ' . a:cmd`
call setline(1, split(substitute(result, '^\n\+', '', ''), '\n'))
endfunction
" ファイルパス簡易入力
cnoremap <C-l>f <C-r>=expand('%:t')<CR>
cnoremap <C-l>d <C-r>=expand('%:p:h')<CR>/
cnoremap <C-l>a <C-r>=expand('%:p')<CR>
cnoremap <C-l>c <C-r>=getcwd()<CR>/
" }}}
"=============================================================================
" ファイル設定 : {{{
" ファイルパス区切り文字
if s:iswin
" set shellslash
endif
" 拡張子毎のfiletype指定
autocmd MyAutoCmd BufRead,BufNewFile *.ctp set filetype=php
autocmd MyAutoCmd BufRead,BufNewFile *.jade set filetype=jade
autocmd MyAutoCmd BufRead,BufNewFile *.coffee set filetype=coffee
" filetypeのエイリアス
autocmd MyAutoCmd FileType js set filetype=javascript
autocmd MyAutoCmd FileType rb set filetype=ruby
autocmd MyAutoCmd FileType py set filetype=python
" 手動filetype設定
nnoremap xof :<C-u>set filetype=
" 編集中ファイルのリネーム
command! -nargs=0 Rename call s:Rename()
function! s:Rename()
let filename = input('New filename: ', expand('%:p:h') . '/', 'file')
if filename != '' && filename !=# 'file'
execute 'file' filename
write
call delete(expand('#'))
endif
endfunction
" スクラッチバッファ
command! -nargs=? -complete=filetype Scratch call s:MakeScratchBuffer(<q-args>, '')
command! -nargs=? -complete=filetype NScratch call s:MakeScratchBuffer(<q-args>, 'n')
command! -nargs=? -complete=filetype VScratch call s:MakeScratchBuffer(<q-args>, 'v')
command! -nargs=? -complete=filetype TScratch call s:MakeScratchBuffer(<q-args>, 't')
function! s:MakeScratchBuffer(filetype, open)
if a:open == 'n'
new
elseif a:open == 'v'
vnew
elseif a:open == 't'
tabnew
else
enew
endif
if a:filetype != ''
let &filetype = a:filetype
endif
call s:SetScratch()
endfunction
" ジャンクファイル
command! -nargs=0 JunkFile call s:OpenJunkFile()
function! s:OpenJunkFile()
let junk_dir = $DOTVIM . strftime('/tmp/junk/%Y/%m')
call s:Mkdir(junk_dir)
let filename = input('Junk name: ', junk_dir . strftime('/%Y-%m-%d-%H%M%S.'))
if filename != ''
execute 'edit ' . filename
endif
endfunction
" ファイル/パス名判定文字
if s:iswin
set isfname&
set isfname-=:
endif
" 各種ファイルオープン
nnoremap [Edit] <Nop>
nmap e [Edit]
nnoremap [Edit]e :edit<SPACE>
nnoremap [Edit]n :new<SPACE>
nnoremap [Edit]t :tabedit<SPACE>
nnoremap [Edit]v :vnew<SPACE>
" mkdirコマンド
command! -nargs=1 -complete=file Mkdir call s:Mkdir(<q-args>)
" }}}
"=============================================================================
" バッファ設定 : {{{
" ファイルを切り替える際に自動で隠す
set hidden
" 他のプログラムで書き換えられたら自動で再読み込み
set autoread
" 現在のバッファ名をヤンク
nnoremap <silent>y% :<C-u>let @" = expand('%:p')<CR>
" カレントバッファの情報を表示
nnoremap <silent><C-g> :<C-u>call <SID>BufInfo()<CR>
function! s:BufInfo()
echo '[ fpath ]' expand('%:p')
echo '[ bufnr ]' bufnr('%')
if filereadable(expand('%'))
echo '[ mtime ]' strftime('%Y-%m-%d %H:%M:%S', getftime(expand('%')))
endif
echo '[ fline ]' line('$') 'lines'
echo '[ fsize ]' s:GetBufByte() 'bytes'
endfunction
" 全バッファを削除
command! -nargs=0 AllWipeout call s:AllWipeout()
function! s:AllWipeout()
for i in range(1, bufnr('$'))
if bufexists(i)
execute 'bwipeout ' . i
endif
endfor
endfunction
" ウィンドウのレイアウトを崩さずにバッファを閉じる
command! -nargs=0 BdKeepWin call s:BdKeepWin()
function! s:BdKeepWin()
if bufname('%') != ''
let curbuf = bufnr('%')
let altbuf = bufnr('#')
let buflist = filter(range(1, bufnr('$')), 'buflisted(v:val) && curbuf != v:val')
if len(buflist) == 0
enew
elseif curbuf != altbuf
execute 'buffer ' . (buflisted(altbuf) ? altbuf : buflist[0])
else
execute 'buffer ' . buflist[0]
endif
if buflisted(curbuf) && bufwinnr(curbuf) == -1
execute 'bdelete ' . curbuf
endif
endif
endfunction
" }}}
"=============================================================================
" ウィンドウ設定 : {{{
" 自動調整を行う
set equalalways
" サイズ調整は幅と高さ
set eadirection=both
" ウィンドウを最大化
nnoremap <C-w>o <C-w>_<C-w>\|
" 他のウィンドウを閉じる
nnoremap <C-w>O <C-w>o
" プレビューウィンドウの高さ
set previewheight=15
" プレビューウィンドウを閉じる
nnoremap <silent><C-w>z :<C-u>call <SID>closePreviewWindow()<CR>
function! s:closePreviewWindow()
if &previewwindow
execute "normal! \<C-w>p"
endif
execute "normal! \<C-w>z"
endfunction
" }}}
"=============================================================================
" ステータスライン設定 : {{{
" ステータスラインを常に表示
set laststatus=2
" ステータスライン表示内容
if has('vim_starting')
let &statusline = ''
let &statusline .= '%{' . s:SID() . 'GetBufBasename()}'
let &statusline .= '%h'
let &statusline .= '%w'
let &statusline .= '%m'
let &statusline .= '%r'
let &statusline .= ' (%<%{expand("%:p:~:h")}) '
let &statusline .= '%='
let &statusline .= '[HEX:%B]'
let &statusline .= '[R:%l(%p%%)]'
let &statusline .= '[C:%c]'
let &statusline .= '%y'
let &statusline .= '[%{&fileencoding != "" ? &fileencoding : &encoding}%{&bomb ? "(BOM)" : ""}]'
let &statusline .= '[%{&fileformat}]'
endif
" 挿入モード時のステータスラインの色を変更
autocmd MyAutoCmd InsertEnter * call s:SwitchStatusLine(s:true)
autocmd MyAutoCmd InsertLeave * call s:SwitchStatusLine(s:false)
let s:hl_statusline = ''
function! s:SwitchStatusLine(insert)
if a:insert
silent! let s:hl_statusline = s:GetHighlight('StatusLine')
execute 'highlight StatusLine ctermfg=Black ctermbg=Yellow guifg=#000000 guibg=#aa4400'
else
highlight clear StatusLine
execute 'highlight ' . s:hl_statusline
endif
endfunction
" }}}
"=============================================================================
" タブ設定 : {{{
" タブバーは常に表示
set showtabline=2
" タブライン表示内容
let &tabline = '%!'. s:SID() . 'MyTabLine()'
function! s:MyTabLine()
let labels = map(range(1, tabpagenr('$')), 's:MyTabLabel(v:val)')
let info = ''
let info .= fnamemodify(getcwd(), ':~')
return join(labels, '') . '%T%=' . info
endfunction
function! s:MyTabLabel(n)
let curbufnr = tabpagebuflist(a:n)[tabpagewinnr(a:n) - 1]
let label = ''
let label .= '%' . a:n . 'T'
let label .= '%#' . (a:n == tabpagenr() ? 'TabLineSel' : 'TabLine') . '#'
let label .= ' '
let label .= a:n . ':'
let label .= ' '
let label .= getbufvar(curbufnr, '&modified') ? '+' : ''
let label .= s:GetBufBasename(curbufnr)
let label .= ' '
let label .= '%#TabLineFill#'
return label
endfunction
" 次/前のタブ
nnoremap <C-n> gt
nnoremap <C-p> gT
" 基本マップ
nnoremap [Tab] <Nop>
nmap <C-t> [Tab]
" タブを移動
nnoremap <silent>[Tab]H :<C-u>call <SID>MoveTabPosition('left')<CR>
nnoremap <silent>[Tab]L :<C-u>call <SID>MoveTabPosition('right')<CR>
nnoremap <silent>[Tab]K :<C-u>call <SID>MoveTabPosition('top')<CR>
nnoremap <silent>[Tab]J :<C-u>call <SID>MoveTabPosition('bottom')<CR>
function! s:MoveTabPosition(direction)
if a:direction == 'left'
execute 'tabmove ' . (tabpagenr() - 2)
elseif a:direction == 'right'
execute 'tabmove ' . (tabpagenr())
elseif a:direction == 'top'
execute 'tabmove 0'
elseif a:direction == 'bottom'
execute 'tabmove 99'
endif
endfunction
" タブを閉じる
nnoremap [Tab]c :<C-u>tabclose<CR>
" 現在以外のタブを閉じる
nnoremap [Tab]o :<C-u>tabonly<CR>
" タブ毎にカレントディレクトリを保持
autocmd MyAutoCmd TabEnter * if exists('t:cwd') | execute "cd" fnameescape(t:cwd) | endif
autocmd MyAutoCmd TabLeave * let t:cwd = getcwd()
autocmd MyAutoCmd BufEnter * if !exists('t:cwd') | call InitTabpageCd() | endif
function! InitTabpageCd()
if !has('vim_starting')
if @% != ''
let curdir = input('Current directory: ', expand("%:p:h"), 'file')
silent! cd `=fnameescape(curdir)`
else
cd ~
endif
endif
let t:cwd = getcwd()
endfunction
" }}}
"=============================================================================
" QuickFix設定 : {{{
" 基本マップ
nnoremap [QuickFix] <Nop>
nmap q [QuickFix]
" 開く/閉じる
nnoremap <silent>[QuickFix]w :<C-u>cwindow<CR>
nnoremap <silent>[QuickFix]o :<C-u>copen<CR>
nnoremap <silent>[QuickFix]c :<C-u>cclose<CR>
" 進む/戻る/先頭/最後
nnoremap <silent>[QuickFix]n :cnext<CR>
nnoremap <silent>[QuickFix]p :cprevious<CR>
nnoremap <silent>[QuickFix]r :<C-u>crewind<CR>
nnoremap <silent>[QuickFix]l :<C-u>clast<CR>
" QuickFixリストの進む/戻る
nnoremap <silent>[QuickFix]N :cnewer<CR>
nnoremap <silent>[QuickFix]P :colder<CR>
" QuickFixリストが生成されたら自動で開く