Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZB: command to delete buffer without closing the window #2434

Open
norcalli opened this issue Apr 15, 2015 · 35 comments
Open

ZB: command to delete buffer without closing the window #2434

norcalli opened this issue Apr 15, 2015 · 35 comments
Labels
enhancement feature request
Milestone

Comments

@norcalli
Copy link

All other solutions I know of are hacks that kinda work (usually by creating a new split and then deleting the buffer in that window, and attempting to cover all possible cases for this), but intrinsic support for this would be greatly appreciated.

@justinmk
Copy link
Member

usually by creating a new split and then deleting the buffer in that window

That isn't necessary at all. The hack I use switches to the next valid buffer (or creates a new empty one), then deletes the original. I'm planning to release it as part of a plugin...

@norcalli
Copy link
Author

@justinmk I think you'll find it's been done already in many different ways.

The fact that you call it a hack would seem to support this even more. It doesn't seem like it should be difficult to implement. *And is more something that should be core functionality than a plugin really.

@justinmk
Copy link
Member

Yes, I know. But the idea of adding a new core command for this doesn't sound appealing. Details would be welcome though.

@splinterofchaos
Copy link

:enew!?

Edit a new, unnamed buffer. Discard any changes to the current buffer.

@norcalli
Copy link
Author

@justinmk A whole terminal was added along with autocommands, a command, and a Terminal class. This is incredibly light weight in comparison.

And surprisingly, bclose is not taken AFAIK, so that would be a valid name, and upon using it, it would destroy the buffer and move to the next available buffer while maintaining the split window or moving to a scratch buffer if it is the last buffer. It is meant to preserve window layout while still closing a buffer.

@splinterofchaos enew! doesn't close the buffer with set hidden

@tarruda
Copy link
Member

tarruda commented Apr 15, 2015

Why not simply implement this as a buffer option?

@justinmk
Copy link
Member

A whole terminal was added along with autocommands, a command, and a Terminal class. This is incredibly light weight in comparison.

I'm talking about the UI, not the technical cost, and in proportion to the benefit. The technical and UI cost of :terminal in relation to its benefit is worth it. Adding :bdelete_but_leave_my_window_alone may not be.

bclose is not taken AFAIK,

So what about someone who wants to :bwipe or :bunload without closing the window?

Why not simply implement this as a buffer option?

Users will still need to wrap that option in a command, so it doesn't gain anything over the current situation.

@justinmk
Copy link
Member

enew! doesn't close the buffer with set hidden

But this does:

set bufhidden=delete
enew!

@norcalli

This comment has been minimized.

@justinmk

This comment has been minimized.

@norcalli

This comment has been minimized.

@justinmk justinmk added the idea label Apr 15, 2015
@tarruda
Copy link
Member

tarruda commented Apr 15, 2015

setl bufhidden=delete | bnext

Really simple, no need for a separate option just to achieve that 😄

@HarmtH
Copy link
Contributor

HarmtH commented Apr 15, 2015

Not entirely true, when you have two windows open displaying the buffer you want to delete, 'bd' deletes the buffer (and both windows), while 'setl bufhidden=delete | bnext' only goes to the next buffer in the current window and nothing more.

Edit: Of course the buffer is still being marked as bufhidden=delete, so :bnext in the other window will delete the buffer then.

@norcalli
Copy link
Author

@HarmtH I've realized this as I've been writing this plugin for the past 20 minutes. It seems it's more nuance than I thought. You have to make sure no windows are pointing at the buffer.

@norcalli

This comment has been minimized.

@HarmtH

This comment has been minimized.

@justinmk justinmk added the enhancement feature request label Oct 19, 2016
@weichm

This comment has been minimized.

@justinmk
Copy link
Member

justinmk commented Jun 5, 2017

@weichm What proposed feature? Nothing concrete was actually proposed.

The request can't be achieved with just one new command, see here. Can't be just one new option either.

Should we add 4 new options/commands for this?

Something that might work: a global option that says "always preserve windows when deleting/wiping/unloading buffers".

