š¹ 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:
:ls
or
:buffers
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:
:buffer <buffer_number>
Example:
:buffer 3
(This will switch to bufferĀ 3.)
Cycle Through Buffers
- Next Buffer:
:bnext
or simply
:bn
- Previous Buffer:
:bprevious
or simply
:bp
Switch to Last Used Buffer
:b#
3ļøā£ Closing Buffers
- Close the current buffer without quitting Vim:
:bd
- Close a specific buffer by number:
:bd <buffer_number>
Example:
:bd 3
- Close all buffers except the current one:
vim
`:bufdo bd`
or
:%bd|e#
4ļøā£ (Optional) Fuzzy Buffer Switching
If you're usingĀ Neovim with Telescope:
:Telescope buffers
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):
" Switch to the next buffer nnoremap <Tab> :bnext<CR> " Switch to the previous buffer nnoremap <S-Tab> :bprevious<CR> " Close the current buffer nnoremap <Leader>bd :bd<CR> " Show buffer list and select one using fzf (if installed) nnoremap <Leader>bl :ls<CR>:buffer<Space>
š¹ 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:
`nnoremap <Leader>bb :Telescope buffers<CR>`
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:
set hidden
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
-- Define a shortcut for setting keymaps
local keymap = vim.keymap.set local opts = { noremap = true, silent = true }
-- Switch to the next buffer
keymap("n", "<Tab>", ":bnext<CR>", opts) -- Switch to the previous buffer
keymap("n", "<S-Tab>", ":bprevious<CR>", opts)
-- Close the current buffer
keymap("n", "<Leader>bd", ":bd<CR>", opts)
-- Show buffer list and select one using FZF (if installed)
keymap("n", "<Leader>bl", ":ls<CR>:buffer<Space>", opts) -- Show buffer list using Telescope (if installed)
keymap("n", "<Leader>bb", ":Telescope buffers<CR>", opts)
š¹ 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):
local keymap = vim.keymap.set local opts = { noremap = true, silent = true }
-- Search and highlight all occurrences of the selected word
keymap("n", "<Leader>h", ":%s/<C-r><C-w>//g<Left><Left>", opts)
-- Same for visual mode (select a word, then press Leader-h)
keymap("v", "<Leader>h", '"hy:%s/<C-r>h//g<Left><Left>', opts)
-- Alternative: Use Ctrl-H instead of Leader-h
keymap("n", "<C-h>", ":%s/<C-r><C-w>//g<Left><Left>", opts)
keymap("v", "<C-h>", '"hy:%s/<C-r>h//g<Left><Left>', opts)`
š¹ 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 ofĀword
Ā 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:
local keymap = vim.keymap.set local opts = { noremap = true, silent = true }
-- Search and manually replace each instance of the word under the cursor
keymap("n", "<Leader>h", ":%s/<C-r><C-w>//gc<Left><Left><Left>", opts)
-- Same functionality for visual mode (select a word, then press Leader-h)
keymap("v", "<Leader>h", '"hy:%s/<C-r>h//gc<Left><Left><Left>', opts)
-- Alternative: Use Ctrl-H instead of Leader-h for manual replacements
keymap("n", "<C-h>", ":%s/<C-r><C-w>//gc<Left><Left><Left>", opts)
keymap("v", "<C-h>", '"hy:%s/<C-r>h//gc<Left><Left><Left>', opts)`
š¹ 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