Skip to content

Instantly share code, notes, and snippets.

@c9s
Created March 27, 2009 04:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save c9s/86544 to your computer and use it in GitHub Desktop.
Save c9s/86544 to your computer and use it in GitHub Desktop.
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