3

I use gutentags and gutentags-plus for generating and managing tag files. When I use the key combinations setup by them ( cX usually ), it opens the relevant lines in a quickfix window and I can use :cnext etc for navigation. But how do I close them quickfix window and go back to the file I was editing after I have gone through the lines found in cscope?

PS: Actually I am using global instead of cscope but I guess on VIM side it should not matter.

3
  • 3
    I'm not sure what your PS means. However I think you're looking for :cclose :h :cclose? – statox Apr 25 '19 at 9:57
  • Corrected and added a link to clarify. Sorry about that! – Osada Lakmal Apr 25 '19 at 10:42
  • Ok I get it now, but my solution still stands: is :cclose the command you're looking for? – statox Apr 25 '19 at 11:44
8

To close the quickfix window, you use :cclose, as @statox mentioned in the comments.

This won't affect which code buffer you're viewing, though. Vim doesn't distinguish between the buffer you were looking at originally and the buffers you navigated to via the quickfix, so to go back you are going to have to use Vim's regular buffer switching commands. If you know the name or number of the original buffer, :b is probably quickest.

If not, you could try mashing Ctrl-O to jump backwards through the jumplist till you reach the right buffer, or you might want to set up some mappings* (or a plugin) for faster buffer navigation.

If you want something automated, how about adding something like following to your .vimrc?

nnoremap <silent> <leader>gs mA:GscopeFind s <C-R><C-W><cr>
nnoremap <leader>b :cclose<CR>`A

The first mapping adapts your current search mapping to first set an uppercase mark A and then continue with its original behaviour.

The second creates a new mapping that closes the quickfix and then returns to the marked position.

* Make sure you read as far down as the EDIT, because it's after that that the juicy details are included.

10
  • Or faster solution would be to create a new tab with the current buffer so that he can just close the tabpage with the quickfix window altogether and go back to the newly created tagpage. But he has to remember to do <C-w>T every time. Or maybe an autocmd trigger with QuickFixCmdPost etc. to open the quickfix window after creating a new tab? That could also work, right? – klaus Apr 25 '19 at 13:40
  • 1
    @klaus Not sure if you saw my edit with the new mappings? Your idea is good too, though! – Rich Apr 25 '19 at 13:41
  • No, you just edited it after I commented. That mapping makes more sense because of automation. Good stuff! – klaus Apr 25 '19 at 13:42
  • 1
    Hmmm, I don't see how :nmap cX mAcX will work, since the cX in it can only call the same mapping again. (In other words, infinite recursion.) If defined after the original cX mapping, it will just overwrite it. Perhaps that part of the answer needs an update...? – filbranden Jun 22 '20 at 15:07
  • 1
    @filbranden What's particularly weird about this one, is not so much the recursive mapping--which could just be a brainfart--but the fact that I wrote an entire explanatory paragraph afterwards that was just utter rubbish! – Rich Jun 22 '20 at 15:59
0

@klaus makes the correct suggestion to set a mark. However I needed to set it before the quick fix window appeared not while traversing the list. I am using gutentags_plus plugin to make cscope work with guten-tags plugin. So I just redefined Gscopefind command mappings from

noremap <silent> <leader>gs :GscopeFind s <C-R><C-W><cr>

to

noremap <silent> <leader>gs mA :GscopeFind s <C-R><C-W><cr>

Your Answer

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

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