0

If the quickfix window is open, I can close it with :cclose. I can map that to a shortcut key:

vim.keymap.set('n', '<C-q>', ":cclose<CR>")
vim.keymap.set('n', '<C-q>', vim.cmd.cclose)

But if I write a user-defined function and try to close the window and then do some other stuff, it doesn't work:

vim.api.nvim_create_user_command('Checkit',
    function()
        vim.cmd.cclose()
        print('🐰 checking ...')
        local output = vim.fn.systemlist("check some stuff here (this takes a while)")
        local errors = ... -- parse output
        vim.fn.setqflist(errors)
        vim.cmd.copen()
    end,
    { nargs = 0 }
)

The quickfix window never closes, even if the system command here takes several seconds.

How can I close the quickfix window at the beginning of this user-defined function?

4
  • It could be that the screen doesn’t redraw; do you have lazyredraw set?
    – D. Ben Knoble
    Commented Apr 6 at 17:46
  • (Are you aware that your function looks a lot like :make after setting makeprg and errorfmt?)
    – D. Ben Knoble
    Commented Apr 6 at 17:47
  • @D.BenKnoble - :set nolazyredraw did not help, but adding vim.cmd.redraw() at the beginning of my script fixed it. Thank you for pointing me in the right direction. (You may wish to add that as an answer.)
    – Sasgorilla
    Commented Apr 12 at 17:27
  • @D.BenKnoble - I was not aware of makeprg - I'm definitely duplicating it here. Thank you!
    – Sasgorilla
    Commented Apr 12 at 17:29

1 Answer 1

1

I suspect the screen isn't redrawing when you want: use :redraw[!] or the Lua equivalent to force that.

You might also be interested in using makeprg and errorfmt by writing a compiler plugin (:help write-compiler-plugin) so that :compiler checkit gives you the write settings and :make runs your executable (parsing the output with errorfmt into the quickfix list).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.