@justinmk
Copy link
Member

justinmk commented Jun 5, 2017

:e term://ipython3
but if I quit the interpreter, the window layout is destroyed.

Only if you delete the buffer. You can exit terminal-mode with CTRL-\ CTRL-N instead.

@weichm

This comment has been minimized.

@justinmk

This comment has been minimized.

@kasymovga

This comment has been minimized.

@justinmk

This comment has been minimized.

@kasymovga

This comment has been minimized.

@kasymovga

This comment has been minimized.

@pantosaur

This comment has been minimized.

@justinmk justinmk reopened this Aug 17, 2019
@JSamir

This comment has been minimized.

@norcalli
Copy link
Author

norcalli commented Dec 13, 2019

Oh wow, I got a notification on this issue today and I didn't realize that I was the original creator. FYI, I can do this with lua and it's pretty much exactly what I want. Now that I'm on the neovim team, maybe we can add this to the core, but here it is for anyone who's interested:

local api = vim.api

local function nvim_loaded_buffers()
  local result = {}
  local buffers = api.nvim_list_bufs()
  for _, buf in ipairs(buffers) do
    if api.nvim_buf_is_loaded(buf) then
      table.insert(result, buf)
    end
  end
  return result
end

-- Kill the target buffer (or the current one if 0/nil)
function buf_kill(target_buf, should_force)
  if not should_force and vim.bo.modified then
    return api.nvim_err_writeln('Buffer is modified. Force required.')
  end
  local command = 'bd'
  if should_force then command = command..'!' end
  if target_buf == 0 or target_buf == nil then
    target_buf = api.nvim_get_current_buf()
  end
  local buffers = nvim_loaded_buffers()
  if #buffers == 1 then
    api.nvim_command(command)
    return
  end
  local nextbuf
  for i, buf in ipairs(buffers) do
    if buf == target_buf then
      nextbuf = buffers[(i - 1 + 1) % #buffers + 1]
      break
    end
  end
  api.nvim_set_current_buf(nextbuf)
  api.nvim_command(table.concat({command, target_buf}, ' '))
end

This is how I use it.

" normal kill
nnoremap <a-c> <Cmd>lua buf_kill(0)<CR>
" force kill
nnoremap <a-s-c> <Cmd>lua buf_kill(0, true)<CR>

It will unload the current buffer and switch to another buffer without closing the window.

E: Also donate your money to the neovim team or your favorite charity instead.

@kasymovga

This comment has been minimized.

@norcalli
Copy link
Author

It's not really a hack for my use case where I want to have the option to either close the window or not. Since you want it to be some kind of default mode option @kasymovga, I recommend opening a new issue, because I would consider this one to be closed.

@justinmk
Copy link
Member

justinmk commented Jan 1, 2020

This would not be implemented as an option, that would break plugins. Instead it could be a new command, I'm thinking ZB (analogous to ZZ and ZQ).

@justinmk justinmk reopened this Jan 1, 2020
@justinmk justinmk changed the title Feature request: command/function to delete buffer without closing the window ZB: command/function to delete buffer without closing the window Jan 1, 2020
@justinmk justinmk changed the title ZB: command/function to delete buffer without closing the window ZB: command to delete buffer without closing the window Jan 1, 2020
@justinmk justinmk added this to the todo milestone Jan 1, 2020
@HarmtH
Copy link
Contributor

HarmtH commented Jan 1, 2020

For the Ex command, i would suggest adding a modifier comparable to :keepjumps and :keepmarks, let's say :keepwindows.

Then it won't only be useful for :bdelete, but also for :bunload, :bwipeout, and maybe other commands.

@justinmk justinmk mentioned this issue Jan 25, 2020
@severij

This comment has been minimized.

@Glavnokoman

This comment has been minimized.

@Glavnokoman

This comment has been minimized.

@neovim neovim locked as off-topic and limited conversation to collaborators Oct 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement feature request
Projects
None yet
Development

No branches or pull requests