Easy answer: use tab completion with :buffers
.
- Type
:b
(there's a space at the end)
- Enter any part of a buffer name.
- Press tab.
If the pattern matches a single buffer you'll be switched to that buffer immediately. Otherwise, you'll see the matches in the standard tab completion way, i.e. all the matching buffers will be shown and you tab through to the one you want to open.
If you want something a bit more sophisticated that allows search with regular expressions and includes a nice menu when there are multiple matches then take a look at this function and command from https://vim.fandom.com/wiki/Easier_buffer_switching
function! BufSel(pattern)
let bufcount = bufnr("$")
let currbufnr = 1
let nummatches = 0
let firstmatchingbufnr = 0
while currbufnr <= bufcount
if(bufexists(currbufnr))
let currbufname = bufname(currbufnr)
if(match(currbufname, a:pattern) > -1)
echo currbufnr . ": ". bufname(currbufnr)
let nummatches += 1
let firstmatchingbufnr = currbufnr
endif
endif
let currbufnr = currbufnr + 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
"Bind the BufSel() function to a user-command
command! -nargs=1 Bs :call BufSel("<args>")
This will...
jump to the matching buffer if only one match is found, or if there are many matches it will print a list of the matching buffers in the command-line area, and allow you to select one of the matching buffers by buffer number.