You can't change the way the :b
command works, but you can create an alternative :B
command, and then create an abbreviation to always use the :B
command when you type :b
.
The below works by first trying using the :b
command, and if the E94 error is thrown, doing a :FZF
instead:
function! BWithFallback(buffer_name) abort
" Check if the input is a number or a string
if a:buffer_name =~# '^\d\+$'
execute 'b' a:buffer_name
else
try
" Try opening the buffer
execute 'b' a:buffer_name
catch /E94/
" If the buffer didn't exist, try doing a fuzzy file search
" using the command provided by the OP.
execute 'FZF''FZF -q' a:buffer_name
endtry
endif
endfunction
" Create new :B command. You could also use -complete=file if you prefer
command! -nargs=1 -complete=buffer B call BWithFallback('<args>')
" When you type :b, replace it with :B
cnoreabbrev <expr> b (getcmdtype() == ':' && getcmdline() ==# 'b') ? 'B' : b
N.B. I don't have fzf
installed, so I don't know if the command :FZF <filename>
works. If not, you will need to edit the line that performs that command. If you can't figure it out, let me know and I'll look into it.