diff options
Diffstat (limited to 'vim/init/keymaps.vim')
-rw-r--r-- | vim/init/keymaps.vim | 115 |
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> | |||
21 | function! s:WriteOrEnterFileName() | 21 | function! s:WriteOrEnterFileName() |
22 | if !empty(expand('%')) | write! | else | call feedkeys(":w ") | endif | 22 | if !empty(expand('%')) | write! | else | call feedkeys(":w ") | endif |
23 | endfunction | 23 | endfunction |
24 | nmap <leader>w :call <SID>WriteOrEnterFileName()<CR> | 24 | nnoremap <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) |
28 | command! W execute 'w !sudo -S tee %' <bar> edit! | 28 | command! W execute 'w !sudo -S tee %' <bar> edit! |
29 | 29 | ||
30 | " Quit | 30 | " Quit |
31 | nmap <leader>q :q<CR> | 31 | nnoremap <leader>q :q<CR> |
32 | nmap cq :cq<CR> | 32 | nnoremap cq :cq<CR> |
33 | 33 | ||
34 | " Remap <CR> in Quickfix, Cmdwin Location list | 34 | " Remap <CR> in Quickfix, Cmdwin Location list |
35 | augroup vimrc_CRfix | 35 | augroup 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 |
60 | nnoremap <leader><leader>sp :set spell!<CR>:set spell?<CR> | 60 | nnoremap \s :set spell!<CR>:set spell?<CR> |
61 | nnoremap <leader>ss ]s | 61 | nnoremap <leader>ss ]s |
62 | nnoremap <leader>S [s | 62 | nnoremap <leader>S [s |
63 | 63 | ||
@@ -68,6 +68,7 @@ nnoremap <C-g> 1<C-g> | |||
68 | vnoremap Tz :!trans -t zh-TW -b<CR> | 68 | vnoremap Tz :!trans -t zh-TW -b<CR> |
69 | vnoremap Te :!trans -t en-US -b<CR> | 69 | vnoremap 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 | ||
83 | nnoremap cd<space> :cd<space> | 84 | nnoremap cd<space> :cd<space> |
84 | nnoremap cdg :call CdToGitRepo()<CR>:pwd<CR> | 85 | nnoremap cdg :call CdToGitRepo()<CR>:pwd<CR> |
85 | noremap <C-[> :cd ..<CR>:pwd<CR> | 86 | nnoremap <C-[> :cd ..<CR>:pwd<CR> |
86 | noremap <C-]> :call InCaseCdToLatestDir()<CR> | 87 | nnoremap <C-]> :call InCaseCdToLatestDir()<CR> |
87 | 88 | ||
88 | " Switch CWD to root git directory | 89 | " Switch CWD to root git directory |
89 | function! CdToGitRepo() | 90 | function! 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) |
108 | noremap <silent> <expr> j (v:count == 0 ? 'gj' : 'j') | 109 | nnoremap <silent> <expr> j (v:count == 0 ? 'gj' : 'j') |
109 | noremap <silent> <expr> k (v:count == 0 ? 'gk' : 'k') | 110 | nnoremap <silent> <expr> k (v:count == 0 ? 'gk' : 'k') |
110 | 111 | ||
111 | " Quick move in a line | 112 | " Quick move in a line |
112 | noremap <C-h> 30h | 113 | nnoremap <C-h> 30h |
113 | noremap <C-l> 30l | 114 | nnoremap <C-l> 30l |
114 | 115 | ||
115 | " File under the cursor | 116 | " File under the cursor |
116 | nnoremap <CR> gf | 117 | nnoremap <CR> gf |
117 | nnoremap gF :e <cfile><CR> | 118 | nnoremap gF :e <cfile><CR> |
118 | 119 | ||
120 | xnoremap iq i" | ||
121 | xnoremap aq a" | ||
122 | |||
123 | |||
119 | " READLINE_FEATURES ----------------{{{ | 124 | " READLINE_FEATURES ----------------{{{ |
120 | 125 | ||
121 | inoremap <C-f> <Right> | 126 | inoremap <C-f> <Right> |
@@ -145,22 +150,22 @@ cnoremap <C-k> <C-x>d$<C-c> | |||
145 | cnoremap <M-d> <C-x>de<C-c> | 150 | cnoremap <M-d> <C-x>de<C-c> |
146 | 151 | ||
147 | " Moving with wrap | 152 | " Moving with wrap |
148 | noremap <m-j> gj | 153 | nnoremap <m-j> gj |
149 | noremap <m-k> gk | 154 | nnoremap <m-k> gk |
150 | inoremap <m-j> <c-\><c-o>gj | 155 | inoremap <m-j> <c-\><c-o>gj |
151 | inoremap <m-k> <c-\><c-o>gk | 156 | inoremap <m-k> <c-\><c-o>gk |
152 | " }}} | 157 | " }}} |
153 | " JUMP_TO_TABS_WITH_ALT ----------------{{{ | 158 | " JUMP_TO_TABS_WITH_ALT ----------------{{{ |
154 | 159 | ||
155 | noremap <silent><A-1> :tabn 1<CR> | 160 | nnoremap <silent><A-1> :tabn 1<CR> |
156 | noremap <silent><A-2> :tabn 2<CR> | 161 | nnoremap <silent><A-2> :tabn 2<CR> |
157 | noremap <silent><M-3> :tabn 3<CR> | 162 | nnoremap <silent><M-3> :tabn 3<CR> |
158 | noremap <silent><M-4> :tabn 4<CR> | 163 | nnoremap <silent><M-4> :tabn 4<CR> |
159 | noremap <silent><M-5> :tabn 5<CR> | 164 | nnoremap <silent><M-5> :tabn 5<CR> |
160 | noremap <silent><M-6> :tabn 6<CR> | 165 | nnoremap <silent><M-6> :tabn 6<CR> |
161 | noremap <silent><M-7> :tabn 7<CR> | 166 | nnoremap <silent><M-7> :tabn 7<CR> |
162 | noremap <silent><M-8> :tabn 8<CR> | 167 | nnoremap <silent><M-8> :tabn 8<CR> |
163 | noremap <silent><M-9> :tablast<CR> | 168 | nnoremap <silent><M-9> :tablast<CR> |
164 | inoremap <silent><A-1> <Esc>:tabn 1<CR> | 169 | inoremap <silent><A-1> <Esc>:tabn 1<CR> |
165 | inoremap <silent><A-2> <Esc>:tabn 2<CR> | 170 | inoremap <silent><A-2> <Esc>:tabn 2<CR> |
166 | inoremap <silent><M-3> <Esc>:tabn 3<CR> | 171 | inoremap <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 |
214 | noremap st :set<space> | 219 | nnoremap so :set<space> |
215 | noremap <leader><leader>ft :<C-\>e'set filetype='..&filetype<CR> | 220 | nnoremap <leader><leader>ft :<C-\>e'set filetype='..&filetype<CR> |
216 | noremap <leader><leader>li :set list!<CR> | 221 | nnoremap <leader><leader>sw :<C-\>e'set shiftwidth='..&shiftwidth<CR> |
217 | noremap <leader><leader>sw :<C-\>e'set shiftwidth='..&shiftwidth<CR> | 222 | nnoremap <leader><leader>ts :<C-\>e'set tabstop='..&tabstop<CR> |
218 | noremap <leader><leader>nu :set number!<CR> | 223 | nnoremap \e :set expandtab!<CR>:set expandtab?<CR> |
219 | noremap <leader><leader>ru :set relativenumber!<CR> | 224 | nnoremap \l :set list!<CR>:set list?<CR> |
225 | nnoremap \n :set nu!<CR>:set nu?<CR> | ||
226 | nnoremap \r :set relativenumber!<CR>:set rnu?<CR> | ||
220 | 227 | ||
221 | " Open a new buffer | 228 | " Open a new buffer |
222 | nmap <leader>B :enew<CR> | 229 | nnoremap <leader>B :enew<CR> |
223 | nmap <leader>O :e /tmp/buffer<CR> | 230 | nnoremap <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 |
226 | augroup SaveLastBuffer | 233 | augroup 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 |
229 | augroup END | 236 | augroup END |
230 | noremap <leader>l :exe "buffer ".g:lastbuffer<CR> | 237 | nnoremap <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 |
240 | endfunction | 247 | endfunction |
241 | nnoremap <leader><leader>gl :call ToggleQuit()<CR> | 248 | nnoremap \q :call ToggleQuit()<CR> |
242 | 249 | ||
243 | function! CloseBufferSafely() | 250 | function! 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 |
257 | endfunction | 265 | endfunction |
258 | func! QuitWithCheck() | 266 | func! 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 |
264 | endfunc | 272 | endfunc |
265 | function! Bye() | 273 | function! 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() | |||
282 | endfunction | 286 | endfunction |
283 | nnoremap <silent> <C-c> :call Bye()<CR> | 287 | nnoremap <silent> <C-c> :call Bye()<CR> |
284 | 288 | ||
285 | 289 | " }}} | |
286 | " Diff Mode ---------------- | 290 | " Diff Mode {{{ |
287 | 291 | ||
288 | function! CloseBuffersForDiff() | 292 | function! 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 | "---------------------------------------------------------------------- |
332 | noremap <m-H> <c-w>h | 336 | nnoremap <m-H> <c-w>h |
333 | noremap <m-L> <c-w>l | 337 | nnoremap <m-L> <c-w>l |
334 | noremap <m-J> <c-w>j | 338 | nnoremap <m-J> <c-w>j |
335 | noremap <m-K> <c-w>k | 339 | nnoremap <m-K> <c-w>k |
336 | inoremap <m-H> <esc><c-w>h | 340 | inoremap <m-H> <esc><c-w>h |
337 | inoremap <m-L> <esc><c-w>l | 341 | inoremap <m-L> <esc><c-w>l |
338 | inoremap <m-J> <esc><c-w>j | 342 | inoremap <m-J> <esc><c-w>j |
@@ -365,12 +369,12 @@ map <leader>tc :tabclose<CR> | |||
365 | map <leader>tm :tabmove<SPACE> | 369 | map <leader>tm :tabmove<SPACE> |
366 | map <leader>to :tabonly<CR> | 370 | map <leader>to :tabonly<CR> |
367 | 371 | ||
368 | noremap <silent><m-h> :call Tab_MoveLeft()<CR> | 372 | nnoremap <silent><m-h> :call Tab_MoveLeft()<CR> |
369 | noremap <silent><m-l> :call Tab_MoveRight()<CR> | 373 | nnoremap <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 |
372 | let g:lasttab = 1 | 376 | let g:lasttab = 1 |
373 | nmap <Leader>tl :exe "tabn ".g:lasttab<CR> | 377 | nnoremap <Leader>tl :exe "tabn ".g:lasttab<CR> |
374 | autocmd TabLeave * let g:lasttab = tabpagenr() | 378 | autocmd 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 |
397 | noremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR> | 401 | nnoremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR> |
398 | noremap <leader><leader>fc :<C-\>e'set foldcolumn='..&foldcolumn<CR> | 402 | nnoremap <leader><leader>fc :<C-\>e'set foldcolumn='..&foldcolumn<CR> |
399 | 403 | ||
400 | nnoremap zi zizz | 404 | nnoremap 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 |
466 | noremap <silent> <leader><CR> :noh<CR> | 470 | nnoremap <silent> <leader><CR> :noh<CR> |
467 | 471 | ||
468 | function! HiFile() | 472 | function! HiFile() |
469 | let i = 1 | 473 | let i = 1 |
@@ -501,7 +505,6 @@ inoremap ( ()<Left> | |||
501 | inoremap [ []<Left> | 505 | inoremap [ []<Left> |
502 | inoremap { {}<Left> | 506 | inoremap { {}<Left> |
503 | 507 | ||
504 | vnoremap S sa | ||
505 | vnoremap ' <ESC>`<i'<ESC>`>la'<ESC> | 508 | vnoremap ' <ESC>`<i'<ESC>`>la'<ESC> |
506 | vnoremap q <ESC>`<i"<ESC>`>la"<ESC> | 509 | vnoremap q <ESC>`<i"<ESC>`>la"<ESC> |
507 | vnoremap ( <ESC>`<i(<ESC>`>la)<ESC> | 510 | vnoremap ( <ESC>`<i(<ESC>`>la)<ESC> |
@@ -591,6 +594,8 @@ vnoremap <CR> <Cmd>call SubstituteBySearch()<CR> | |||
591 | " }}} | 594 | " }}} |
592 | " SIGN ----------------{{{ | 595 | " SIGN ----------------{{{ |
593 | 596 | ||
597 | nnoremap <leader><leader>sc :<C-\>e'set signcolumn='..&signcolumn<CR> | ||
598 | |||
594 | nnoremap <leader>si :exe ":sign place " .. line('.') .. " line=" .. line('.') .. " name=piet file=" .. expand("%:p")<CR> | 599 | nnoremap <leader>si :exe ":sign place " .. line('.') .. " line=" .. line('.') .. " name=piet file=" .. expand("%:p")<CR> |
595 | nnoremap <leader>sI :exe ":sign unplace * file=" .. expand("%:p")<CR> | 600 | nnoremap <leader>sI :exe ":sign unplace * file=" .. expand("%:p")<CR> |
596 | 601 | ||