diff options
Diffstat (limited to 'vim/init/keymaps.vim')
-rw-r--r-- | vim/init/keymaps.vim | 230 |
1 files changed, 143 insertions, 87 deletions
diff --git a/vim/init/keymaps.vim b/vim/init/keymaps.vim index f46e67b..38bdd0e 100644 --- a/vim/init/keymaps.vim +++ b/vim/init/keymaps.vim | |||
@@ -1,7 +1,6 @@ | |||
1 | "====================================================================== | 1 | "====================================================================== |
2 | " Only for key mapping | 2 | " Only for key mapping |
3 | "====================================================================== | 3 | "====================================================================== |
4 | " vim: set ts=4 sw=4 tw=78 noet : | ||
5 | 4 | ||
6 | " COMMON_MAPPING ----------------{{{ | 5 | " COMMON_MAPPING ----------------{{{ |
7 | 6 | ||
@@ -14,29 +13,22 @@ vnoremap * y/\V<C-R>=escape(@",'/\')<CR><CR> | |||
14 | " Disable highlight when <leader><CR> is pressed | 13 | " Disable highlight when <leader><CR> is pressed |
15 | map <silent> <leader><CR> :noh<CR> | 14 | map <silent> <leader><CR> :noh<CR> |
16 | 15 | ||
17 | " Paste register 0 | 16 | " Set wrap |
18 | nnoremap <C-p> "0p | 17 | nnoremap <leader>W :set wrap!<CR>:set wrap?<CR> |
19 | 18 | ||
20 | " Fast saving | 19 | " Fast saving |
21 | nmap <leader>w :w!<CR> | 20 | nmap <leader>w :w!<CR> |
22 | 21 | ||
22 | " :W sudo saves the file | ||
23 | " (useful for handling the permission-denied error) | ||
24 | command! W execute 'w !sudo -S tee %' <bar> edit! | ||
25 | |||
23 | " Fast quit with error | 26 | " Fast quit with error |
24 | nmap <leader>q :q<CR> | 27 | nmap <leader>q :q<CR> |
25 | 28 | ||
26 | " Fast quit with error | 29 | " Fast quit with error |
27 | nmap cq :cq<CR> | 30 | nmap cq :cq<CR> |
28 | 31 | ||
29 | " Switch wrap | ||
30 | nmap <leader>W :set wrap!<CR> | ||
31 | |||
32 | " :W sudo saves the file | ||
33 | " (useful for handling the permission-denied error) | ||
34 | command! W execute 'w !sudo -S tee %' <bar> edit! | ||
35 | |||
36 | " Enter to open file | ||
37 | nnoremap <CR> gf | ||
38 | nnoremap gF :e <cfile><CR> | ||
39 | |||
40 | " Remap <CR> in Quickfix, Cmdwin Location list | 32 | " Remap <CR> in Quickfix, Cmdwin Location list |
41 | augroup vimrc_CRfix | 33 | augroup vimrc_CRfix |
42 | au! | 34 | au! |
@@ -49,13 +41,25 @@ augroup END | |||
49 | " nnoremap <leader>, :terminal ++noclose<CR> | 41 | " nnoremap <leader>, :terminal ++noclose<CR> |
50 | vnoremap <leader>, :terminal<CR> | 42 | vnoremap <leader>, :terminal<CR> |
51 | 43 | ||
44 | " Paste register 0 | ||
45 | nnoremap <C-p> "0p | ||
46 | |||
52 | " Toggle paste mode on and off | 47 | " Toggle paste mode on and off |
53 | map <leader>pp :setlocal paste!<CR> | 48 | map <leader>pp :setlocal paste!<CR> |
54 | 49 | ||
50 | " Copy from system clipboard | ||
51 | nnoremap <leader>P :r !xsel -ob<CR> | ||
52 | vnoremap Y :w !xsel -ib<CR> | ||
53 | |||
55 | " Switch CWD to the directory of the open buffer | 54 | " Switch CWD to the directory of the open buffer |
56 | nnoremap cd :cd %:p:h<CR>:pwd<CR> | 55 | nnoremap cd :cd %:p:h<CR>:pwd<CR> |
57 | " Switch CWD to root git directory | 56 | " Switch CWD to root git directory |
58 | nnoremap cdg :execute 'cd' fnameescape(fnamemodify(finddir('.git', escape(expand('%:p:h'), ' ') . ';'), ':h'))<CR>:pwd<CR> | 57 | function! CdToGitRepo() |
58 | let l:git_dir = finddir('.git', escape(expand('%:p:h'), ' ') . ';') | ||
59 | let l:repo = fnameescape(fnamemodify(l:git_dir, ':h')) | ||
60 | execute "cd" l:repo | ||
61 | endfunction | ||
62 | nnoremap cdg :call CdToGitRepo()<CR>:pwd<CR> | ||
59 | 63 | ||
60 | " alias for cd | 64 | " alias for cd |
61 | nnoremap cdd :cd<space> | 65 | nnoremap cdd :cd<space> |
@@ -83,10 +87,6 @@ nnoremap <C-k> ddkP | |||
83 | " execute "set <M-l>=\el" | 87 | " execute "set <M-l>=\el" |
84 | " execute "set <M-h>=\eh" | 88 | " execute "set <M-h>=\eh" |
85 | 89 | ||
86 | " Copy from system clipboard | ||
87 | nnoremap <leader>P :r !xsel -ob<CR> | ||
88 | vnoremap Y :w !xsel -ib<CR> | ||
89 | |||
90 | " Spell | 90 | " Spell |
91 | nnoremap <leader><leader>sp :set spell!<CR> | 91 | nnoremap <leader><leader>sp :set spell!<CR> |
92 | nnoremap <leader>ss ]s | 92 | nnoremap <leader>ss ]s |
@@ -99,11 +99,6 @@ nnoremap <C-g> 1<C-g> | |||
99 | vnoremap Tz :!trans -t zh-TW -b<CR> | 99 | vnoremap Tz :!trans -t zh-TW -b<CR> |
100 | vnoremap Te :!trans -t en-US -b<CR> | 100 | vnoremap Te :!trans -t en-US -b<CR> |
101 | 101 | ||
102 | " source .vimrc | ||
103 | nnoremap <leader>so V:so<CR> | ||
104 | nnoremap <leader><leader>so :source ~/.vimrc<CR> | ||
105 | vnoremap so :source<CR> | ||
106 | |||
107 | " }}} | 102 | " }}} |
108 | " MOVE ----------------{{{ | 103 | " MOVE ----------------{{{ |
109 | 104 | ||
@@ -115,15 +110,11 @@ noremap <silent> <expr> k (v:count == 0 ? 'gk' : 'k') | |||
115 | noremap <C-h> 30h | 110 | noremap <C-h> 30h |
116 | noremap <C-l> 30l | 111 | noremap <C-l> 30l |
117 | 112 | ||
118 | " }}} | 113 | " File under the cursor |
119 | " MANAGE_VIMRC ----------------{{{ | 114 | nnoremap <CR> gf |
115 | nnoremap gF :e <cfile><CR> | ||
120 | 116 | ||
121 | nnoremap <leader>e :scriptnames<space> | 117 | " READLINE_FEATURES ----------------{{{ |
122 | nnoremap <leader>ee :edit $MYVIMRC<CR> | ||
123 | autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC | ||
124 | |||
125 | " }}} | ||
126 | " MOVING_WITH_READLINE ----------------{{{ | ||
127 | 118 | ||
128 | inoremap <C-f> <Right> | 119 | inoremap <C-f> <Right> |
129 | inoremap <C-b> <Left> | 120 | inoremap <C-b> <Left> |
@@ -157,15 +148,6 @@ noremap <m-k> gk | |||
157 | inoremap <m-j> <c-\><c-o>gj | 148 | inoremap <m-j> <c-\><c-o>gj |
158 | inoremap <m-k> <c-\><c-o>gk | 149 | inoremap <m-k> <c-\><c-o>gk |
159 | " }}} | 150 | " }}} |
160 | " INSERT_SURROUNDING ----------------{{{ | ||
161 | |||
162 | inoremap ' ''<Left> | ||
163 | inoremap " ""<Left> | ||
164 | inoremap ( ()<Left> | ||
165 | inoremap [ []<Left> | ||
166 | inoremap { {}<Left> | ||
167 | |||
168 | " }}} | ||
169 | " JUMP_TO_TABS_WITH_ALT ----------------{{{ | 151 | " JUMP_TO_TABS_WITH_ALT ----------------{{{ |
170 | 152 | ||
171 | noremap <silent><A-1> :tabn 1<CR> | 153 | noremap <silent><A-1> :tabn 1<CR> |
@@ -188,87 +170,122 @@ inoremap <silent><M-8> <Esc>:tabn 8<CR> | |||
188 | inoremap <silent><M-9> <Esc>:tablast<CR> | 170 | inoremap <silent><M-9> <Esc>:tablast<CR> |
189 | 171 | ||
190 | " }}} | 172 | " }}} |
191 | " MANAGE_TABS ----------------{{{ | ||
192 | 173 | ||
193 | " Useful mappings for managing tabs | 174 | " }}} |
194 | map <leader>tn :tabnew<CR> | 175 | " MANAGE_VIMRC ----------------{{{ |
195 | map <leader>tc :tabclose<CR> | ||
196 | map <leader>tm :tabmove<SPACE> | ||
197 | map <leader>to :tabonly<CR> | ||
198 | |||
199 | noremap <silent><m-h> :call Tab_MoveLeft()<CR> | ||
200 | noremap <silent><m-l> :call Tab_MoveRight()<CR> | ||
201 | 176 | ||
202 | " Let <leader>tl toggle between this and the last accessed tab | 177 | " source .vimrc |
203 | let g:lasttab = 1 | 178 | nnoremap <leader>so V:so<CR> |
204 | nmap <Leader>tl :exe "tabn ".g:lasttab<CR> | 179 | nnoremap <leader><leader>so :source ~/.vimrc<CR> |
205 | autocmd TabLeave * let g:lasttab = tabpagenr() | 180 | vnoremap so :source<CR> |
181 | autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC | ||
206 | 182 | ||
207 | " Opens a new tab with the current buffer's path | 183 | " Find scripts |
208 | " Super useful when editing files in the same directory | 184 | nnoremap <leader>e :scriptnames<space> |
209 | map <leader>te :tabedit <C-r>=expand("%:p:h")<CR> | 185 | nnoremap <leader>ee :edit $MYVIMRC<CR> |
210 | 186 | ||
211 | " Tab move functions | ||
212 | function! Tab_MoveLeft() | ||
213 | let l:tabnr = tabpagenr() - 2 | ||
214 | if l:tabnr >= 0 | ||
215 | exec 'tabmove '.l:tabnr | ||
216 | endif | ||
217 | endfunc | ||
218 | function! Tab_MoveRight() | ||
219 | let l:tabnr = tabpagenr() + 1 | ||
220 | if l:tabnr <= tabpagenr('$') | ||
221 | exec 'tabmove '.l:tabnr | ||
222 | endif | ||
223 | endfunc | ||
224 | " }}} | 187 | " }}} |
225 | " MANAGE_BUFFERS ----------------{{{ | 188 | " MANAGE_BUFFERS ----------------{{{ |
226 | 189 | ||
227 | " set buflisted | 190 | " Set options |
228 | noremap <leader>st :set<space> | 191 | noremap <leader>st :set<space> |
192 | noremap <leader><leader>ft :<C-\>e'set filetype='..&filetype<CR> | ||
229 | 193 | ||
230 | " Open a new buffer | 194 | " Open a new buffer |
231 | nmap <leader><leader>b :enew<CR> | ||
232 | nmap <leader>B :enew<CR> | 195 | nmap <leader>B :enew<CR> |
233 | nmap <leader>O :e /tmp/buffer<CR> | 196 | nmap <leader>O :e /tmp/buffer<CR> |
234 | 197 | ||
235 | " Next buffer | ||
236 | noremap <leader>l :exe "buffer ".g:lastbuffer<CR> | ||
237 | " noremap <Tab> :exe 'buffer '.g:lastbuffer<CR> | ||
238 | |||
239 | " set filetype | ||
240 | noremap <leader><leader>ft :<C-\>e'set filetype='..&filetype<CR> | ||
241 | |||
242 | " Let <leader>l toggle between this and the last accessed buffer | 198 | " Let <leader>l toggle between this and the last accessed buffer |
243 | augroup SaveLastBuffer | 199 | augroup SaveLastBuffer |
244 | let g:lastbuffer = 1 | 200 | let g:lastbuffer = 1 |
245 | au BufLeave * let g:lastbuffer = bufnr() | 201 | au BufLeave * let g:lastbuffer = bufnr() |
246 | augroup END | 202 | augroup END |
203 | noremap <leader>l :exe "buffer ".g:lastbuffer<CR> | ||
204 | |||
205 | " Use Ctrl-C for buffer delete or quit vim ----------------{{{ | ||
206 | |||
207 | " Toggle behavior for the last buffer in the last window | ||
208 | let g:quitVimWhenPressingCtrlC = 1 | ||
209 | function! ToggleQuit() | ||
210 | let g:quitVimWhenPressingCtrlC = g:quitVimWhenPressingCtrlC ? 0 : 1 | ||
211 | let message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock" | ||
212 | echo message | ||
213 | endfunction | ||
214 | nnoremap <leader><leader>gl :call ToggleQuit()<CR> | ||
215 | |||
216 | function! CheckSave() | ||
217 | if &modified | ||
218 | let answer = confirm("Save changes?", "&Yes\n&No\n&Cancel") | ||
219 | if answer == 1 | write | endif | ||
220 | if answer == 3 | return | endif | ||
221 | endif | ||
222 | |||
223 | if len(getbufinfo({'buflisted': 1})) == 2 | ||
224 | try | buffer # | bdelete! # | catch | endtry | ||
225 | else | ||
226 | bdelete! | ||
227 | endif | ||
228 | endfunction | ||
229 | func! QuitWithCheck() | ||
230 | if g:quitVimWhenPressingCtrlC | ||
231 | silent! quit | ||
232 | else | ||
233 | echo "Press <leader><leader>gl to allow quit with <C-c>" | ||
234 | endif | ||
235 | endfunc | ||
236 | function! Bye() | ||
237 | let windows = gettabinfo(tabpagenr())[0]['windows'] | ||
238 | let bufs = gettabinfo(tabpagenr())[0]['variables']['bufs'] | ||
239 | if len(windows) == 1 && len(bufs) == 1 | ||
240 | echo 'quit' | ||
241 | call QuitWithCheck() | ||
242 | elseif &diff | ||
243 | call CloseBuffersForDiff() | ||
244 | else | ||
245 | echo 'bdelete' | ||
246 | call CheckSave() | ||
247 | endif | ||
248 | endfunction | ||
249 | nnoremap <silent> <C-c> :call Bye()<CR> | ||
250 | |||
251 | "}}} | ||
252 | " Diff Mode ----------------{{{ | ||
253 | |||
254 | function! CloseBuffersForDiff() | ||
255 | windo | if &diff && &buftype == "nofile" | bdelete | endif | ||
256 | norm! zv | ||
257 | endfunction | ||
258 | |||
259 | command! DiffOrig vert new | set buftype=nofile nobuflisted | read ++edit # | 0d_ | ||
260 | \ | diffthis | wincmd p | diffthis | ||
247 | 261 | ||
248 | " Uset <C-w>d to toggle Diff mode | 262 | " Uset <C-w>d to toggle Diff mode |
249 | function! s:SwitchDiff() | 263 | function! s:SwitchDiff() |
250 | if &diff | 264 | if &diff |
251 | windo | if &buftype == "nofile" | bdelete | endif | 265 | call CloseBuffersForDiff() |
252 | else | 266 | else |
253 | DiffOrig | 267 | DiffOrig |
254 | wincmd p | set nobuflisted | wincmd p | ||
255 | endif | 268 | endif |
256 | endfunction | 269 | endfunction |
257 | com! SwitchDiff call s:SwitchDiff() | 270 | com! SwitchDiff call s:SwitchDiff() |
258 | nnoremap <C-w>d <Cmd>silent! SwitchDiff<CR> | 271 | nnoremap <C-w>d <Cmd>silent! SwitchDiff<CR> |
259 | 272 | ||
260 | function! s:SwitchDiffForGitHEAD() | 273 | function! s:SwitchDiffForGitHEAD() |
261 | nmap cdg | 274 | norm cdg |
262 | if &diff | 275 | if &diff |
263 | windo | if &buftype == "nofile" | bdelete | endif | 276 | windo | if &buftype == "nofile" | bdelete | endif |
277 | norm! zv | ||
264 | else | 278 | else |
265 | vert new | set buftype=nofile nobuflisted | 279 | vert new | set buftype=nofile nobuflisted |
266 | read !git show HEAD:# | 280 | read !git show HEAD:# |
267 | 0d_ | diffthis | wincmd p | diffthis | 281 | 0d_ | diffthis | wincmd p | diffthis |
268 | endif | 282 | endif |
269 | endfunction | 283 | endfunction |
270 | com! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD() | 284 | command! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD() |
271 | nnoremap <C-w>D <Cmd>silent! SwitchDiffForGitHEAD<CR> | 285 | nnoremap <C-w>D <Cmd>silent! SwitchDiffForGitHEAD<CR> |
286 | |||
287 | " }}} | ||
288 | |||
272 | " }}} | 289 | " }}} |
273 | " MANAGE_WINDOWS ----------------{{{ | 290 | " MANAGE_WINDOWS ----------------{{{ |
274 | 291 | ||
@@ -306,7 +323,41 @@ elseif has('nvim') | |||
306 | tnoremap <m-q> <c-\><c-n> | 323 | tnoremap <m-q> <c-\><c-n> |
307 | endif | 324 | endif |
308 | " }}} | 325 | " }}} |
309 | " FOLDING ----------------{{{ | 326 | " MANAGE_TABS ----------------{{{ |
327 | |||
328 | " Useful mappings for managing tabs | ||
329 | map <leader>tn :tabnew<CR> | ||
330 | map <leader>tc :tabclose<CR> | ||
331 | map <leader>tm :tabmove<SPACE> | ||
332 | map <leader>to :tabonly<CR> | ||
333 | |||
334 | noremap <silent><m-h> :call Tab_MoveLeft()<CR> | ||
335 | noremap <silent><m-l> :call Tab_MoveRight()<CR> | ||
336 | |||
337 | " Let <leader>tl toggle between this and the last accessed tab | ||
338 | let g:lasttab = 1 | ||
339 | nmap <Leader>tl :exe "tabn ".g:lasttab<CR> | ||
340 | autocmd TabLeave * let g:lasttab = tabpagenr() | ||
341 | |||
342 | " Opens a new tab with the current buffer's path | ||
343 | " Super useful when editing files in the same directory | ||
344 | map <leader>te :tabedit <C-r>=expand("%:p:h")<CR> | ||
345 | |||
346 | " Tab move functions | ||
347 | function! Tvab_MoveLeft() | ||
348 | let l:tabnr = tabpagenr() - 2 | ||
349 | if l:tabnr >= 0 | ||
350 | exec 'tabmove '.l:tabnr | ||
351 | endif | ||
352 | endfunc | ||
353 | function! Tab_MoveRight() | ||
354 | let l:tabnr = tabpagenr() + 1 | ||
355 | if l:tabnr <= tabpagenr('$') | ||
356 | exec 'tabmove '.l:tabnr | ||
357 | endif | ||
358 | endfunc | ||
359 | " }}} | ||
360 | " FOLD ----------------{{{ | ||
310 | 361 | ||
311 | " Set foldmethod | 362 | " Set foldmethod |
312 | noremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR> | 363 | noremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR> |
@@ -317,13 +368,19 @@ nnoremap zr zr:set foldlevel<CR> | |||
317 | 368 | ||
318 | " Use l to open fold | 369 | " Use l to open fold |
319 | nnoremap <expr> l foldclosed('.') == -1 ? 'l' : 'zo' | 370 | nnoremap <expr> l foldclosed('.') == -1 ? 'l' : 'zo' |
320 | sourcesourcesource | 371 | |
321 | " Open fold in next line | 372 | " Open fold in next line |
322 | nnoremap <expr> zo foldclosed('.') == -1 ? 'zjzo' : 'zo' | 373 | nnoremap <expr> zo foldclosed('.') == -1 ? 'zjzo' : 'zo' |
323 | nnoremap <expr> zO foldclosed('.') == -1 ? 'zjzO' : 'zO' | 374 | nnoremap <expr> zO foldclosed('.') == -1 ? 'zjzO' : 'zO' |
324 | 375 | ||
325 | " }}} | 376 | " }}} |
326 | " SURROURD_WITH_CHAR ----------------{{{ | 377 | " SURROUND ----------------{{{ |
378 | |||
379 | inoremap ' ''<Left> | ||
380 | inoremap " ""<Left> | ||
381 | inoremap ( ()<Left> | ||
382 | inoremap [ []<Left> | ||
383 | inoremap { {}<Left> | ||
327 | 384 | ||
328 | vnoremap S sa | 385 | vnoremap S sa |
329 | vnoremap ' <ESC>`<i'<ESC>`>la'<ESC> | 386 | vnoremap ' <ESC>`<i'<ESC>`>la'<ESC> |
@@ -403,7 +460,6 @@ let g:tig_explorer_keymap_commit_vsplit = '<C-v>' | |||
403 | nnoremap <C-t> <Cmd>Tig<CR> | 460 | nnoremap <C-t> <Cmd>Tig<CR> |
404 | nnoremap <C-t>s <Cmd>TigStatus<CR> | 461 | nnoremap <C-t>s <Cmd>TigStatus<CR> |
405 | nnoremap <C-t>b <Cmd>TigBlame<CR> | 462 | nnoremap <C-t>b <Cmd>TigBlame<CR> |
406 | nnoremap <C-t>d :vertical TigOpenFileWithCommit <C-R>+ % 0<CR> | ||
407 | 463 | ||
408 | " }}} | 464 | " }}} |
409 | " Markdown items (temproray solution) ----------------{{{ | 465 | " Markdown items (temproray solution) ----------------{{{ |