Buffers
πΉ What Are Buffers?
A buffer is a temporary in-memory representation of an open file or unsaved text inside an editor. Think of buffers as "open documents" that you can switch between without closing them.
π οΈ Buffers in Vim/Neovim/GVim
When you open a file in Vim, it gets loaded into a buffer.
You can open multiple buffers at the same time without opening multiple windows or tabs.
Buffers can hold files, temporary text, or unsaved changes.
Even if a buffer is hidden, it remains in memory and can be switched back to.
π οΈ Buffers in Emacs
Emacs also uses buffers, but it treats everything as a buffer, including files, help pages, terminal emulators, and interactive shells.
Unlike Vim, Emacs does not have a strict "tab" system, as all content is just another buffer.
In Vim, Neovim, and GVim, you can list all open buffers and switch between them using the following commands:
1οΈβ£ List All Open Buffers
In normal mode, type:
or
This will show a list of open buffers with their buffer numbers, status, and filenames.
#
β Indicates the alternate buffer+
β The currently active buffer%
β The current buffer-
β Previously active buffer
2οΈβ£ Switch Between Buffers
You can switch between buffers using their buffer number or by cycling through them.
Switch to a Specific Buffer by Number
Use:
Example:
(This will switch to buffer 3.)
Cycle Through Buffers
Next Buffer:
Previous Buffer:
Switch to Last Used Buffer
3οΈβ£ Closing Buffers
Close the current buffer without quitting Vim:
Close a specific buffer by number:
Close all buffers except the current one:
4οΈβ£ (Optional) Fuzzy Buffer Switching
If you're using Neovim with Telescope:
This will open a fuzzy search window for buffers.
# custom key mapping for faster buffer switching? π
πΉ Custom Key Mappings for Faster Buffer Switching
Here are some handy key mappings for navigating buffers quickly in Vim/Neovim. Add these to your ~/.vimrc
(Vim/GVim) or ~/.config/nvim/init.vim
(Neovim):
πΉ Explanation of Key Bindings
<Tab>
β Move to the next buffer.<Shift-Tab>
β Move to the previous buffer.<Leader>bd
β Delete (close) the current buffer.<Leader>bl
β List buffers and select one by typing its number.
Note: The
<Leader>
key is usually mapped to\
(backslash) or<Space>
(which you can configure in your Vim settings).
πΉ Advanced: Buffer Navigation with Fuzzy Search (Neovim)
If you use Telescope (Neovim Plugin), you can add this key mapping:
Now pressing <Leader>bb
will show a fuzzy search window to switch between buffers quickly.
πΉ Want a Tab-Like Behavior with Buffers?
Vim tabs and buffers are different concepts. If you want buffers to behave like tabs, try:
This allows you to switch buffers without closing unsaved files.
π Conclusion
Buffers are temporary storage for open files (like tabs in a browser but more lightweight).
Both Vim and Emacs use buffers, but Emacs extends the concept further.
Key mappings make buffer switching faster (
<Tab>
,<S-Tab>
,<Leader>bd
).Telescope (Neovim) makes searching through buffers even smoother.
Would you like an autoclosing buffer manager (like BufOnly
or bufkill.vim
) for better control? π
Hereβs the Lua version of your key mappings for Neovim. Add this to your init.lua
or inside a lua
configuration file like ~/.config/nvim/lua/keymaps.lua
and source it.
πΉ Lua Key Mappings for Buffers
πΉ Explanation
vim.keymap.set(mode, key, command, options)
"n"
β Normal mode"<Tab>"
β Keybinding":bnext<CR>"
β Command executed{ noremap = true, silent = true }
β Prevents recursive mapping and suppresses output This format is more efficient in Neovim and follows modern Lua-based configurations.
Would you like buffer closing improvements (e.g., bdelete
vs bwipeout
)? π
Replacing Words
You can achieve this functionality in Neovim/Vim by setting up a key mapping that:
Selects the word under the cursor or allows you to visually select a word.
Highlights all occurrences of the word in the document.
Allows you to replace the word with an alternative easily.
πΉ Lua Key Mapping (for Neovim)
Add this to your init.lua
(or keymaps.lua
if you separate configs):
πΉ Explanation
<C-r><C-w>
β Inserts the word under the cursor into the command line.:%s/word//g
β Searches (s/
) the entire file (%
) for occurrences ofword
and prepares for replacement.<Left><Left>
β Moves the cursor left twice so you can type the replacement word easily.Visual mode (
v
):h
is used as a register to store the selected text ("hy
).The word is then inserted into the search pattern with
<C-r>h
.
πΉ How to Use
Move the cursor to a word (or visually select a word).
Press
<Leader>h
(or<C-h>
if you prefer).All occurrences of the word will be highlighted.
Type the replacement word and press
<Enter>
.
If you want to manually confirm each replacement instead of replacing all occurrences at once, you can use Vimβs interactive substitution mode with the c
(confirm) flag.
πΉ Updated Key Mappings for Manual Confirmation
Modify your Neovim init.lua
(or keymaps.lua
file) to use c
for confirming each replacement:
πΉ Whatβs Different?
Adding
c
in:%s/.../gc
This enables interactive replacement mode, so Vim will prompt you for each occurrence before replacing.
πΉ How It Works
Move the cursor to a word (or visually select a word).
Press
<Leader>h>
(or<C-h>
if you prefer).Vim will highlight the first match and prompt:
replace with (y/n/a/q/l/^E/^Y)?
y
β Replace this occurrencen
β Skip this occurrencea
β Replace all occurrences (without asking again)q
β Quit replacementl
β Replace this occurrence and quit afterward<Ctrl-E>
/<Ctrl-Y>
β Scroll the screen while deciding
Last updated