Code Monkey home page Code Monkey logo

close-buffers.nvim's Introduction

close-buffers.nvim

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

Demo

Requirements

Neovim >= 0.5

Installation

use 'kazhala/close-buffers.nvim'
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 }) -- Delete all non-visible buffers
require('close_buffers').delete({ type = 'nameless' }) -- Delete all buffers without name
require('close_buffers').delete({ type = 'this' }) -- Delete the current buffer
require('close_buffers').delete({ type = 1 }) -- Delete the specified buffer number
require('close_buffers').delete({ regex = '.*[.]md' }) -- Delete all buffers matching the regex

-- bwipeout
require('close_buffers').wipe({ type = 'all', force = true }) -- Wipe all buffers
require('close_buffers').wipe({ type = 'other' }) -- Wipe all buffers except the current focused
require('close_buffers').wipe({ type = 'hidden', glob = '*.lua' }) -- Wipe all buffers matching the glob

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
:BDelete this
:BDelete 1
:BDelete regex='.*[.].md'

:BWipeout! all
:BWipeout other
:BWipeout hidden glob=*.lua

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

bufnr

The bufnr would be one of the result from the vim :ls command.

You can also directly delete a buffer by providing buffer number as the type. The type argument will automatically change to this when specifying a buffer number as the type.

require('close_buffers').delete({ type = 1 }) -- Delete buffer with number 1
:BDelete 1

force

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

require('close_buffers').this({ type = 'this', force = true })
:BDelete! this

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.

require('close_buffers').delete({ regex = '.*[.]md', force = true }) -- Delete all markdown buffers
require('close_buffers').delete({ type = 'hidden', regex = '.*[.]lua' }) -- Delete all hidden lua buffers
: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 value all.

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

Credit

close-buffers.nvim's People

Contributors

kazhala avatar petertriho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

close-buffers.nvim's Issues

Go to previous buffer instead of the last when closing current.

It seems goes to last buffer instead of previous one, for example:

  • 3 buffers in order: A B C
  • switch from A to C
  • close current buffer C with lua require("close_buffers").delete({ type = "this" })
  • it switches to B instead of A

Is it possible to switch back to A when C is closed? Because A is the buffer before it switched to C.

Thanks.

Menu

Maybe that's one more thing, but a menu might be useful
menu

Delete all unlisted buffers

Cheers on this great plugin !

Would it be possible to get rid of all unlisted buffers as well, by specifying it as a type for instance ? Neovim usually has a bunch of these open and i'd like to close them before saving a session and closing Neovim. This way when the session is restored these buffers won't come up as unnamed (and named, for instance packer status) buffers (which won't go away during session restore with BWipeout! nameless or it's BDelete counterpart).

feature request: close nvim after closing last buffer

hey, i'm looking for something that would optionally close neovim, when there is just one buffer open and i invoke a command to close it. do you think something like this would be in the scope of this project?

Incorrect behavior when closing last buffer when NvimTree is open

When closing the last buffer with NvimTree open and preserve_window_layout on, close-buffers will also close the NvimTree buffer (but correctly preserve the windows), even though I have NvimTree in the ignore list.
This bug occurs when running :BDelete with this, all, etc (anything with preserve_window_layout on and that removes the last buffer)
Before running :BDelete this
image

After:
image

Correct behavior:
image

Invalid window id: 1014

E5108: Error executing lua ...ker/opt/close-buffers.nvim/lua/close_buffers/buffers.lua:61: Invalid window id: 1014
stack traceback:
        [C]: in function 'nvim_win_set_buf'
        ...ker/opt/close-buffers.nvim/lua/close_buffers/buffers.lua:61: in function 'preserve_window_layout'
        ...ker/opt/close-buffers.nvim/lua/close_buffers/buffers.lua:176: in function 'close'
        ...pack/packer/opt/close-buffers.nvim/lua/close_buffers.lua:22: in function 'delete'
        [string ":lua"]:1: in main chunk
  • nvim-tree installed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.