Home

BufEnter

BufEnter is an autocommand event in Vim and Neovim that fires when a buffer becomes the active buffer in a window. It runs after the buffer is shown in the current window, and it is commonly used to apply buffer-local options, initialize state, or trigger actions that should occur whenever you switch to that buffer.

Related events include BufWinEnter and BufLeave. BufWinEnter fires when a window displaying a buffer becomes active,

Typical usage involves defining autocommands that adjust behavior on a per-buffer basis. For example, you might

Because BufEnter can fire frequently as you navigate between buffers, it is common to limit commands with

See also BufLeave and BufWinEnter for related lifecycle events, as well as general autocommand usage and per-buffer

---

which
can
happen
when
you
move
between
windows.
BufEnter,
by
contrast,
is
specifically
about
the
buffer
becoming
the
active
buffer
in
the
current
window,
regardless
of
whether
you
have
just
changed
windows
or
switched
buffers
within
the
same
window.
set
buffer-local
options
when
entering
certain
file
types,
such
as
changing
indentation
for
Python
files
or
enabling
line
wrapping
for
Markdown
files.
Examples
include
setting
tab
width
for
Python
buffers
or
enabling
wrap
for
text-oriented
files,
all
scoped
to
the
buffers
that
match
a
pattern.
filetype
or
filename
patterns,
or
to
scope
commands
to
the
<buffer>
to
avoid
affecting
other
buffers
unintentionally.
Proper
scoping
helps
keep
behavior
predictable
and
efficient
as
you
switch
among
multiple
open
buffers.
configuration
practices.