From e456325257a6b7edf8b143f86deab26e119a11b9 Mon Sep 17 00:00:00 2001 From: jdhao Date: Sat, 17 Oct 2020 01:14:02 +0800 Subject: [PATCH] add gb mapping to switch buffers --- mappings.vim | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/mappings.vim b/mappings.vim index df4afe1..e9abf86 100644 --- a/mappings.vim +++ b/mappings.vim @@ -179,4 +179,33 @@ xnoremap :call utils#MoveSelection('down') " Replace visual selection with text in register, but not contaminate the " register, see also https://stackoverflow.com/q/10723700/6064933. xnoremap p "_cp + +nnoremap gb :call GoToBuffer(v:count, 'forward') +nnoremap gB :call GoToBuffer(v:count, 'backward') + +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 "}