aboutsummaryrefslogtreecommitdiffhomepage
path: root/vim/init/keymaps.vim
diff options
context:
space:
mode:
Diffstat (limited to 'vim/init/keymaps.vim')
-rw-r--r--vim/init/keymaps.vim188
1 files changed, 124 insertions, 64 deletions
diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim
index f71ec7c..0132706 100644
--- a/vim/init/keymaps.vim
+++ b/vim/init/keymaps.vim
@@ -7,6 +7,9 @@
7" Space for searching 7" Space for searching
8map <space> / 8map <space> /
9 9
10" Escape normal mode by <C-c>
11imap <C-c> <Esc>l
12
10" Search for selected test 13" Search for selected test
11vnoremap * y/\V<C-R>=escape(@",'/\')<CR><CR> 14vnoremap * y/\V<C-R>=escape(@",'/\')<CR><CR>
12 15
@@ -23,10 +26,8 @@ nmap <leader>w :w!<CR>
23" (useful for handling the permission-denied error) 26" (useful for handling the permission-denied error)
24command! W execute 'w !sudo -S tee %' <bar> edit! 27command! W execute 'w !sudo -S tee %' <bar> edit!
25 28
26" Fast quit with error 29" Quit
27nmap <leader>q :q<CR> 30nmap <leader>q :q<CR>
28
29" Fast quit with error
30nmap cq :cq<CR> 31nmap cq :cq<CR>
31 32
32" Remap <CR> in Quickfix, Cmdwin Location list 33" Remap <CR> in Quickfix, Cmdwin Location list
@@ -88,7 +89,8 @@ nnoremap <C-k> ddkP
88" execute "set <M-h>=\eh" 89" execute "set <M-h>=\eh"
89 90
90" Spell 91" Spell
91nnoremap <leader><leader>sp :set spell!<CR> 92nnoremap <expr> <leader><leader>sp ": echo spell "..(echo &spell ? "on" : "off").."<CR>"
93nnoremap <expr> <leader><leader>sp ": echo spell "
92nnoremap <leader>ss ]s 94nnoremap <leader>ss ]s
93nnoremap <leader>S [s 95nnoremap <leader>S [s
94 96
@@ -190,6 +192,10 @@ nnoremap <leader>ee :edit $MYVIMRC<CR>
190" Set options 192" Set options
191noremap <leader>st :set<space> 193noremap <leader>st :set<space>
192noremap <leader><leader>ft :<C-\>e'set filetype='..&filetype<CR> 194noremap <leader><leader>ft :<C-\>e'set filetype='..&filetype<CR>
195noremap <leader><leader>li :set list!<CR>
196noremap <leader><leader>sw :<C-\>e'set shiftwidth='..&shiftwidth<CR>
197noremap <leader><leader>nu :set number!<CR>
198noremap <leader><leader>ru :set relativenumber!<CR>
193 199
194" Open a new buffer 200" Open a new buffer
195nmap <leader>B :enew<CR> 201nmap <leader>B :enew<CR>
@@ -213,43 +219,53 @@ function! ToggleQuit()
213endfunction 219endfunction
214nnoremap <leader><leader>gl :call ToggleQuit()<CR> 220nnoremap <leader><leader>gl :call ToggleQuit()<CR>
215 221
216function! CheckSave() 222function! CloseBufferSafely()
217 if &modified 223 if &modified
218 let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel") 224 let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel")
219 if answer == 1 | write | endif 225 if answer == 1 | write | endif
220 if answer == 3 | return | endif 226 if answer == 3 | return | endif
221 endif 227 endif
222 228
223 bdelete! 229 let bufs = getbufinfo({'buflisted': 1})
224 230 if len(bufs) == 1
225endfunction 231 bdelete!
226func! QuitWithCheck()
227 if g:quitVimWhenPressingCtrlC
228 silent! quit
229 else 232 else
230 echo "Press <leader><leader>gl to allow quit with <C-c>" 233 b# | bd! #
231 endif 234 endif
235endfunction
236func! QuitWithCheck()
237if g:quitVimWhenPressingCtrlC
238 silent! quit
239else
240 echo "Press <leader><leader>gl to allow quit with <C-c>"
241endif
232endfunc 242endfunc
233function! Bye() 243function! Bye()
234 let windows = gettabinfo(tabpagenr())[0]['windows'] 244let windows = gettabinfo(tabpagenr())[0]['windows']
245try
235 let bufs = gettabinfo(tabpagenr())[0]['variables']['bufs'] 246 let bufs = gettabinfo(tabpagenr())[0]['variables']['bufs']
236 247catch
237 if len(windows) == 1 && len(bufs) == 1 248 let bufs = getbufinfo({'buflisted': 1})
238 call QuitWithCheck() 249endtry
239 elseif &diff 250
240 call CloseBuffersForDiff() 251if len(windows) == 1 && len(bufs) == 1
241 else 252 call QuitWithCheck()
242 call CheckSave() 253elseif &diff
243 endif 254 call CloseBuffersForDiff()
255elseif len(windows) >1
256 quit
257else
258 silent! call CloseBufferSafely()
259endif
244endfunction 260endfunction
245nnoremap <silent> <C-c> :call Bye()<CR> 261nnoremap <silent> <C-c> :call Bye()<CR>
246 262
247"}}} 263
248" Diff Mode ----------------{{{ 264" Diff Mode ----------------
249 265
250function! CloseBuffersForDiff() 266function! CloseBuffersForDiff()
251 windo | if &diff && &buftype == "nofile" | bdelete | endif 267 windo | if &diff && &buftype == "nofile" | bdelete | endif
252 norm! zv 268 norm! zv
253endfunction 269endfunction
254 270
255command! DiffOrig vert new | set buftype=nofile nobuflisted | read ++edit # | 0d_ 271command! DiffOrig vert new | set buftype=nofile nobuflisted | read ++edit # | 0d_
@@ -257,25 +273,25 @@ command! DiffOrig vert new | set buftype=nofile nobuflisted | read ++edit # | 0d
257 273
258" Uset <C-w>d to toggle Diff mode 274" Uset <C-w>d to toggle Diff mode
259function! s:SwitchDiff() 275function! s:SwitchDiff()
260 if &diff 276if &diff
261 call CloseBuffersForDiff() 277 call CloseBuffersForDiff()
262 else 278else
263 DiffOrig 279 DiffOrig
264 endif 280endif
265endfunction 281endfunction
266com! SwitchDiff call s:SwitchDiff() 282com! SwitchDiff call s:SwitchDiff()
267nnoremap <C-w>d <Cmd>silent! SwitchDiff<CR> 283nnoremap <C-w>d <Cmd>silent! SwitchDiff<CR>
268 284
269function! s:SwitchDiffForGitHEAD() 285function! s:SwitchDiffForGitHEAD()
270 norm cdg 286norm cdg
271 if &diff 287if &diff
272 windo | if &buftype == "nofile" | bdelete | endif 288 windo | if &buftype == "nofile" | bdelete | endif
273 norm! zv 289 norm! zv
274 else 290else
275 vert new | set buftype=nofile nobuflisted 291 vert new | set buftype=nofile nobuflisted
276 read !git show HEAD:# 292 read !git show HEAD:#
277 0d_ | diffthis | wincmd p | diffthis 293 0d_ | diffthis | wincmd p | diffthis
278 endif 294endif
279endfunction 295endfunction
280command! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD() 296command! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD()
281nnoremap <C-w>D <Cmd>silent! SwitchDiffForGitHEAD<CR> 297nnoremap <C-w>D <Cmd>silent! SwitchDiffForGitHEAD<CR>
@@ -301,22 +317,22 @@ inoremap <m-J> <esc><c-w>j
301inoremap <m-K> <esc><c-w>k 317inoremap <m-K> <esc><c-w>k
302 318
303if has('terminal') && exists(':terminal') == 2 && has('patch-8.1.1') 319if has('terminal') && exists(':terminal') == 2 && has('patch-8.1.1')
304 " vim 8.1 支持 termwinkey ,不需要把 terminal 切换成 normal 模式 320" vim 8.1 支持 termwinkey ,不需要把 terminal 切换成 normal 模式
305 " 设置 termwinkey 为 CTRL 加减号(GVIM),有些终端下是 CTRL+? 321" 设置 termwinkey 为 CTRL 加减号(GVIM),有些终端下是 CTRL+?
306 " 后面四个键位是搭配 termwinkey 的,如果 termwinkey 更改,也要改 322" 后面四个键位是搭配 termwinkey 的,如果 termwinkey 更改,也要改
307 set termwinkey=<c-_> 323set termwinkey=<c-_>
308 tnoremap <m-H> <c-_>h 324tnoremap <m-H> <c-_>h
309 tnoremap <m-L> <c-_>l 325tnoremap <m-L> <c-_>l
310 tnoremap <m-J> <c-_>j 326tnoremap <m-J> <c-_>j
311 tnoremap <m-K> <c-_>k 327tnoremap <m-K> <c-_>k
312 tnoremap <m-q> <c-\><c-n> 328tnoremap <m-q> <c-\><c-n>
313elseif has('nvim') 329elseif has('nvim')
314 " neovim 没有 termwinkey 支持,必须把 terminal 切换回 normal 模式 330" neovim 没有 termwinkey 支持,必须把 terminal 切换回 normal 模式
315 tnoremap <m-H> <c-\><c-n><c-w>h 331tnoremap <m-H> <c-\><c-n><c-w>h
316 tnoremap <m-L> <c-\><c-n><c-w>l 332tnoremap <m-L> <c-\><c-n><c-w>l
317 tnoremap <m-J> <c-\><c-n><c-w>j 333tnoremap <m-J> <c-\><c-n><c-w>j
318 tnoremap <m-K> <c-\><c-n><c-w>k 334tnoremap <m-K> <c-\><c-n><c-w>k
319 tnoremap <m-q> <c-\><c-n> 335tnoremap <m-q> <c-\><c-n>
320endif 336endif
321" }}} 337" }}}
322" MANAGE_TABS ----------------{{{ 338" MANAGE_TABS ----------------{{{
@@ -341,35 +357,44 @@ map <leader>te :tabedit <C-r>=expand("%:p:h")<CR>
341 357
342" Tab move functions 358" Tab move functions
343function! Tvab_MoveLeft() 359function! Tvab_MoveLeft()
344 let l:tabnr = tabpagenr() - 2 360let l:tabnr = tabpagenr() - 2
345 if l:tabnr >= 0 361if l:tabnr >= 0
346 exec 'tabmove '.l:tabnr 362 exec 'tabmove '.l:tabnr
347 endif 363endif
348endfunc 364endfunc
349function! Tab_MoveRight() 365function! Tab_MoveRight()
350 let l:tabnr = tabpagenr() + 1 366let l:tabnr = tabpagenr() + 1
351 if l:tabnr <= tabpagenr('$') 367if l:tabnr <= tabpagenr('$')
352 exec 'tabmove '.l:tabnr 368 exec 'tabmove '.l:tabnr
353 endif 369endif
354endfunc 370endfunc
355" }}} 371" }}}
356" FOLD ----------------{{{ 372" FOLD ----------------{{{
357 373
358" Set foldmethod 374" Set foldmethod
359noremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR> 375noremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR>
376noremap <leader><leader>fc :<C-\>e'set foldcolumn='..&foldcolumn<CR>
360 377
361" Show fold level when it changes 378" Show fold level when it changes
362nnoremap zm zm:set foldlevel<CR> 379nnoremap zm zm:set foldlevel<CR>
363nnoremap zr zr:set foldlevel<CR> 380nnoremap zr zr:set foldlevel<CR>
364 381
365" Fold file except selection 382" Fold file except selection
383let g:original_foldmethod = ""
366function! UnfoldSelectionOnly() 384function! UnfoldSelectionOnly()
367 set foldmethod=manual 385 echo 'Unfold'..&foldmethod
386 let g:original_foldmethod = &foldmethod
387 let &foldmethod = "manual"
368 norm! zE 388 norm! zE
369 execute "0,'<-1fold" 389 execute "0,'<-1fold"
370 execute "'>+1,$fold" 390 execute "'>+1,$fold"
371endfunction 391endfunction
372vnoremap zF :call UnfoldSelectionOnly()<CR> 392function! ResumeFoldmethod()
393 norm! zE
394 let &foldmethod = g:original_foldmethod ? g:original_foldmethod : "indent"
395endfunc
396vnoremap zF :<C-u>call UnfoldSelectionOnly()<CR>
397nnoremap zF :<C-u>call ResumeFoldmethod()<CR>
373 398
374" Use l to open fold 399" Use l to open fold
375nnoremap <expr> l foldclosed('.') == -1 ? 'l' : 'zo' 400nnoremap <expr> l foldclosed('.') == -1 ? 'l' : 'zo'
@@ -379,6 +404,36 @@ nnoremap <expr> zo foldclosed('.') == -1 ? 'zjzo' : 'zo'
379nnoremap <expr> zO foldclosed('.') == -1 ? 'zjzO' : 'zO' 404nnoremap <expr> zO foldclosed('.') == -1 ? 'zjzO' : 'zO'
380 405
381" }}} 406" }}}
407" HIGHLIGHT ----------------{{{
408
409function! HiFile()
410 let i = 1
411 while i <= line("$")
412 if strlen(getline(i)) > 0 && len(split(getline(i))) > 2
413 let w = split(getline(i))[0]
414 let l:command = "syn match " . w . " /^" . w . "\\s\\+xxx/"
415 exe l:command
416 endif
417 let i += 1
418 endwhile
419endfunction
420
421function! GetHighlightGroupName()
422 let l:syntaxID = synID(line('.'), col('.'), 1)
423 let l:groupName = synIDattr(l:syntaxID, 'name')
424 echo "Highlight Group Name: " . l:groupName
425endfunction
426nnoremap <leader>H :call GetHighlightGroupName()<CR>
427
428" Persist visualized lines
429" define line highlight color
430highlight MultiLineHighlight ctermbg=LightYellow guibg=LightYellow ctermfg=Black guifg=Black
431" highlight the current line
432nnoremap <silent> <leader>gh :call matchadd('MultiLineHighlight', '\%'.line('.').'l')<CR>
433" clear all the highlighted lines
434nnoremap <silent> <leader>gH :call clearmatches()<CR>
435
436" }}}
382" SURROUND ----------------{{{ 437" SURROUND ----------------{{{
383 438
384inoremap ' ''<Left> 439inoremap ' ''<Left>
@@ -398,6 +453,11 @@ vnoremap <space> <ESC>`<i<space><ESC>`>la<space><ESC>
398vnoremap Q <ESC>`<i「<ESC>`>la」<ESC> 453vnoremap Q <ESC>`<i「<ESC>`>la」<ESC>
399 454
400" }}} 455" }}}
456" QUICKFIX ----------------{{{
457nnoremap <leader>cn :cn<CR>
458nnoremap <leader>cp :cp<CR>
459nnoremap <leader>cw :cw 10<CR>
460" }}}
401" REDIRECTION_WITH_BUFFER ----------------{{{ 461" REDIRECTION_WITH_BUFFER ----------------{{{
402 462
403" Usage: 463" Usage: