Add (Y/n) confirm for existing unsaved buffer?

I’m using Neovide which doesn’t have a popup if exiting a buffer with unwritten changes.

I thought it might be cool to do this with a neovide confirmation, in the status line or wherever, so I don’t need to do :q! over and over (I can just use hotkey like close tab)

Currently I had something like this but it seems to really conflict with a lot of plugins and generate errors. Is there something I’m doin wrong?

vim.api.nvim_create_autocmd('BufWinLeave', {
  callback = function()
    if vim.bo.modified then
      vim.ui.select(
        { 'Save', 'Lose', 'Cancel' },
        { prompt = 'Save or lose before leave?' },
        function(choice)
          if choice == 'Save' then
            vim.cmd('write!')
            vim.cmd('blast')
          elseif choice == 'Lose' then
            vim.cmd('edit!')
            vim.cmd('blast')
          end
        end
      )
      vim.cmd('last')
    end
  end
})

I use this (paired with dressing.nvim)

local function quit()
  local bufnr = vim.api.nvim_get_current_buf()
  local buf_windows = vim.call("win_findbuf", bufnr)
  local modified = vim.api.nvim_get_option_value("modified", { buf = bufnr })
  if modified and #buf_windows == 1 then
    vim.ui.input({
      prompt = "... unwritten changes, want to quit? (y/n) ",
    }, function(input)

      if input == "y" then
        vim.cmd("qa!")
      end
    end)
  else
    vim.cmd("qa!")
  end
end

vim.keymap.set("n", "<leader>q", quit, { silent = true })
1 Like

Dude thats awesome, thank you!

1 Like

This is built in with “:confirm”. Try :confirm quitall, :confirm bdelete, etc.

2 Likes

Dude holy crap WTF… thats awesome

1 Like

Thats so much better. How has no one mentioned this, ive searched forever for something like that.

1 Like

That requires to always put confirm in front of commands. I think much better to :set confirm so you will be always asked for confirmation instead of receiving error.

2 Likes