2

In the quick-fix window, if I'm loading new results, the previous buffers stay which I don't want. I want only those buffers to stay which I open intentionally (e.g. to write new code, do diff of files or write test cases).

In the case of searching in files, the intention is to temporary open files to see the matching results rather than to keep them. If I keep them open then there will be a large number of open buffers which will make it difficult to switch between them.

To prevent this I added a mapping in my .vimrc file like so:

nnoremap <expr> <F2> (getreg('#')==getreg('%')) ? '<CR><C-W>p':'<CR>:bd#<CR><C-W>p'

This actually deletes the last buffer but sometimes gives an error that E516: No buffers were deleted. This happens because the condition (getreg('#')==getreg('%')) always returns false.

What is the best way of re-writing this mapping to avoid the error?

I'm open to any suggestions if my way of using Vim is not great

7
  • Welcome to Vi and Vim! Can you clarify what you’re trying to accomplish? It’s usually fine for previous buffers to stay around, especially with the hidden option set and judicious window use. – D. Ben Knoble Jul 23 '20 at 16:32
  • I want only those buffers to stay which i open intentionally(e.g. to write new code, do diff of files or write test cases). In case of searching in files, the intention is to temporary open files to see the matching results rather than keep them. If i keep them open then there will be a number of open buffers which which will make it difficult to switch buffers. I'm open to any suggestions if my way of using Vim is not great – Haris Muzaffar Jul 23 '20 at 17:25
  • @HarisMuzaffar I think you should edit your initial post to provide the details you give in this comment. If I understand correctly, you would like to get rid of every buffer that is not in the quickfix when you open that quickfix? Also, welcome! – Biggybi Jul 23 '20 at 18:43
  • @HarisMuzaffar perhaps you’re falling into a trap of inefficient buffer navigation; I can suggest some other navigation techniques, though they’re tangential to your question. Happy to Vi and Vim Chat about those on the side. In the meantime, Biggybi is right: would you edit as much detail as you can into the question, and hopefully someone will be able to work out an answer – D. Ben Knoble Jul 23 '20 at 19:12
  • I have edited the question. – Haris Muzaffar Jul 23 '20 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.