vimrc, vimperatorrc
[dotfiles.git] / vim / vimrc
1 " Bundle ------------------------------------------------------------------ {{{
2 " Sun Sep 21 00:46:43 CST 2014
3 " refer https://github.com/connermcd/dotfiles.git
4 " and  https://bitbucket.org/sjl/dotfiles
5 set nocompatible
6 filetype off
7
8 set rtp+=~/.vim/bundle/Vundle.vim
9 call vundle#begin()
10
11 Plugin 'gmarik/Vundle.vim'
12 Bundle 'vim-scripts/FuzzyFinder'
13 Bundle 'vim-scripts/L9'
14 Bundle 'Valloric/YouCompleteMe'
15 Bundle 'octol/vim-cpp-enhanced-highlight'
16 Bundle 'bling/vim-airline'
17 Bundle 'payneseu/nerdtree'
18 Bundle 'Townk/vim-autoclose'
19 Bundle 'majutsushi/tagbar'
20 Bundle 'vim-scripts/tcd.vim'
21 Bundle 'vim-scripts/cmdline-completion'
22 Bundle 'vim-scripts/vcscommand.vim'
23 Bundle 'Lokaltog/vim-easymotion'
24 "Bundle 'vim-scripts/YankRing.vim'
25 Bundle 'vim-scripts/gtags.vim'
26 Bundle 'kshenoy/vim-signature'
27 Bundle 'nathanaelkane/vim-indent-guides'
28 Bundle 'rking/ag.vim'
29 Bundle 'payneseu/mark-2.8.4'
30 "Bundle 'derekwyatt/vim-fswitch'
31 Bundle 'sjl/gundo.vim'
32 Bundle 'tpope/vim-markdown'
33 "Bundle 'vim-scripts/BreakPts'
34 "Bundle 'vim-scripts/genutils'
35 "Bundle 'vim-scripts/Marks-Browser'
36 Bundle 'MattesGroeger/vim-bookmarks'
37 "Bundle 'tpope/vim-repeat'
38 "Bundle 'kien/ctrlp.vim'
39 "disable since some conflicting mapping
40 Bundle 'vim-latex/vim-latex'
41
42 call vundle#end()
43 filetype plugin indent on
44
45 " }}}
46 " Basic options ----------------------------------------------------------- {{{
47 set number
48 set relativenumber
49 set tabstop=4
50 set softtabstop=4
51 set shiftwidth=4
52 set smartindent
53 set smarttab
54 set vb t_vb=
55 set scrolloff=5
56 set sidescroll=10
57 set history=1000
58 set ruler
59 set showcmd
60 set incsearch
61 set hidden
62 set diffopt=filler,vertical
63
64 "set list
65 set listchars=tab:▸\ ,eol:¬,extends:❯,precedes:❮
66 set ttymouse=xterm2
67 set mouse=a
68 set splitbelow
69 set splitright
70 set nobackup
71 set noswapfile
72 set magic
73 set completeopt=longest,menuone
74 set ttyfast
75 "set wildmode=list:longest
76 set wildmenu
77 set wildignore=*.o,*.obj,*~,*.swp,*.DS_Store,*.git,*.svn,*.hg
78 set lazyredraw
79 set hlsearch
80 set autoindent
81 set confirm
82 set nostartofline   " don't jump to first character when paging
83 set wrap
84 set expandtab
85 "set textwidth, formatoptions for chinese
86 set formatoptions+=m
87 set colorcolumn=+1
88 syntax on
89 " colorscheme {{{
90 set cursorline
91 if has("gui_running")
92         set macmeta  "" Macvim only
93         colorscheme molokai
94         set guioptions+=c
95         set guifont=Sauce\ Code\ Powerline\ Light:h11
96         highlight Cursor guifg=white guibg=green
97         set guicursor=a:blinkon0
98         set guioptions=eac
99 "       set lines=999
100 "       set columns=999
101 else
102         set t_Co=256
103         colorscheme molokai22
104         " http://sourceforge.net/mailarchive/forum.php?thread_name=AANLkTinkbdoZ8eNR1X2UobLTeww1jFrvfJxTMfKSq-L%2B%40mail.gmail.com&forum_name=tmux-users
105         "https://gist.github.com/andyfowler/1195581  
106         if exists('$TMUX')
107           let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
108           let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"
109         else
110           let &t_SI = "\<Esc>]50;CursorShape=1\x7"
111           let &t_EI = "\<Esc>]50;CursorShape=0\x7"
112         endif"
113 endif
114         
115 " }}}
116 " }}}
117 " Mappings ---------------------------------------------------------------- {{{
118 let mapleader = ";"
119 map Q gq
120 " Normal mode ------------------------------------------------------------- {{{
121 nnoremap Y      y$
122 nnoremap ' `
123 nnoremap ` '
124 nnoremap J      <C-D>
125 nnoremap K      <C-U>
126
127 nmap <Leader>w  :w<CR>
128 nmap <Leader>q  :q<CR>
129 nmap <Leader>e  :e<Space>
130 nmap <Leader>W  :w !sudo tee %<CR>
131 nmap <Leader>h  :vertical help<Space>
132 nmap <Leader>t  :tabedit %<Space>
133 "nmap <Leader>x :x<CR>
134
135 " windows 
136 nmap <C-H>  <C-W>h
137 nmap <C-J>  <C-W>j
138 nmap <C-L>  <C-W>l
139 nmap <C-K>  <C-W>k
140 nnoremap <Leader>v      <C-W>v
141 nnoremap <Leader>s      <C-W>s
142 "nnoremap  so           <C-W>o
143 nmap +  <C-w>3+
144 nmap -  <C-w>3-
145 nmap <  <C-w>3<
146 nmap >  <C-w>3>
147 nmap =  <C-w>=
148
149 nnoremap <C-D>  <C-D>zz
150 nnoremap <C-U>  <C-U>zz
151
152 nnoremap <SPACE>        <C-F>
153 nnoremap <S-SPACE>      <C-B>
154 nnoremap <BS>   <C-B>
155 nnoremap <A-n>  gt
156 nnoremap <A-p>  gT
157 "nmap <A-TAB>   :bn<CR>
158 " map <A-TAB> Alt+TAB  
159 "noremap <A-S-TAB> "input C-V then input key sequence to
160 "http://vim.wikia.com/wiki/Make_Shift-Tab_work
161 "nnoremap <ESC><TAB> :bn<CR>
162 nnoremap <ESC><TAB> :tabnext<CR>
163 "nnoremap \e\19 :bp<CR>
164 nnoremap \e\19 :tabprevious<CR>
165 "nmap <A-S-TAB> :bp<CR>
166 "nmap <A-SPACE> :b#<CR>
167 "nmap <Leader>d :bd<CR>
168 "" keymappig for register operations
169 nnoremap <Leader>r      :registers<CR>
170 "noremap <Leader>x      :<C-p>
171 nnoremap <Leader>a :
172
173 nmap <Leader>p  "*p
174
175
176 nmap <A-=> :resize<CR>:vertical resize<CR>
177 nmap \e= :resize<CR>:vertical resize<CR>
178
179 nnoremap n nzzzv
180 nnoremap N      Nzzzv
181 nnoremap g;     g;zzzv
182 nnoremap g,     g,zzzv
183 nnoremap zR     zRzz
184 nnoremap zr     zrzz
185 nnoremap zn znzz
186
187 nnoremap zj zjzz
188 nnoremap zh zhzz
189
190 noremap <Leader>z       zmzvzz
191 nmap \\ :nohlsearch<CR>
192 " }}}
193 " Insert mode ------------------------------------------------------------- {{{
194 inoremap jj             <Esc>
195 inoremap <C-U> <C-G>u<C-U>
196 inoremap <C-a>  <Home>
197 inoremap <C-e>  <End>
198 inoremap <C-D>  <Del>
199 inoremap <C-b>  <S-Left>
200 inoremap <C-f>  <S-Right>
201 imap <A-d>      <S-Right><C-w>
202 " }}}
203 " Command mode ------------------------------------------------------- {{{
204 cmap jj         <C-c>
205 cnoremap <C-A>  <Home>
206 cnoremap <C-E>  <End>
207 cnoremap <C-B>  <Left>
208 cnoremap <C-F>  <Right>
209 cnoremap <C-D>  <Del>
210
211
212 if has("gui_running")
213 " only for Macvim <D-Left> D is Command key
214         cmap <A-a>      <D-Left>
215         cmap <A-e>      <D-Right>
216
217         cnoremap <A-b>  <S-Left>
218         cnoremap <A-f>  <S-Right>
219         cnoremap <A-d>  <S-Right><S-Right><S-Left><C-w>
220 else
221         cnoremap \eb     <S-Left>
222         cnoremap \ef     <S-Right>
223         cnoremap \ed     <S-Right><S-Right><S-Left><C-w>
224 endif
225 " delet a word left of cursor
226 cmap <S-BS>     <C-w>
227 " }}}
228 " Visual mode ------------------------------------------------------------- {{{
229 "" key mapping for  visul mode
230 vmap <C-k>      {       
231 vmap <C-j>      }       
232 "vmap <Leader>y "*y
233 vmap Y  "*y
234 "
235 "vmap <C-Y>  "qy
236 "vmap <C-P>  "qp
237 vnoremap    Y   "*y
238 vnoremap    <Leader>p  "*p
239 "search for visually selectecd text
240 vnoremap // y/<C-R>"<CR>
241 vnoremap <Leader>a  :
242 " }}}
243 " }}}
244 " Plugins Configuration --------------------------------------------------- {{{
245 " Fuzzy Finder ------------------------------------------------------------ {{{
246 let g:fuf_modesDisable = []
247 let g:fuf_mrufile_maxItem = 400
248 let g:fuf_mrucmd_maxItem = 400
249 let g:fuf_file_exclude = '\v\~$|\.(DS_Store|o|exe|dll|bak|orig|swp|pyc)$|(^|[/\\])\.(hg|git|bzr|svn)($|[/\\])'
250 let g:fuf_buffertag_ctagsPath = '/usr/local/bin/ctags'
251 let g:fuf_enumeratingLimit = 30
252 nnoremap <silent> sj     :FufBuffer<CR>
253 nnoremap <silent> sk     :FufFileWithCurrentBufferDir<CR>
254 nnoremap <silent> sK     :FufFileWithFullCwd<CR>
255 nnoremap <silent> s<C-k> :FufFile<CR>
256 "nnoremap <silent> sl     :FufCoverageFileChange<CR>
257 "nnoremap <silent> sL     :FufCoverageFileChange<CR>
258 "nnoremap <silent> s<C-l> :FufCoverageFileRegister<CR>
259 nnoremap <silent> sd     :FufDirWithCurrentBufferDir<CR>
260 nnoremap <silent> sD     :FufDirWithFullCwd<CR>
261 nnoremap <silent> s<C-d> :FufDir<CR>
262 nnoremap <silent> sn     :FufMruFile<CR>
263 nnoremap <silent> sN     :FufMruFileInCwd<CR>
264 nnoremap <silent> sm     :FufMruCmd<CR>
265 "nnoremap <silent> su     :FufBookmarkFile<CR>
266 nnoremap <silent> s<C-u> :FufBookmarkFileAdd<CR>
267 "vnoremap <silent> s<C-u> :FufBookmarkFileAddAsSelectedText<CR>
268 nnoremap <silent> si     :FufBookmarkDir<CR>
269 nnoremap <silent> s<C-i> :FufBookmarkDirAdd<CR>
270 nnoremap <silent> sT     :FufTag<CR>
271 nnoremap <silent> st     :FufTag!<CR>
272 nnoremap <silent> s<C-]> :FufTagWithCursorWord!<CR>
273 nnoremap <silent> s,     :FufBufferTag<CR>
274 nnoremap <silent> s<     :FufBufferTag!<CR>
275 "vnoremap <silent> s,     :FufBufferTagWithSelectedText!<CR>
276 "vnoremap <silent> s<     :FufBufferTagWithSelectedText<CR>
277 nnoremap <silent> s}     :FufBufferTagWithCursorWord!<CR>
278 nnoremap <silent> sl     :FufBufferTagAll<CR>
279 "nnoremap <silent> s.     :FufBufferTagAll<CR>
280 nnoremap <silent> s>     :FufBufferTagAll!<CR>
281 "vnoremap <silent> s.     :FufBufferTagAllWithSelectedText!<CR>
282 "vnoremap <silent> s>     :FufBufferTagAllWithSelectedText<CR>
283 "nnoremap <silent> s]     :FufBufferTagAllWithCursorWord!<CR>
284 nnoremap <silent> sG     :FufTaggedFile<CR>
285 nnoremap <silent> sg     :FufTaggedFile!<CR>
286 "nnoremap <silent> so     :FufJumpList<CR>
287 "nnoremap <silent> sp     :FufChangeList<CR>
288 "nnoremap <silent> sq     :FufQuickfix<CR>
289 "nnoremap <silent> sy     :FufLine<CR>
290 "  nnoremap <silent> sh     :FufHelp<CR>
291 "nnoremap <silent> se     :FufEditDataFile<CR>
292 nnoremap <silent> sr     :FufRenewCache<CR>
293 " }}}
294 " YouCompleteMe ----------------------------------------------------------- {{{
295 "syntax on, must before the YCM,
296
297 "let g:ycm_collect_identifiers_from_tags_files = 1
298 "let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py'
299 let g:ycm_auto_trigger = 1
300 let g:ycm_seed_identifiers_with_syntax = 1
301 let g:ycm_show_diagnostics_ui = 0
302 let g:ycm_enable_diagnostic_signs = 0
303 set completeopt-=preview   " disable preview windows for completion
304 let g:ycm_confirm_extra_conf = 0
305 " delete default mapping for <leader>d
306 let g:ycm_key_detailed_diagnostics = ''
307
308 " }}}
309 " Airline ----------------------------------------------------------------- {{{
310 "if has("gui_running")
311
312   let g:airline_mode_map = {
313       \ '__' : '-',
314       \ 'n'  : 'N',
315       \ 'i'  : 'I',
316       \ 'R'  : 'R',
317       \ 'c'  : 'C',
318       \ 'v'  : 'V',
319       \ 'V'  : 'V',
320       \ '\16' : 'V',
321       \ 's'  : 'S',
322       \ 'S'  : 'S',
323       \ '\13' : 'S',
324       \ }
325
326         let g:airline_theme = 'badwolf'
327
328 "if has("gui_running")
329         let g:airline_powerline_fonts=1
330         let g:airline_left_sep = ''
331         let g:airline_left_alt_sep = ''
332         let g:airline_right_sep = ''
333         let g:airline_right_alt_sep = ''
334
335 "       let g:airline#extensions#tabline#show_buffers = 1
336         let g:airline#extensions#tabline#show_buffers = 0
337         let g:airline#extensions#tabline#enabled = 1
338         let g:airline#extensions#tabline#left_sep = ''
339         let g:airline#extensions#tabline#left_alt_sep =''
340         let g:airline#extensions#tagbar#enabled = 1
341 "else 
342 "       let g:airline_powerline_fonts=1
343 "       let g:airline_left_sep = ''
344 ""      let g:airline_left_alt_sep = ''
345 "       let g:airline_right_sep = ''
346 ""      let g:airline_right_alt_sep = ''
347 "
348 "       let g:airline#extensions#tabline#show_buffers = 0
349 "       let g:airline#extensions#tabline#enabled = 1
350 "       let g:airline#extensions#tabline#left_sep = ''
351 ""      let g:airline#extensions#tabline#left_alt_sep =''
352 "       let g:airline#extensions#tagbar#enabled = 1
353 "endif
354         "
355 "       let g:airline_section_a = 'N'
356         let g:airline_section_b = '%f%m'
357         let g:airline_section_c = ''
358         let g:airline_section_x = '[%-.50{CurDir()}]'
359         let g:airline_section_y = '%y'
360         let g:airline_section_z = '%c, %l/%L %p%%'
361         let g:airline_section_warning = '' " (syntastic, whitespace)
362
363 set laststatus=2
364 set statusline=\ %<%F%m%=[%-.50{CurDir()}]\ %y\ %c,\ %l/%L\ \ %p%%\ 
365 function! CurDir()
366         let curdir = substitute(getcwd(), $HOME, "~", "g")
367         return curdir
368 endfunction
369
370 "endif
371 " }}}
372 " NerdTree ---------------------------------------------------------------- {{{
373 nmap <Leader>n  :NERDTreeToggle<CR>
374 nmap <Leader>N  :NERDTreeFind<CR>
375 " }}}
376 " AutoClose --------------------------------------------------------------- {{{
377 let g:AutoCloseProtectedRegions = ["Dialog", "ThoughBubble"]
378 " }}}
379 " TagBar ------------------------------------------------------------------ {{{
380 noremap <Leader>f       :TagbarToggle<CR><C-w>l
381 let Tlist_Ctags_Cmd="/usr/local/bin/ctags"
382 " }}}
383 " Tcd --------------------------------------------------------------------- {{{
384 cabbrev cd Tcd
385 " }}}
386 " cmdline-completion ------------------------------------------------------ {{{
387 "http://foocoder.com/blog/mei-ri-vimcha-jian-suo-jin-xian-shi-vim-indent-guides.html/
388 cmap <C-J> <Plug>CmdlineCompletionBackward
389 cmap <C-K> <Plug>CmdlineCompletionForward
390 " }}}
391 " VSCCommand -------------------------------------------------------------- {{{
392 "" ==============  VCSCommand.vim =====================
393 "let VCSCommandSVKExec='disabled no such executable'
394 let VCSCommandDeleteOnHide=1    
395 let VCSCommandDisableMappings=1
396 "let VCSCommandEnableBufferSetup=1
397 "nmap <Leader>sd        :VCSVimDiff<CR>
398 augroup VCSCommand
399         autocmd User VCSBufferCreated silent! nmap <unique> <buffer> q :bwipeout<cr> | setlocal nomodifiable
400 augroup END
401 nmap <Leader>dg         :diffget<CR>
402 " }}}
403 " YankRing ---------------------------------------------------------------- {{{
404 let g:yankring_min_element_length = 2
405 nmap \r :YRShow<CR>
406 let g:yankring_replace_n_pkey = '<A->>'
407 let g:yankring_replace_n_nkey = '<A-<>'
408 let g:yankring_history_dir = "$HOME/.vim/temp/"
409 " }}}
410 " Gtags ------------------------------------------------------------------- {{{
411 if has("gui_running")
412         noremap <A-.> :Gtags<CR>
413         noremap <A-r> :Gtags -r<CR>
414         noremap <A-o> :Gtags -s<CR>
415         noremap <A-g> :Gtags -g<CR> 
416 else
417         noremap \e. :Gtags<CR>
418         noremap \er :Gtags -r<CR>
419 "       noremap j :Gtags -s<CR>
420         noremap \eg :Gtags -g<CR> 
421 endif
422 " }}}
423 " Marks ------------------------------------------------------------------- {{{
424 nmap  \h <Plug>MarkSet
425 "nmap  * <Plug>MarkSearchNext
426 "nmap  # <Plug>MarkSearchPrev
427 " }}}
428 " Gundo ------------------------------------------------------------------- {{{
429 "    nnoremap <F5> :GundoToggle<CR>
430 " }}}
431 " Bookmarks --------------------------------------------------------------- {{{
432
433 let g:bookmark_save_per_working_dir = 1
434 let g:bookmark_auto_close = 1
435 let g:bookmark_auto_save = 1
436 " unmap the default mapping in plugins
437 nmap <Plug>DisableBookmarkToggle        <Plug>BookmarkToggle
438 nmap <Plug>DisableBookmarkAnnotate      <Plug>BookmarkAnnotate
439 nmap <Plug>DisableBookmarkShowAll       <Plug>BookmarkShowAll
440 nmap <Plug>DisableBookmarkNext          <Plug>BookmarkNext
441 nmap <Plug>DisableBookmarkPrev          <Plug>BookmarkPrev
442 nmap <Plug>DisableBookmarkClear         <Plug>BookmarkClear
443 nmap <Plug>DisableBookmarkClearAll      <Plug>BookmarkClearAll
444 " remapping
445 nmap mm <Plug>BookmarkToggle
446 "  nmap ,i <Plug>BookmarkAnnotate
447 nmap \m <Plug>BookmarkShowAll
448 let g:bookmark_center = 1
449 let g:bookmark_auto_close = 1
450 "let g:bookmark_highlight_lines = 1
451 "  nmap ,j <Plug>BookmarkNext
452 "  nmap ,k <Plug>BookmarkPrev
453 "  nmap ,c <Plug>BookmarkClear
454 "  nmap ,x <Plug>BookmarkClearAll
455 " }}}
456 " EasyMotion -------------------------------------------------------------- {{{
457 map en <Plug>(easymotion-sn)
458 map ej <Plug>(easymotion-j)
459 map ek <Plug>(easymotion-k)
460 map eb <Plug>(easymotion-b)
461 map ew <Plug>(easymotion-w)
462 map ee <Plug>(easymotion-w)
463 map el <Plug>(easymotion-lineforward)
464 map eh <Plug>(easymotion-linebackward)
465 "" }}}
466 " vim-latex --------------------------------------------------------------- {{{
467 "set grepprg=grep\ -nH\ $*
468 "let g:tex_flavor='latex'
469 "set iskeyword+=:
470 "autocmd BufEnter *.tex set sw=2
471 "disable default mapping 
472 imap #$ <Plug>IMAP_JumpForward
473 nmap #$ <Plug>IMAP_JumpForward
474 vmap #$ <Plug>IMAP_JumpForward
475 vmap #$ <Plug>IMAP_DeleteAndJumpForward
476 let g:Imap_FreezeImap=1
477 " }}}
478 " }}}
479 " FileType ---------------------------------------------------------------- {{{
480 autocmd BufNewFile,BufRead *.log set filetype=logecc
481 autocmd BufNewFile,BufRead *.log.[0-9] set filetype=logecc
482 autocmd BufNewFile,BufRead *tmux.conf set filetype=tmux
483 autocmd BufNewFile,BufRead SConstruct set filetype=python
484 autocmd BufNewFile,BufRead SConscript* set filetype=python
485 autocmd BufNewFile,BufReadPost *.md set filetype=markdown
486 autocmd BufNewFile,BufRead *.c set filetype=cpp
487
488 " http://vim.wikia.com/wiki/Update_the_diff_view_automatically
489 autocmd InsertLeave,BufWritePost,CursorHold * if &diff == 1 | diffupdate | endif
490 " setlocal nomodifiable for svn diff
491 autocmd BufWinEnter *.svn-base setlocal nomodifiable 
492
493 autocmd WinEnter * nnoremap <buffer> gj @=(&diff)?']czz':'gj'<CR> |
494         \ nnoremap <buffer> gk @=(&diff)?'[czz':'gk'<CR>
495
496 autocmd FileType qf nnoremap <buffer> <silent> q :q<CR> | setlocal nowrap | setlocal nocursorline
497 autocmd FileType help nnoremap <buffer> <silent> q :q<CR> | vertical resize 85;
498 autocmd FileType c,cpp setlocal foldmethod=syntax |
499     \ let b:AutoClosePairs = AutoClose#DefaultPairsModified("\"", "{}") |
500         \ inoremap <buffer> { {<CR>}<ESC>kA<CR> |
501     \ nnoremap <buffer> <silent> <C-]> :let word=expand("<cword>")<CR>:wincmd o<cr>:vsp<CR>:exec("tag ". word)<cr>zzzr:wincmd w<cr>
502 autocmd FileType python setlocal foldmethod=indent
503
504 autocmd FileType xml setlocal foldmethod=syntax |
505         \ let g:xml_syntax_folding=1
506 autocmd FileType vim setlocal foldmethod=marker |
507         \ setlocal formatoptions-=c formatoptions-=r formatoptions-=o |
508     \ let b:AutoClosePairs = AutoClose#DefaultPairsModified("", "{} \"")
509
510 autocmd Filetype markdown setlocal textwidth=80
511
512 autocmd CmdwinEnter * map <buffer> q :q<CR>
513
514 " }}}
515 " Functions --------------------------------------------------------------- {{{
516 " DiffOrig ---------------------------------------------------------------- {{{
517 " refert vimrc_example.vim 
518 " Convenient command to see the difference between the current buffer and the
519 " file it was loaded from, thus the changes you made.
520 " Only define it when not defined already.
521 if !exists(":DiffOrig")
522   command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
523                   \ | wincmd p | diffthis
524 endif 
525 " }}}
526 " Fold Quickfix ----------------------------------------------------------- {{{
527 "http://vim.wikia.com/wiki/Show_only_lines_in_quickfix_list_for_current_buffer 
528 if ! exists('g:foldmisses_context')
529   let g:foldmisses_context = 0
530 endif
531
532 " Add manual fold from line1 to line2, inclusive.
533 function! s:Fold(line1, line2)
534   if a:line1 < a:line2
535     execute a:line1.','.a:line2.'fold'
536   endif
537 endfunction
538
539 " Return list of line numbers for current buffer found in quickfix list.
540 function! s:GetHitLineNumbers(list)
541   let result = []
542   for d in a:list
543     if d.valid && d.bufnr == bufnr('')
544       call add(result, d.lnum)
545     endif
546   endfor
547   return result
548 endfunction
549
550 function! s:FoldMisses(list, context)
551   setlocal foldmethod=manual
552   normal! zE
553   let extra = a:context == 99999 ? g:foldmisses_context : a:context
554   let last = 0
555   for lnum in s:GetHitLineNumbers(a:list)
556 "    let start = last==0 ? 1 : last+extra
557     let start = last==0 ? 1 : last+1+extra
558     call s:Fold(start, lnum-1-extra)
559     let last = lnum
560   endfor
561   call s:Fold(last+1+extra, line('$'))
562 "  call s:Fold(last+extra, line('$'))
563 endfunction
564
565 ":[N]FoldMisses [N]     Show only the lines (and surrounding [N] lines
566 ":[N]FoldLMisses [N]    of context) in the current buffer that appear
567 "                       in the quickfix / location list.
568 "                       Missed, error-free lines are folded away.
569 command! -bar -count=99999 FoldMisses call s:FoldMisses(getqflist(), <count>)
570 command! -bar -count=99999 FoldLMisses call s:FoldMisses(getloclist(0), <count>)
571 " }}}
572 " ToggleList -------------------------------------------------------------- {{{
573 "http://vim.wikia.com/wiki/Toggle_to_open_or_close_the_quickfix_window
574 function! GetBufferList()
575   redir =>buflist
576   silent! ls
577   redir END
578   return buflist
579 endfunction
580
581 function! ToggleList(bufname, pfx)
582   let buflist = GetBufferList()
583   for bufnum in map(filter(split(buflist, '\n'), 'v:val =~ "'.a:bufname.'"'), 'str2nr(matchstr(v:val, "\\d\\+"))')
584     if bufwinnr(bufnum) != -1
585       exec(a:pfx.'close')
586       return
587     endif
588   endfor
589   if a:pfx == 'l' && len(getloclist(0)) == 0
590       echohl ErrorMsg
591       echo "Location List is Empty."
592       return
593   endif
594   let winnr = winnr()
595   exec(a:pfx.'open')
596   if winnr() != winnr
597     wincmd p
598   endif
599 endfunction
600
601 nmap <silent> \l :call ToggleList("Location List", 'l')<CR>
602 nmap <silent> \q :call ToggleList("Quickfix List", 'c')<CR>
603 " }}}
604 " DiffToggle -------------------------------------------------------------- {{{
605 "if !exists(":DiffToggle")
606 "       \ endif
607 "endif
608 function! DiffToggle()
609         if(&diff)
610                 diffoff | wincmd l | :q
611         else
612                 exec ":VCSVimDiff" | wincmd h
613         endif
614 endfunction
615 nnoremap \d :call DiffToggle()<CR>
616 "noremap <Leader>d      @=(&diff)?':diffoff':":VCSVimDiff"<CR><CR>
617 " }}}
618
619 " }}}
620
621 "nnoremap <silent> <Space> @=(foldlevel('.')?'za':"\<Space>")<CR>
622 nnoremap <silent> <Space> @=(foldlevel('.')?'za':"\<Space>")<CR>
623 "noremap <Leader>d      @=(&diff)?':diffoff':":VCSVimDiff"<CR><CR>
624
625 " https://bitbucket.org/sjl/dotfiles/src/tip/vim/vimrc
626 " Make sure Vim returns to the same line when you reopen a file.
627 " Thanks, Amit
628 augroup line_return
629     au!
630     au BufReadPost *
631         \ if line("'\"") > 0 && line("'\"") <= line("$") |
632         \     execute 'normal! g`"zvzz' |
633         \ endif
634 augroup END
635
636 iabbrev  -...  <C-R>=repeat('-', 80 - col(".") - 4 ) . " {{{"
637 iabbrev         "}   " }}}
638
639 " $ sed -n l
640 " to show the key sequence when pressing some key, 
641 " such as Ctrl-V
642 " cnfile
643 " cpfile
644
645
646 " some prefix for mapping
647 " <Leader> = ;
648 " ,
649 " \
650 " m
651 nnoremap B ^
652 nnoremap E $
653 nnoremap <C-N>  :cn<CR>zzzv
654 nnoremap <C-P>  :cp<CR>zzzv
655 "http://dougblack.io/words/a-good-vimrc.html
656 "nnoremap gV `[v`]
657
658 "nnoremap <silent> <C-]> :let word=expand("<cword>")<CR>:vsp<CR>:wincmd w<cr>:exec("tag ". word)<cr>
659 "nnoremap <silent> <C-]> :let word=expand("<cword>")<CR>:wincmd o<cr>:vsp<CR>:exec("tag ". word)<cr>zz:wincmd w<cr>
660 " command in normal mode, and visual mode
661 " yiw  = viwy
662 " diw  = viwd
663 " gn   select search and in visual mode
664 " confirm when using :q to quit the last tab page,
665 "Alias q if\ winnr('$')>1||tabpagenr('$')>1||confirm('Really\ quit?',\ "&OK\\n&Cancel")==1|quit|endif
666
667
668
669 " color of current line
670 "let s:marklist = ''
671 nnoremap <Leader>m  :marks abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<CR>:normal '
672
673 "let g:ctrlp_extensions = ['tag', 'buffertag', 'quickfix', 'dir', 'rtscript',
674 "                          \ 'undo', 'line', 'changes', 'mixed', 'bookmarkdir']
675
676