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.vim230
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
15map <silent> <leader><CR> :noh<CR> 14map <silent> <leader><CR> :noh<CR>
16 15
17" Paste register 0 16" Set wrap
18nnoremap <C-p> "0p 17nnoremap <leader>W :set wrap!<CR>:set wrap?<CR>
19 18
20" Fast saving 19" Fast saving
21nmap <leader>w :w!<CR> 20nmap <leader>w :w!<CR>
22 21
22" :W sudo saves the file
23" (useful for handling the permission-denied error)
24command! W execute 'w !sudo -S tee %' <bar> edit!
25
23" Fast quit with error 26" Fast quit with error
24nmap <leader>q :q<CR> 27nmap <leader>q :q<CR>
25 28
26" Fast quit with error 29" Fast quit with error
27nmap cq :cq<CR> 30nmap cq :cq<CR>
28 31
29" Switch wrap
30nmap <leader>W :set wrap!<CR>
31
32" :W sudo saves the file
33" (useful for handling the permission-denied error)
34command! W execute 'w !sudo -S tee %' <bar> edit!
35
36" Enter to open file
37nnoremap <CR> gf
38nnoremap gF :e <cfile><CR>
39
40" Remap <CR> in Quickfix, Cmdwin Location list 32" Remap <CR> in Quickfix, Cmdwin Location list
41augroup vimrc_CRfix 33augroup vimrc_CRfix
42 au! 34 au!
@@ -49,13 +41,25 @@ augroup END
49" nnoremap <leader>, :terminal ++noclose<CR> 41" nnoremap <leader>, :terminal ++noclose<CR>
50vnoremap <leader>, :terminal<CR> 42vnoremap <leader>, :terminal<CR>
51 43
44" Paste register 0
45nnoremap <C-p> "0p
46
52" Toggle paste mode on and off 47" Toggle paste mode on and off
53map <leader>pp :setlocal paste!<CR> 48map <leader>pp :setlocal paste!<CR>
54 49
50" Copy from system clipboard
51nnoremap <leader>P :r !xsel -ob<CR>
52vnoremap 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
56nnoremap cd :cd %:p:h<CR>:pwd<CR> 55nnoremap cd :cd %:p:h<CR>:pwd<CR>
57" Switch CWD to root git directory 56" Switch CWD to root git directory
58nnoremap cdg :execute 'cd' fnameescape(fnamemodify(finddir('.git', escape(expand('%:p:h'), ' ') . ';'), ':h'))<CR>:pwd<CR> 57function! 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
61endfunction
62nnoremap cdg :call CdToGitRepo()<CR>:pwd<CR>
59 63
60" alias for cd 64" alias for cd
61nnoremap cdd :cd<space> 65nnoremap 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
87nnoremap <leader>P :r !xsel -ob<CR>
88vnoremap Y :w !xsel -ib<CR>
89
90" Spell 90" Spell
91nnoremap <leader><leader>sp :set spell!<CR> 91nnoremap <leader><leader>sp :set spell!<CR>
92nnoremap <leader>ss ]s 92nnoremap <leader>ss ]s
@@ -99,11 +99,6 @@ nnoremap <C-g> 1<C-g>
99vnoremap Tz :!trans -t zh-TW -b<CR> 99vnoremap Tz :!trans -t zh-TW -b<CR>
100vnoremap Te :!trans -t en-US -b<CR> 100vnoremap Te :!trans -t en-US -b<CR>
101 101
102" source .vimrc
103nnoremap <leader>so V:so<CR>
104nnoremap <leader><leader>so :source ~/.vimrc<CR>
105vnoremap 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')
115noremap <C-h> 30h 110noremap <C-h> 30h
116noremap <C-l> 30l 111noremap <C-l> 30l
117 112
118" }}} 113" File under the cursor
119" MANAGE_VIMRC ----------------{{{ 114nnoremap <CR> gf
115nnoremap gF :e <cfile><CR>
120 116
121nnoremap <leader>e :scriptnames<space> 117" READLINE_FEATURES ----------------{{{
122nnoremap <leader>ee :edit $MYVIMRC<CR>
123autocmd! BUFWRITEPOST $MYVIMRC source $MYVIMRC
124
125" }}}
126" MOVING_WITH_READLINE ----------------{{{
127 118
128inoremap <C-f> <Right> 119inoremap <C-f> <Right>
129inoremap <C-b> <Left> 120inoremap <C-b> <Left>
@@ -157,15 +148,6 @@ noremap <m-k> gk
157inoremap <m-j> <c-\><c-o>gj 148inoremap <m-j> <c-\><c-o>gj
158inoremap <m-k> <c-\><c-o>gk 149inoremap <m-k> <c-\><c-o>gk
159" }}} 150" }}}
160" INSERT_SURROUNDING ----------------{{{
161
162inoremap ' ''<Left>
163inoremap " ""<Left>
164inoremap ( ()<Left>
165inoremap [ []<Left>
166inoremap { {}<Left>
167
168" }}}
169" JUMP_TO_TABS_WITH_ALT ----------------{{{ 151" JUMP_TO_TABS_WITH_ALT ----------------{{{
170 152
171noremap <silent><A-1> :tabn 1<CR> 153noremap <silent><A-1> :tabn 1<CR>
@@ -188,87 +170,122 @@ inoremap <silent><M-8> <Esc>:tabn 8<CR>
188inoremap <silent><M-9> <Esc>:tablast<CR> 170inoremap <silent><M-9> <Esc>:tablast<CR>
189 171
190" }}} 172" }}}
191" MANAGE_TABS ----------------{{{
192 173
193" Useful mappings for managing tabs 174" }}}
194map <leader>tn :tabnew<CR> 175" MANAGE_VIMRC ----------------{{{
195map <leader>tc :tabclose<CR>
196map <leader>tm :tabmove<SPACE>
197map <leader>to :tabonly<CR>
198
199noremap <silent><m-h> :call Tab_MoveLeft()<CR>
200noremap <silent><m-l> :call Tab_MoveRight()<CR>
201 176
202" Let <leader>tl toggle between this and the last accessed tab 177" source .vimrc
203let g:lasttab = 1 178nnoremap <leader>so V:so<CR>
204nmap <Leader>tl :exe "tabn ".g:lasttab<CR> 179nnoremap <leader><leader>so :source ~/.vimrc<CR>
205autocmd TabLeave * let g:lasttab = tabpagenr() 180vnoremap so :source<CR>
181autocmd! 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 184nnoremap <leader>e :scriptnames<space>
209map <leader>te :tabedit <C-r>=expand("%:p:h")<CR> 185nnoremap <leader>ee :edit $MYVIMRC<CR>
210 186
211" Tab move functions
212function! Tab_MoveLeft()
213 let l:tabnr = tabpagenr() - 2
214 if l:tabnr >= 0
215 exec 'tabmove '.l:tabnr
216 endif
217endfunc
218function! Tab_MoveRight()
219 let l:tabnr = tabpagenr() + 1
220 if l:tabnr <= tabpagenr('$')
221 exec 'tabmove '.l:tabnr
222 endif
223endfunc
224" }}} 187" }}}
225" MANAGE_BUFFERS ----------------{{{ 188" MANAGE_BUFFERS ----------------{{{
226 189
227" set buflisted 190" Set options
228noremap <leader>st :set<space> 191noremap <leader>st :set<space>
192noremap <leader><leader>ft :<C-\>e'set filetype='..&filetype<CR>
229 193
230" Open a new buffer 194" Open a new buffer
231nmap <leader><leader>b :enew<CR>
232nmap <leader>B :enew<CR> 195nmap <leader>B :enew<CR>
233nmap <leader>O :e /tmp/buffer<CR> 196nmap <leader>O :e /tmp/buffer<CR>
234 197
235" Next buffer
236noremap <leader>l :exe "buffer ".g:lastbuffer<CR>
237" noremap <Tab> :exe 'buffer '.g:lastbuffer<CR>
238
239" set filetype
240noremap <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
243augroup SaveLastBuffer 199augroup 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()
246augroup END 202augroup END
203noremap <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
208let g:quitVimWhenPressingCtrlC = 1
209function! ToggleQuit()
210 let g:quitVimWhenPressingCtrlC = g:quitVimWhenPressingCtrlC ? 0 : 1
211 let message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock"
212 echo message
213endfunction
214nnoremap <leader><leader>gl :call ToggleQuit()<CR>
215
216function! 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
228endfunction
229func! QuitWithCheck()
230 if g:quitVimWhenPressingCtrlC
231 silent! quit
232 else
233 echo "Press <leader><leader>gl to allow quit with <C-c>"
234 endif
235endfunc
236function! 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
248endfunction
249nnoremap <silent> <C-c> :call Bye()<CR>
250
251"}}}
252" Diff Mode ----------------{{{
253
254function! CloseBuffersForDiff()
255 windo | if &diff && &buftype == "nofile" | bdelete | endif
256 norm! zv
257endfunction
258
259command! 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
249function! s:SwitchDiff() 263function! 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
256endfunction 269endfunction
257com! SwitchDiff call s:SwitchDiff() 270com! SwitchDiff call s:SwitchDiff()
258nnoremap <C-w>d <Cmd>silent! SwitchDiff<CR> 271nnoremap <C-w>d <Cmd>silent! SwitchDiff<CR>
259 272
260function! s:SwitchDiffForGitHEAD() 273function! 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
269endfunction 283endfunction
270com! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD() 284command! SwitchDiffForGitHEAD call s:SwitchDiffForGitHEAD()
271nnoremap <C-w>D <Cmd>silent! SwitchDiffForGitHEAD<CR> 285nnoremap <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>
307endif 324endif
308" }}} 325" }}}
309" FOLDING ----------------{{{ 326" MANAGE_TABS ----------------{{{
327
328" Useful mappings for managing tabs
329map <leader>tn :tabnew<CR>
330map <leader>tc :tabclose<CR>
331map <leader>tm :tabmove<SPACE>
332map <leader>to :tabonly<CR>
333
334noremap <silent><m-h> :call Tab_MoveLeft()<CR>
335noremap <silent><m-l> :call Tab_MoveRight()<CR>
336
337" Let <leader>tl toggle between this and the last accessed tab
338let g:lasttab = 1
339nmap <Leader>tl :exe "tabn ".g:lasttab<CR>
340autocmd 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
344map <leader>te :tabedit <C-r>=expand("%:p:h")<CR>
345
346" Tab move functions
347function! Tvab_MoveLeft()
348 let l:tabnr = tabpagenr() - 2
349 if l:tabnr >= 0
350 exec 'tabmove '.l:tabnr
351 endif
352endfunc
353function! Tab_MoveRight()
354 let l:tabnr = tabpagenr() + 1
355 if l:tabnr <= tabpagenr('$')
356 exec 'tabmove '.l:tabnr
357 endif
358endfunc
359" }}}
360" FOLD ----------------{{{
310 361
311" Set foldmethod 362" Set foldmethod
312noremap <leader><leader>fm :<C-\>e'set foldmethod='..&foldmethod<CR> 363noremap <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
319nnoremap <expr> l foldclosed('.') == -1 ? 'l' : 'zo' 370nnoremap <expr> l foldclosed('.') == -1 ? 'l' : 'zo'
320sourcesourcesource 371
321" Open fold in next line 372" Open fold in next line
322nnoremap <expr> zo foldclosed('.') == -1 ? 'zjzo' : 'zo' 373nnoremap <expr> zo foldclosed('.') == -1 ? 'zjzo' : 'zo'
323nnoremap <expr> zO foldclosed('.') == -1 ? 'zjzO' : 'zO' 374nnoremap <expr> zO foldclosed('.') == -1 ? 'zjzO' : 'zO'
324 375
325" }}} 376" }}}
326" SURROURD_WITH_CHAR ----------------{{{ 377" SURROUND ----------------{{{
378
379inoremap ' ''<Left>
380inoremap " ""<Left>
381inoremap ( ()<Left>
382inoremap [ []<Left>
383inoremap { {}<Left>
327 384
328vnoremap S sa 385vnoremap S sa
329vnoremap ' <ESC>`<i'<ESC>`>la'<ESC> 386vnoremap ' <ESC>`<i'<ESC>`>la'<ESC>
@@ -403,7 +460,6 @@ let g:tig_explorer_keymap_commit_vsplit = '<C-v>'
403nnoremap <C-t> <Cmd>Tig<CR> 460nnoremap <C-t> <Cmd>Tig<CR>
404nnoremap <C-t>s <Cmd>TigStatus<CR> 461nnoremap <C-t>s <Cmd>TigStatus<CR>
405nnoremap <C-t>b <Cmd>TigBlame<CR> 462nnoremap <C-t>b <Cmd>TigBlame<CR>
406nnoremap <C-t>d :vertical TigOpenFileWithCommit <C-R>+ % 0<CR>
407 463
408" }}} 464" }}}
409" Markdown items (temproray solution) ----------------{{{ 465" Markdown items (temproray solution) ----------------{{{