šŸ”¹ 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:

  1. Selects the word under the cursorĀ or allows you toĀ visually selectĀ a word.
  2. Highlights all occurrences of the word in the document.
  3. 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

  1. Move the cursor to a wordĀ (or visually select a word).
  2. PressĀ <Leader>hĀ (orĀ <C-h>Ā if you prefer).
  3. All occurrences of the word will be highlighted.
  4. 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

  1. Move the cursor to a wordĀ (or visually select a word).

  2. PressĀ <Leader>h>Ā (orĀ <C-h>Ā if you prefer).

  3. Vim will highlight the first matchĀ and prompt:

    replace with (y/n/a/q/l/^E/^Y)?

    • y → Replace this occurrence
    • n → Skip this occurrence
    • a → ReplaceĀ all occurrencesĀ (without asking again)
    • q → Quit replacement
    • l → Replace this occurrence andĀ quit afterward
    • <Ctrl-E>Ā /Ā <Ctrl-Y> → Scroll the screen while deciding