Wes Miller
Wes Miller

Reputation: 2231

How can I cause the QuickFix window to close after I select an item in it?

I got the wonderful bookmarks.vim plugin to my vim. I especially like the named bookmarks and using the QuickFix window to list them.

In the code to show the bookmark list I'd like to add something that causes the QuickFix window to close after I select a choice. How do I do that?

" Open all bookmarks in the quickfix window
command! CopenBookmarks call s:CopenBookmarks()
function! s:CopenBookmarks()
let choices = []

for [name, place] in items(g:BOOKMARKS)
let [filename, cursor] = place

call add(choices, {
\ 'text': name,
\ 'filename': filename,
\ 'lnum': cursor[1],
\ 'col': cursor[2]
\ })
endfor

call setqflist(choices)
copen
endfunction

Upvotes: 6

Views: 3389

Answers (3)

Nick Veale
Nick Veale

Reputation: 11

If none of the above work, the BufReadPost quickfix event will also fire.

Here's the lua code for that ...

vim.api.nvim_create_autocmd(
 "BufReadPost",
 { pattern = "quickfix", command = [[nnoremap <buffer> <CR <CR>:cclose<CR>]] }
)

Upvotes: 1

Gijs Groote
Gijs Groote

Reputation: 107

in lua:

-- close quickfix menu after selecting choice
vim.api.nvim_create_autocmd(
  "FileType", {
  pattern={"qf"},
  command=[[nnoremap <buffer> <CR> <CR>:cclose<CR>]]})

Upvotes: 6

Ingo Karkat
Ingo Karkat

Reputation: 172510

Override the <CR> mapping that is used in the quickfix window to select an entry:

:autocmd FileType qf nnoremap <buffer> <CR> <CR>:cclose<CR>

Note: If you don't want this applied to location lists, you need to tweak the mapping a bit.

Upvotes: 13

Related Questions