Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
vim: buffer sel by pattern
" buffer sel by pattern {{{
" ====================================
function! BufSel(pattern)
let buf_count = bufnr("$")
let cur_bufnr = 1
let nummatches = 0
let firstmatchingbufnr = 0
while cur_bufnr <= buf_count
if(bufexists(cur_bufnr))
let currbufname = bufname(cur_bufnr)
if(match(currbufname, a:pattern) > -1)
echo cur_bufnr . ": ". bufname(cur_bufnr)
let nummatches += 1
let firstmatchingbufnr = cur_bufnr
endif
endif
let cur_bufnr = cur_bufnr + 1
endwhile
if(nummatches == 1)
execute ":buffer ". firstmatchingbufnr
elseif(nummatches > 1)
let desiredbufnr = input("Enter buffer number: ")
if(strlen(desiredbufnr) != 0)
execute ":buffer ". desiredbufnr
endif
else
echo "No matching buffers"
endif
endfunction
function! BufSelInput()
let pattern = input( "pattern: " )
call BufSel( pattern )
endfunction
"Bind the BufSel() function to a user-command
command! -nargs=1 Bs :call BufSel("<args>")
nmap <leader>bf :call BufSelInput()<CR>
" ====================================
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.