kazhala/close-buffers.nvim

github github
buffers plugin
star 30
stars
alert-circle 2
open issues
users 2
subscribers
git-branch 0
forks
CREATED

2021-07-27

UPDATED

3 months ago

packer

require('packer').startup(function()
  use 'kazhala/close-buffers.nvim'
end)

paq

require "paq" { 
  'kazhala/close-buffers.nvim'
}

close-buffers.nvim

Lua port of close-buffers.vim with serveral feature extensions. This plugin allows you to quickly delete multiple buffers based on the conditions provided.

Demo

Requirements

Neovim >= 0.5

Installation

packer.nvim

use 'kazhala/close-buffers.nvim'

vim-plug

plug 'kazhala/close-buffers.nvim'

Configuration

require('close_buffers').setup({
  filetype_ignore = {},  -- Filetype to ignore when running deletions
  file_glob_ignore = {},  -- File name glob pattern to ignore when running deletions (e.g. '*.md')
  file_regex_ignore = {}, -- File name regex pattern to ignore when running deletions (e.g. '.*[.]md')
  preserve_window_layout = { 'this', 'nameless' },  -- Types of deletion that should preserve the window layout
  next_buffer_cmd = nil,  -- Custom function to retrieve the next buffer when preserving window layout
})

Example

By default, the next_buffer_cmd will attempt to get the next buffer by using the vim buffer ID.

This may not be as useful if you use bufferline plugins like nvim-bufferline.lua since you can rearrange the buffer orders ignoring the buffer ID. The following example will use the cycle command provided by nvim-bufferline.lua to get the next buffer when preserving the window layout.

require('close_buffers').setup({
  preserve_window_layout = { 'this' },
  next_buffer_cmd = function(windows)
    require('bufferline').cycle(1)
    local bufnr = vim.api.nvim_get_current_buf()

    for _, window in ipairs(windows) do
      vim.api.nvim_win_set_buf(window, bufnr)
    end
  end,
})

vim.api.nvim_set_keymap(
  'n',
  '<leader>th',
  [[<CMD>lua require('close_buffers').delete({type = 'hidden'})<CR>]],
  { noremap = true, silent = true }
)
vim.api.nvim_set_keymap(
  'n',
  '<leader>tu',
  [[<CMD>lua require('close_buffers').delete({type = 'nameless'})<CR>]],
  { noremap = true, silent = true }
)
vim.api.nvim_set_keymap(
  'n',
  '<leader>tc',
  [[<CMD>lua require('close_buffers').delete({type = 'this'})<CR>]],
  { noremap = true, silent = true }
)

Usage

Lua

-- bdelete
require('close_buffers').delete({type = 'hidden', force = true})
require('close_buffers').delete({type = 'nameless'})
require('close_buffers').delete({type = 'this'})

-- bwipeout
require('close_buffers').wipe({type = 'all', force = true})
require('close_buffers').wipe({type = 'other'})

Vim

The plugin exposes 2 vim commands, BDelete and BWipeout. Each takes a single argument listed under the options section. To force a deletion, simply append the command with a bang.

:BDelete hidden
:BDelete! nameless
:BWipeout! all
:BDelete other
:BDelete! this

Options

type

type description
hidden Delete all listed buffers that's not visible in the current window
nameless Delete all listed buffers without name
this Delete the current focused buffer without changing the window layout
all Delete all listed buffers
other Delete all listed buffers except the current focused buffer

force

Append a bang to the bwipeout or bdelete commands to force a deletion.

regex

Using this argument will take precedence over file_regex_ignore setting.

Delete buffers which matches the regex provided. When providing regex as an argument, the type argument can be optional which will fallback to the value all.

-- delele all markdown buffers
require('close_buffers').delete({ regex = '.*[.]md', force = true })

-- delete all hidden lua buffers
require('close_buffers').delete({ type = 'hidden', regex = '.*[.]lua' })
:BDelete! regex=.*[.]md
:BDelete hidden regex=.*[.]lua

glob

Using this argument will take precedence over file_glob_ignore setting.

Similar to regex, delete buffers which matches the glob pattern provided. When providing glob as an argument, the type argument can be optional which will fallback to the the value all.

-- delele all markdown buffers
require('close_buffers').delete({ glob = '*.md', force = true })

-- delete all hidden lua buffers
require('close_buffers').delete({ type = 'hidden', glob = '*.lua' })
:BDelete! glob=*.md
:BDelete hidden glob=*.lua

Credit