Code Monkey home page Code Monkey logo

nvim-bufdel's Introduction

nvim-bufdel

A very small Neovim plugin to improve the deletion of buffers.

Improvements:

  • Preserve the layout of windows. Deleting a buffer will no longer close any window unexpectedly (see demo).
  • Cycle through buffers according to their number. This is especially helpful when using a bufferline: we get the same behavior as closing tabs in Chrome / Firefox (see demo).
  • Terminal buffers are deleted without prompt.
  • Exit Neovim when last buffer is deleted.

demo

Here the same buffer is displayed in left and top-right window. Deleting that buffer preserves the window layout and the first buffer with a number greater than the deleted one is selected instead (the one immediately to the right in the bufferline).

Installation

With packer.nvim:

use {'ojroques/nvim-bufdel'}

With paq-nvim:

paq {'ojroques/nvim-bufdel'}

Usage

Delete the current buffer:

:BufDel

Delete the current buffer and ignore changes:

:BufDel!

Delete a buffer by its name or number (use quotes in case the buffer name is a number):

:BufDel <bufexpr>

Configuration

You can pass options to the setup() function. Here are all options with their default settings:

require('bufdel').setup {
  next = 'cycle'  -- or 'alternate'
}

The next option is used to retrieve the next buffer to show after deleting one. By default the plugin cycles through buffers according to their number. To show instead the alternate buffer, set the option to alternate.

Direct Integration

The plugin fits in a single file, you can very well download it and include it among your config files.

You can also integrate the command directly into your config. Here's a condensed version of the plugin (minus minor improvements) in Lua:

function delete_buffer()
  local buflisted = fn.getbufinfo({buflisted = 1})
  local cur_winnr, cur_bufnr = fn.winnr(), fn.bufnr()
  if #buflisted < 2 then cmd 'confirm qall' return end
  for _, winid in ipairs(fn.getbufinfo(cur_bufnr)[1].windows) do
    cmd(string.format('%d wincmd w', fn.win_id2win(winid)))
    cmd(cur_bufnr == buflisted[#buflisted].bufnr and 'bp' or 'bn')
  end
  cmd(string.format('%d wincmd w', cur_winnr))
  local is_terminal = fn.getbufvar(cur_bufnr, '&buftype') == 'terminal'
  cmd(is_terminal and 'bd! #' or 'silent! confirm bd #')
end

Or in Vimscript:

function! s:delete_buffer()
  let l:buflisted = getbufinfo({'buflisted': 1})
  let [l:cur_winnr, l:cur_bufnr] = [winnr(), bufnr()]
  if len(l:buflisted) < 2 | confirm qall | return | endif
  for l:winid in getbufinfo(l:cur_bufnr)[0].windows
    execute(win_id2win(l:winid) . 'wincmd w')
    if l:cur_bufnr == l:buflisted[-1].bufnr | bp | else | bn | endif
  endfor
  execute(l:cur_winnr . 'wincmd w')
  let l:is_terminal = getbufvar(l:cur_bufnr, '&buftype') == 'terminal'
  if l:is_terminal | bd! # | else | silent! confirm bd # | endif
endfunction

License

LICENSE

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.