aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjdhao <jdhao@hotmail.com>2020-10-17 01:14:02 +0800
committerjdhao <jdhao@hotmail.com>2020-10-17 01:14:02 +0800
commite456325257a6b7edf8b143f86deab26e119a11b9 (patch)
tree4b3d25740100be07ac06b7afd3ed48be021f7199
parent666ec43e0b68ce73a52d14edb539b3c13787585c (diff)
add gb mapping to switch buffers
-rw-r--r--mappings.vim29
1 files changed, 29 insertions, 0 deletions
diff --git a/mappings.vim b/mappings.vim
index df4afe1..e9abf86 100644
--- a/mappings.vim
+++ b/mappings.vim
@@ -179,4 +179,33 @@ xnoremap <silent> <A-j> :<C-U>call utils#MoveSelection('down')<CR>
" Replace visual selection with text in register, but not contaminate the
" register, see also https://stackoverflow.com/q/10723700/6064933.
xnoremap p "_c<ESC>p
+
+nnoremap <silent> gb :<C-U>call <SID>GoToBuffer(v:count, 'forward')<CR>
+nnoremap <silent> gB :<C-U>call <SID>GoToBuffer(v:count, 'backward')<CR>
+
+function! s:GoToBuffer(count, direction) abort
+ if a:count == 0
+ if a:direction ==# 'forward'
+ bnext
+ elseif a:direction ==# 'backward'
+ bprevious
+ else
+ echoerr 'Bad argument ' a:direction
+ endif
+ return
+ endif
+ " Check the validity of buffer number.
+ if index(s:GetBufNums(), a:count) == -1
+ echohl WarningMsg | echomsg 'Invalid bufnr: ' a:count | echohl None
+ return
+ endif
+
+ silent execute('buffer' . a:count)
+endfunction
+
+function! s:GetBufNums() abort
+ let l:buf_infos = getbufinfo({'buflisted':1})
+ let l:buf_nums = map(l:buf_infos, "v:val['bufnr']")
+ return l:buf_nums
+endfunction
"}