Skip to main content

Buffers

A buffer is an in-memory instance of a file (or an unnamed scratch space). Understanding buffers is essential for working with multiple files efficiently in Neovim.

Core Idea

A buffer holds file content in memory. A window displays a buffer. You can have hundreds of buffers open and only a few windows showing them.

How Buffers Work​

Opening Files into Buffers​

:e filename.lua → edit (open) a file
:e . → open the directory (netrw file browser)
:badd filename.lua → add file to buffer list without displaying
:bnext :bn → next buffer
:bprev :bp → previous buffer
:bfirst :bf → first buffer
:blast :bl → last buffer
:b# → alternate (last) buffer
:b {num} → switch to buffer number
:b {partial} → switch by partial name match

Keymap Recommendations​

~/.config/nvim/lua/config/keymaps.lua
vim.keymap.set('n', '<leader>bn', ':bnext<CR>', { desc = "Next buffer" })
vim.keymap.set('n', '<leader>bp', ':bprev<CR>', { desc = "Previous buffer" })
vim.keymap.set('n', '<leader>bd', ':bdelete<CR>', { desc = "Delete buffer" })
vim.keymap.set('n', '<Tab>', ':bnext<CR>', { desc = "Next buffer" })
vim.keymap.set('n', '<S-Tab>', ':bprev<CR>', { desc = "Prev buffer" })

Listing Buffers​

:ls → list all buffers with their status
:buffers → same as :ls

Output example:

1 h "init.lua" line 1
2 a "options.lua" line 45
3 %a "keymaps.lua" line 12
4 # "plugins/lsp.lua" line 1
SymbolMeaning
%Current buffer
#Alternate buffer
aActive (visible)
hHidden
+Modified (unsaved)
-Not modifiable

Closing Buffers​

:bdelete :bd → close current buffer
:bdelete! :bd! → close without saving
:bdelete 3 → close buffer number 3
:%bdelete → close all buffers
warning

:bdelete removes the buffer but does NOT close the window. If it was the only buffer, you'll see an empty window.

Buffer-Specific Commands​

:bufdo {cmd} → run command in **all** buffers
:bufdo %s/old/new/ge → replace in all open buffers + suppress errors
:bufdo update → save all modified buffers

Writing and Quitting Buffers​

:w → write current buffer
:wa → write all modified buffers
:wq → write and quit
:qa → quit all windows
:qa! → quit all without saving

Buffer Types​

Buffer typeDescription
Normal bufferRegular file being edited
Scratch bufferUnnamed, temporary content
Help buffer:help content
Terminal buffer:terminal output
Quickfix buffer:copen list

Creating a Scratch Buffer​

-- Create a new empty scratch buffer
vim.keymap.set('n', '<leader>bs', function()
vim.cmd('enew')
vim.bo.buftype = 'nofile'
vim.bo.bufhidden = 'wipe'
vim.bo.swapfile = false
end, { desc = "New scratch buffer" })

In LazyVim​

LazyVim Context

LazyVim pre-configures keymaps and plugins for buffer management. It uses bufferline.nvim to display buffers at the top of the screen like traditional editor tabs, and neo-tree or Telescope for switching.

AspectVanilla NeovimLazyVim
Next/Prev Buffer:bnext / :bprev<S-h> / <S-l> or [b / ]b
Delete Buffer:bdelete<leader>bd (uses mini.bufremove to avoid messing up window layout)
Close other buffers:1,$-1bd (complex)<leader>bo
Switch Buffer:b [name]<leader>fb (Telescope buffers)

What's Next​