akinsho / bufferline.nvim

A snazzy bufferline for Neovim

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Feature Request]: I'd like to close only the current tab.

RobGThai opened this issue · comments

What?

Say I have 3 tabs open in Bufferline. I'm on the second tab. I'd like to close this one but the only available options are Right/Left/Others. I think it's really more intuitive to have BufferLineCloseCurrent.

Why?

Not sure if there is a command I'm missing here?

I think it's really more intuitive to have BufferLineCloseCurrent, looking at other similar threads and you suggested :bdelete. However, this delete the whole window instead of a single tab. I'm not sure if that's the intended action.

If you are against of this suggestion, could you help guide me how I could achieve this? I'm a novice when it come to actually knowing about the internal work of vim. My guess is there should be a way to get buffer number of the current tab and send that one to bdelete like so :bdelete! 1234.

If someone looking for a workaround of this situation. I'm currently using nvim-bufdel to solve this.

I still think such foundation action should be consider alongside other "close" commands tho.

Also, I don't understand the reason for this, I had to use a new function to work around the situation, something like:
I had some issues using the plugin with nvim-tree, so I had to resort to this.

function close_current_buffer()
  vim.api.nvim_command('NvimTreeClose')
  vim.api.nvim_command('bdelete')
  vim.api.nvim_command('NvimTreeOpen')
  vim.api.nvim_command('wincmd l')
end

vim.keymap.set('n', '<leader>q',     '<Cmd>lua close_current_buffer()<CR>', {})

It would be great to have that, especially since it's very common to close the current buffer.

commented

lazyvim distribution includes https://github.com/echasnovski/mini.bufremove which solves this. I didn't realize it wasn't part of bufferline in fact.

commented

I've mentioned in previous issues. I don't intend to add this command, as to be honest, I did not want to add any buffer management commands. You have to understand that this plugin is mainly about seeing buffers not about closing them or opening them. There are so many plugins that people have worked on to add solutions for that like bufdel. This isn't a place where I'm going to add more of this and frankly I would deprecate what exists already if I could, but people now depend on it but I certainly won't look to add to it cuz it just adds more surface area for bugs and feature requests

I use these simple tricks, no plugins needed. Alt + c for navigate to the left buffer then close the old one, Alt + Shift + C for the opposite way

  vim.keymap.set("n", "<A-c>", function()
    local buffer_id = vim.fn.bufnr()

    if buffer_id then
      vim.cmd("<cmd>BufferLineCyclePrev")
      vim.cmd("<cmd>bdelete "..buffer_id)
    end
  end)

  vim.keymap.set("n", "<A-C>", function()
    local buffer_id = vim.fn.bufnr()

    if buffer_id then
      vim.cmd("<cmd>BufferLineCycleNext")
      vim.cmd("<cmd>bdelete "..buffer_id)
    end
  end)

I use these simple tricks, no plugins needed. Alt + c for navigate to the left buffer then close the old one, Alt + Shift + C for the opposite way

  vim.keymap.set("n", "<A-c>", function()
    local buffer_id = vim.fn.bufnr()

    if buffer_id then
      vim.cmd("<cmd>BufferLineCyclePrev")
      vim.cmd("<cmd>bdelete "..buffer_id)
    end
  end)

  vim.keymap.set("n", "<A-C>", function()
    local buffer_id = vim.fn.bufnr()

    if buffer_id then
      vim.cmd("<cmd>BufferLineCycleNext")
      vim.cmd("<cmd>bdelete "..buffer_id)
    end
  end)

Great solution, but for me, the cmd function does not expect "cmd" text, so I fixed like this:

vim.keymap.set('n', '<A-c>', function()
  local buffer_id = vim.fn.bufnr()
  vim.cmd 'BufferLineCyclePrev'
  vim.cmd('bdelete ' .. buffer_id)
end, { desc = 'Close current buffer and go to previous' })

vim.keymap.set('n', '<A-C>', function()
  local buffer_id = vim.fn.bufnr()
  vim.cmd 'BufferLineCycleNext'
  vim.cmd('bdelete ' .. buffer_id)
end, { desc = 'Close current buffer and go to next' })