TransWikia.com

How to delete the previous buffer when loading new result from quick-fix window?

Vi and Vim Asked by Haris Muzaffar on December 14, 2021

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

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP