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.vim115
1 files changed, 60 insertions, 55 deletions
diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim
index f13756b..1eb2b20 100644
--- a/vim/init/keymaps.vim
+++ b/vim/init/keymaps.vim
@@ -21,15 +21,15 @@ nnoremap <leader>W :set wrap!<CR>
21function! s:WriteOrEnterFileName() 21function! s:WriteOrEnterFileName()
22 if !empty(expand('%')) | write! | else | call feedkeys(":w ") | endif 22 if !empty(expand('%')) | write! | else | call feedkeys(":w ") | endif
23endfunction 23endfunction
24nmap <leader>w :call <SID>WriteOrEnterFileName()<CR> 24nnoremap <leader>w :call <SID>WriteOrEnterFileName()<CR>
25 25
26" :W sudo saves the file 26" :W sudo saves the file
27" (useful for handling the permission-denied error) 27" (useful for handling the permission-denied error)
28command! W execute 'w !sudo -S tee %' <bar> edit! 28command! W execute 'w !sudo -S tee %' <bar> edit!
29 29
30" Quit 30" Quit
31nmap <leader>q :q<CR> 31nnoremap <leader>q :q<CR>
32nmap cq :cq<CR> 32nnoremap cq :cq<CR>
33 33
34" Remap <CR> in Quickfix, Cmdwin Location list 34" Remap <CR> in Quickfix, Cmdwin Location list
35augroup vimrc_CRfix 35augroup vimrc_CRfix
@@ -57,7 +57,7 @@ augroup END
57" execute "set <M-h>=\eh" 57" execute "set <M-h>=\eh"
58 58
59" Spell 59" Spell
60nnoremap <leader><leader>sp :set spell!<CR>:set spell?<CR> 60nnoremap \s :set spell!<CR>:set spell?<CR>
61nnoremap <leader>ss ]s 61nnoremap <leader>ss ]s
62nnoremap <leader>S [s 62nnoremap <leader>S [s
63 63
@@ -68,6 +68,7 @@ nnoremap <C-g> 1<C-g>
68vnoremap Tz :!trans -t zh-TW -b<CR> 68vnoremap Tz :!trans -t zh-TW -b<CR>
69vnoremap Te :!trans -t en-US -b<CR> 69vnoremap Te :!trans -t en-US -b<CR>
70 70
71
71" }}} 72" }}}
72" WORKING_DIR ----------------{{{ 73" WORKING_DIR ----------------{{{
73 74
@@ -82,8 +83,8 @@ nnoremap cd :cd %:p:h<CR>:pwd<CR>
82 83
83nnoremap cd<space> :cd<space> 84nnoremap cd<space> :cd<space>
84nnoremap cdg :call CdToGitRepo()<CR>:pwd<CR> 85nnoremap cdg :call CdToGitRepo()<CR>:pwd<CR>
85noremap <C-[> :cd ..<CR>:pwd<CR> 86nnoremap <C-[> :cd ..<CR>:pwd<CR>
86noremap <C-]> :call InCaseCdToLatestDir()<CR> 87nnoremap <C-]> :call InCaseCdToLatestDir()<CR>
87 88
88" Switch CWD to root git directory 89" Switch CWD to root git directory
89function! CdToGitRepo() 90function! CdToGitRepo()
@@ -105,17 +106,21 @@ endfunction
105" MOTION ----------------{{{ 106" MOTION ----------------{{{
106 107
107" j/k will move virtual lines (lines that wrap) 108" j/k will move virtual lines (lines that wrap)
108noremap <silent> <expr> j (v:count == 0 ? 'gj' : 'j') 109nnoremap <silent> <expr> j (v:count == 0 ? 'gj' : 'j')
109noremap <silent> <expr> k (v:count == 0 ? 'gk' : 'k') 110nnoremap <silent> <expr> k (v:count == 0 ? 'gk' : 'k')
110 111
111" Quick move in a line 112" Quick move in a line
112noremap <C-h> 30h 113nnoremap <C-h> 30h
113noremap <C-l> 30l 114nnoremap <C-l> 30l
114 115
115" File under the cursor 116" File under the cursor
116nnoremap <CR> gf 117nnoremap <CR> gf
117nnoremap gF :e <cfile><CR> 118nnoremap gF :e <cfile><CR>
118 119
120xnoremap iq i"
121xnoremap aq a"
122
123
119" READLINE_FEATURES ----------------{{{ 124" READLINE_FEATURES ----------------{{{
120 125
121inoremap <C-f> <Right> 126inoremap <C-f> <Right>
@@ -145,22 +150,22 @@ cnoremap <C-k> <C-x>d$<C-c>
145cnoremap <M-d> <C-x>de<C-c> 150cnoremap <M-d> <C-x>de<C-c>
146 151
147" Moving with wrap 152" Moving with wrap
148noremap <m-j> gj 153nnoremap <m-j> gj
149noremap <m-k> gk 154nnoremap <m-k> gk
150inoremap <m-j> <c-\><c-o>gj 155inoremap <m-j> <c-\><c-o>gj
151inoremap <m-k> <c-\><c-o>gk 156inoremap <m-k> <c-\><c-o>gk
152" }}} 157" }}}
153" JUMP_TO_TABS_WITH_ALT ----------------{{{ 158" JUMP_TO_TABS_WITH_ALT ----------------{{{
154 159
155noremap <silent><A-1> :tabn 1<CR> 160nnoremap <silent><A-1> :tabn 1<CR>
156noremap <silent><A-2> :tabn 2<CR> 161nnoremap <silent><A-2> :tabn 2<CR>
157noremap <silent><M-3> :tabn 3<CR> 162nnoremap <silent><M-3> :tabn 3<CR>
158noremap <silent><M-4> :tabn 4<CR> 163nnoremap <silent><M-4> :tabn 4<CR>
159noremap <silent><M-5> :tabn 5<CR> 164nnoremap <silent><M-5> :tabn 5<CR>
160noremap <silent><M-6> :tabn 6<CR> 165nnoremap <silent><M-6> :tabn 6<CR>
161noremap <silent><M-7> :tabn 7<CR> 166nnoremap <silent><M-7> :tabn 7<CR>
162noremap <silent><M-8> :tabn 8<CR> 167nnoremap <silent><M-8> :tabn 8<CR>
163noremap <silent><M-9> :tablast<CR> 168nnoremap <silent><M-9> :tablast<CR>
164inoremap <silent><A-1> <Esc>:tabn 1<CR> 169inoremap <silent><A-1> <Esc>:tabn 1<CR>
165inoremap <silent><A-2> <Esc>:tabn 2<CR> 170inoremap <silent><A-2> <Esc>:tabn 2<CR>
166inoremap <silent><M-3> <Esc>:tabn 3<CR> 171inoremap <silent><M-3> <Esc>:tabn 3<CR>
@@ -211,23 +216,25 @@ nnoremap <leader>ee :edit $MYVIMRC<CR>
211" MANAGE_BUFFERS ----------------{{{ 216" MANAGE_BUFFERS ----------------{{{
212 217
213" Set options 218" Set options
214noremap st :set<space> 219nnoremap so :set<space>
215noremap <leader><leader>ft :<C-\>e'set filetype='..&filetype<CR> 220nnoremap <leader><leader>ft :<C-\>e'set filetype='..&filetype<CR>
216noremap <leader><leader>li :set list!<CR> 221nnoremap <leader><leader>sw :<C-\>e'set shiftwidth='..&shiftwidth<CR>
217noremap <leader><leader>sw :<C-\>e'set shiftwidth='..&shiftwidth<CR> 222nnoremap <leader><leader>ts :<C-\>e'set tabstop='..&tabstop<CR>
218noremap <leader><leader>nu :set number!<CR> 223nnoremap \e :set expandtab!<CR>:set expandtab?<CR>
219noremap <leader><leader>ru :set relativenumber!<CR> 224nnoremap \l :set list!<CR>:set list?<CR>
225nnoremap \n :set nu!<CR>:set nu?<CR>
226nnoremap \r :set relativenumber!<CR>:set rnu?<CR>
220 227
221" Open a new buffer 228" Open a new buffer
222nmap <leader>B :enew<CR> 229nnoremap <leader>B :enew<CR>
223nmap <leader>O :e /tmp/buffer<CR> 230nnoremap <leader>O :e /tmp/buffer<CR>
224 231
225" Let <leader>l toggle between this and the last accessed buffer 232" Let <leader>l toggle between this and the last accessed buffer
226augroup SaveLastBuffer 233augroup SaveLastBuffer
227 let g:lastbuffer = 1 234 let g:lastbuffer = 1
228 au BufLeave * let g:lastbuffer = bufnr() 235 au BufLeave * if &buflisted | let g:lastbuffer = expand('<abuf>') | endif
229augroup END 236augroup END
230noremap <leader>l :exe "buffer ".g:lastbuffer<CR> 237nnoremap <leader>l :exe "buffer ".g:lastbuffer<CR>
231 238
232" Use Ctrl-C for buffer delete or quit vim ----------------{{{ 239" Use Ctrl-C for buffer delete or quit vim ----------------{{{
233 240
@@ -238,9 +245,11 @@ function! ToggleQuit()
238 let message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock" 245 let message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock"
239 echo message 246 echo message
240endfunction 247endfunction
241nnoremap <leader><leader>gl :call ToggleQuit()<CR> 248nnoremap \q :call ToggleQuit()<CR>
242 249
243function! CloseBufferSafely() 250function! CloseBufferSafely()
251 let l:bufnr = bufnr()
252 " Ask Saving
244 if &modified 253 if &modified
245 let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel") 254 let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel")
246 if answer == 1 | write | endif 255 if answer == 1 | write | endif
@@ -248,27 +257,22 @@ function! CloseBufferSafely()
248 if answer == "" | return | endif 257 if answer == "" | return | endif
249 endif 258 endif
250 259
251 let bufs = getbufinfo({'buflisted': 1}) 260 if g:tab_group[tabpagenr()] == [l:bufnr]
252 if len(bufs) == 1 261 bdelete
253 bdelete!
254 else 262 else
255 b# | bd! # 263 bprevious | bd #
256 endif 264 endif
257endfunction 265endfunction
258func! QuitWithCheck() 266func! QuitWithCheck()
259 if g:quitVimWhenPressingCtrlC 267 if g:quitVimWhenPressingCtrlC
260 silent! quit 268 silent! quit
261 else 269 else
262 echo "Press <leader><leader>gl to allow quit with <C-c>" 270 echo "Press \\q to allow quit with <C-c>"
263 endif 271 endif
264endfunc 272endfunc
265function! Bye() 273function! Bye()
266 let windows = gettabinfo(tabpagenr())[0]['windows'] 274 let windows = gettabinfo(tabpagenr())[0]['windows']
267 try 275 let bufs = getbufinfo({'buflisted': 1})
268 let bufs = gettabinfo(tabpagenr())[0]['variables']['bufs']
269 catch
270 let bufs = getbufinfo({'buflisted': 1})
271 endtry
272 276
273 if len(windows) == 1 && len(bufs) == 1 277 if len(windows) == 1 && len(bufs) == 1
274 call QuitWithCheck() 278 call QuitWithCheck()
@@ -282,8 +286,8 @@ function! Bye()
282endfunction 286endfunction
283nnoremap <silent> <C-c> :call Bye()<CR> 287nnoremap <silent> <C-c> :call Bye()<CR>
284 288
285 289" }}}
286" Diff Mode ---------------- 290" Diff Mode {{{
287 291
288function! CloseBuffersForDiff() 292function! CloseBuffersForDiff()
289 windo | if &diff && &buftype == "nofile" | bdelete | endif 293 windo | if &diff && &buftype == "nofile" | bdelete | endif
@@ -329,10 +333,10 @@ nnoremap <leader><leader>sb :windo set scrollbind!<CR>
329" 传统的 CTRL+hjkl 移动窗口不适用于 vim 8.1 的终端模式,CTRL+hjkl 在 333" 传统的 CTRL+hjkl 移动窗口不适用于 vim 8.1 的终端模式,CTRL+hjkl 在
330" bash/zsh 及带文本界面的程序中都是重要键位需要保留,不能 tnoremap 的 334" bash/zsh 及带文本界面的程序中都是重要键位需要保留,不能 tnoremap 的
331"---------------------------------------------------------------------- 335"----------------------------------------------------------------------
332noremap <m-H> <c-w>h 336nnoremap <m-H> <c-w>h
333noremap <m-L> <c-w>l 337nnoremap <m-L> <c-w>l
334noremap <m-J> <c-w>j 338nnoremap <m-J> <c-w>j
335noremap <m-K> <c-w>k 339nnoremap <m-K> <c-w>k
336inoremap <m-H> <esc><c-w>h 340inoremap <m-H> <esc><c-w>h
337inoremap <m-L> <esc><c-w>l 341inoremap <m-L> <esc><c-w>l
338inoremap <m-J> <esc><c-w>j 342inoremap <m-J> <esc><c-w>j
@@ -365,12 +369,12 @@ map <leader>tc :tabclose<CR>
365map <leader>tm :tabmove<SPACE> 369map <leader>tm :tabmove<SPACE>
366map <leader>to :tabonly<CR> 370map <leader>to :tabonly<CR>
367 371
368noremap <silent><m-h> :call Tab_MoveLeft()<CR> 372nnoremap <silent><m-h> :call Tab_MoveLeft()<CR>
369noremap <silent><m-l> :call Tab_MoveRight()<CR> 373nnoremap <silent><m-l> :call Tab_MoveRight()<CR>
370 374
371" Let <leader>tl toggle between this and the last accessed tab 375" Let <leader>tl toggle between this and the last accessed tab
372let g:lasttab = 1 376let g:lasttab = 1
373nmap <Leader>tl :exe "tabn ".g:lasttab<CR> 377nnoremap <Leader>tl :exe "tabn ".g:lasttab<CR>
374autocmd TabLeave * let g:lasttab = tabpagenr() 378autocmd TabLeave * let g:lasttab = tabpagenr()
375 379
376" Opens a new tab with the current buffer's path 380" Opens a new tab with the current buffer's path
@@ -394,8 +398,8 @@ endfunc
394" FOLD ----------------{{{ 398" FOLD ----------------{{{
395 399
396" Set fold options 400" Set fold options
397noremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR> 401nnoremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR>
398noremap <leader><leader>fc :<C-\>e'set foldcolumn='..&foldcolumn<CR> 402nnoremap <leader><leader>fc :<C-\>e'set foldcolumn='..&foldcolumn<CR>
399 403
400nnoremap zi zizz 404nnoremap zi zizz
401 405
@@ -463,7 +467,7 @@ endfunction
463" HIGHLIGHT ----------------{{{ 467" HIGHLIGHT ----------------{{{
464 468
465" Disable highlight when <leader><CR> is pressed 469" Disable highlight when <leader><CR> is pressed
466noremap <silent> <leader><CR> :noh<CR> 470nnoremap <silent> <leader><CR> :noh<CR>
467 471
468function! HiFile() 472function! HiFile()
469 let i = 1 473 let i = 1
@@ -501,7 +505,6 @@ inoremap ( ()<Left>
501inoremap [ []<Left> 505inoremap [ []<Left>
502inoremap { {}<Left> 506inoremap { {}<Left>
503 507
504vnoremap S sa
505vnoremap ' <ESC>`<i'<ESC>`>la'<ESC> 508vnoremap ' <ESC>`<i'<ESC>`>la'<ESC>
506vnoremap q <ESC>`<i"<ESC>`>la"<ESC> 509vnoremap q <ESC>`<i"<ESC>`>la"<ESC>
507vnoremap ( <ESC>`<i(<ESC>`>la)<ESC> 510vnoremap ( <ESC>`<i(<ESC>`>la)<ESC>
@@ -591,6 +594,8 @@ vnoremap <CR> <Cmd>call SubstituteBySearch()<CR>
591" }}} 594" }}}
592" SIGN ----------------{{{ 595" SIGN ----------------{{{
593 596
597nnoremap <leader><leader>sc :<C-\>e'set signcolumn='..&signcolumn<CR>
598
594nnoremap <leader>si :exe ":sign place " .. line('.') .. " line=" .. line('.') .. " name=piet file=" .. expand("%:p")<CR> 599nnoremap <leader>si :exe ":sign place " .. line('.') .. " line=" .. line('.') .. " name=piet file=" .. expand("%:p")<CR>
595nnoremap <leader>sI :exe ":sign unplace * file=" .. expand("%:p")<CR> 600nnoremap <leader>sI :exe ":sign unplace * file=" .. expand("%:p")<CR>
596 601