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

3

6

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
Tyree answered 23/1, 2014 at 23:18 Comment(2)
Sounds as though it may be helpful: a means of toggling the quickfix window (close it if it’s open, open it if it’s not) is given at vim.wikia.com/wiki/Toggle_to_open_or_close_the_quickfix_window.Destructible
@ebenezer I'll keep this one on the back burner for now. I have <A-down> and <A-up> mapped for moving between upper and lower windows and F3 maps to quit, so <A-down>F3 is just as few/many keystrokes as the given solution.Tyree
B
13

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.

Belga answered 24/1, 2014 at 7:35 Comment(0)
T
6

in lua:

-- close quickfix menu after selecting choice
vim.api.nvim_create_autocmd(
  "FileType", {
  pattern={"qf"},
  command=[[nnoremap <buffer> <CR> <CR>:cclose<CR>]]})
Tamelatameless answered 7/1, 2023 at 11:10 Comment(0)
C
0

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>]] }
)
Conductivity answered 3/1 at 9:48 Comment(1)
Welcome to SO and thank you for writing an answer. The question was about Vim, not Neovim. Lua code may be of limited value, here.Amaryllidaceous

© 2022 - 2024 — McMap. All rights reserved.