vimtricks.wiki Concise Vim tricks, one at a time.

How do I switch to a buffer by typing part of its filename?

Answer

:b partial<Tab>

Explanation

The :b (buffer) command accepts partial filename matching with tab completion. You don't need to remember buffer numbers or type full paths — just type enough characters to uniquely identify the file.

How it works

:b main<Tab>     " Completes to 'main.go' if unique
:b hand<Tab>     " Completes to 'handlers.go'
:b test<Tab>     " Cycles through buffers containing 'test'

Substring matching

Vim matches against any part of the filename:

:b util          " Matches 'src/utils/helpers.go'
:b spec          " Matches 'tests/handler_spec.rb'

With set hidden

Ensure you can switch buffers without saving first:

:set hidden      " Allow switching from unsaved buffers

Multiple matches

If multiple buffers match, Tab cycles through them:

:b test<Tab>     " → test_handler.go
:b test<Tab>     " → test_utils.go
:b test<Tab>     " → test_model.go

Or use :ls to see all buffers first:

:ls              " List all buffers with numbers
:b 3             " Switch to buffer 3

Tips

  • Enable wildmenu for a visual completion bar: :set wildmenu
  • Set wildmode=longest:full,full for bash-like completion
  • :sb partial opens the buffer in a new split
  • :vert sb partial opens in a vertical split
  • :b # switches to the alternate buffer (same as <C-^>)
  • With wildmenu enabled, use <Left>/<Right> to browse matches
  • This is often faster than fuzzy finders for small projects

Next

How do I run the same command across all windows, buffers, or tabs?